Skip to content
Related Articles

Related Articles

Improve Article

Perl | pos() function in Regular Expression

  • Last Updated : 16 Jun, 2021

pos() function in Perl is used to return the position of the last match using the ‘m’ modifier in Regex. 
pos function can be used with the character classes in Regex to return a list of all the required substring positions in the given string. Global operator ‘g’ can also be used along with the ‘m’ modifier to search for the substring within the whole text.
 

Syntax: pos(String) 
Parameter: String after applying Regular Expression 
Returns: the position of the matched substring 
 

Example 1: Using a substring character 
 

Perl




#!/usr/bin/perl
 
# Program to print position of a substring
$String = "Geeks For Geeks";
 
print" Position of 'G' in string:\n";
 
# Regex to search for substring
# using m modifier
while($String =~ m/G/g)
{
     
    # Finding the position of substring
    # using pos() function
    $position = pos($String);
    print "$position\n";
}
Output: 
Position of 'G' in string:
1
11

 

 
Example 2: Using a character class 
 



Perl




#!/usr/bin/perl
 
# Program to print position of a substring
$String = "Geeks For Geeks";
 
print "Position of all Uppercase characters:\n";
 
# Regex to search for
# all the upper case characters
# using character class
while($String =~ m/[A-Z]/g)
{
     
    # Finding the position of substring
    # using pos() function
    $position = pos($String);
    print "$position, ";
}
 
print "\nPosition of all Lowercase characters:\n";
 
# Regex to search for
# all the lower case characters
# using character class
while($String =~ m/[a-z]/g)
{
     
    # Finding the position of substring
    # using pos() function
    $position = pos($String);
    print "$position, ";
}
Output: 
Position of all Uppercase characters:
1, 7, 11, 
Position of all Lowercase characters:
2, 3, 4, 5, 8, 9, 12, 13, 14, 15,

 

 
Example 3: Position of spaces 
 

Perl




#!/usr/bin/perl
 
# Program to print position of a substring
$String = "Geeks For Geeks";
 
# Regex to search for
# all the spaces
while($String =~ m/\s/g)
{
     
    # Finding the position of substring
    # using pos() function
    $position = pos($String);
    print "$position\n";
}
Output: 
6
10

 

  
Use of \G Assertion to match from specified position:
\G Assertion in Perl Regex is used to match the substring starting from a position specified by pos() function till the matching character specified in the regex. This will return the position of the first occurrence of the character specified by the ‘m’ modifier. 
Example: 
 

Perl




#!/usr/bin/perl
 
# Defining the default string
$_ = "Geeks World is the best";
 
# Terminating character
# using m modifier
m/o/g;
 
# Specifying the starting position
$position = pos();
 
# Using \G Assertion
m/\G(.*)/g;
 
# Printing the position
# and the remaining string
print "$position $1";
Output: 
8 rld is the best

 

In the above example, the position of the first occurrence of the matching substring is printed along with the remaining string. If there is a need to restart counting position for the next occurrence of the matching character, just store the remaining string that is in $1, into the default string.
Example: 
 

Perl




#!/usr/bin/perl
 
# Defining the default string
$_ = "Geeks World is the best among all";
 
# Terminating character
# using m modifier
m/o/g;
 
# Specifying the starting position
$position = pos();
 
# Using \G Assertion
m/\G(.*)/g;
 
# Printing the position
# and the remaining string
print "$position $1\n";
 
# To start counting from the matched character
# until the next possible match
$_ = $1;
m/o/g;
 
$position = pos();
 
# Using \G Assertion
m/\G(.*)/g;
 
# Printing the position
# and the remaining string
print "$position $1\n";
Output: 
8 rld is the best among all
19 ng all

 




My Personal Notes arrow_drop_up
Recommended Articles
Page :