Next | Regular Expression Mastery | 71 |
In scalar context, /g turns the matcher into an iterator
while ("I like pie" =~ /\w+/ g) { print "<$&>\n"; }
<I> <like> <pie>
Each scalar has a current position
/g starts from the current position and sets it afterwards
You can get and set the current position with the pos function:
my $s = "I like pie"; for ($i = 0; $i < length($s); $i += 2) { pos($s) = $i; $s =~ /\w*/ g; print "<$&>\n"; }
<I> <like> <ke> <> <ie>
A failed match on a string resets its pos
Next | Copyright © 2002 M. J. Dominus |