in reply to A regex question.
Because you're using the /g regex modifier for all matches, the string match position never resets to the start of the string, but advances for each and every match performed. (Update: I should have written: advances for each and every successful match performed.)
The fact that the first match in the OPed code is ac:\@Work\Perl\monks>perl -wMstrict -le "my $s = 'xx111yy22xx333yy44zz555xx666'; ;; print qq{first capture: '$1'} if $s =~ m{ (\d+) }xmsg; ;; print qq{subsequent captures: '$1'} while $s =~ m{ (\d+) }xmsg; " first capture: '111' subsequent captures: '22' subsequent captures: '333' subsequent captures: '44' subsequent captures: '555' subsequent captures: '666'
Note that use of the /g modifier in an
if ($string =~ m{ ... }xmsg) { ... }
statement is useless except as a way to introduce exactly the behavior you are seeing!
Update: A number of incremental updates made.
Give a man a fish: <%-(-(-(-<
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: A regex question.
by Anonymous Monk on Jul 31, 2015 at 23:57 UTC | |
by Athanasius (Archbishop) on Aug 01, 2015 at 03:23 UTC | |
by FeistyLemur (Acolyte) on Aug 02, 2015 at 05:15 UTC | |
by AnomalousMonk (Archbishop) on Aug 01, 2015 at 08:51 UTC |