Alter Regular Expression Match Position in Perl

How to alter the regular expression match position?

The perl function pos() retrieves or modifies the position where the next match attempt is going to begin. The beginning of a string (first character) has a position zero. This position can be modified by using the function as the left side of an assignment, e.g., pos($string) = 123;.

An example of altering position can be,

while ($string =~ m/regex/g)  {
print "Found .  Next attempt would be at character " . pos($string)+1 . "\n";

#pos($string)=5;   #this would change the position of the reg ex match at 5.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>