in reply to Regex matches the same pattern alternatively
Further to ikegami's point, here's an illustration of the way the /g modifier causes the regex engine to interact with the position offset (if that's the right term) of a string as returned by pos.
>perl -wMstrict -le "my $s = 'oneone'; my $regex = qr/one/; ;; for (1 .. 6) { if ($s =~ /$regex/g) { printf qq{matches '$&' at %d, pos at %d \n}, $-[0], pos $s; } else { print 'no match'; } } " matches 'one' at 0, pos at 3 matches 'one' at 3, pos at 6 no match matches 'one' at 0, pos at 3 matches 'one' at 3, pos at 6 no match
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Regex matches the same pattern alternatively
by regexmegex (Initiate) on Oct 23, 2011 at 09:04 UTC |