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.
Parameter: String after applying Regular Expression
Returns: the position of the matched substring
Example 1: Using a substring character
Position of 'G' in string: 1 11
Example 2: Using a character class
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
\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 ‘
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.
8 rld is the best among all 19 ng all
- Perl | Backtracking in Regular Expression
- Perl | 'e' modifier in Regular Expression
- Perl | Quantifiers in Regular Expression
- Perl | Operators in Regular Expression
- Perl | Regular Expressions
- Perl | Special Character Classes in Regular Expressions
- Perl | tell() Function
- Perl | uc() Function
- Perl | each() Function
- Perl | int() function
- Perl | oct() Function
- Perl | sin() Function
- Perl | log() Function
- Perl | cos() Function
- Perl | exp Function
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.