in reply to Re: Re: Infinite loop regex
in thread Infinite loop regex
Perl keeps the position of regexp-matching in mind: bound to a variable! See:
my $var = 'a111a222a333'; $var =~ m/a(\d+)/g and print $1 # prints '111'
The regex ended after the three 1es, at position 4. This position ist stored by perl inside tha variable. You can retrieve it via pos($var). Read on it in `perldoc -f pos`
$var =~ m/a(\d+)/g and print $1; #will now print '222' because the regex starts at # pos($var) (which is 4) #You can also use pos() as an lvalue pos($var) = 0; $var =~ m/a(\d+)/ and print $1; # NO, doesn't print '333' # but '111' because pos($var) wat set to 0 and so # the regexp startsat positin 0 again
Why do I tell you so? If you generate the strng again, the pos() will allways go back to 0, because you do not always match on the same variable, but on a new variable each time.
--
|
---|