![]() |
|
Do you know where your variables are? | |
PerlMonks |
Re^3: Explain a regexp matched group resultby ig (Vicar) |
on Oct 28, 2013 at 19:10 UTC ( #1060041=note: print w/replies, xml ) | Need Help?? |
From perlvar: $-[0] is the offset of the start of the last successful match. You have my $re1 = qr/((a+)?(b+)?(c))*/; Your outer capture group may be repeated zero or more times. In the case of your test string, "aacbbbcac", it matches three times: At the first repeat, it matches "aac" with $1 being "aac", $2 being "aa", $3 being undef (not matched) and $4 being "c", but because of the repeat count it doesn't stop there, so you never see these values. At the second repeat $1 is "bbbc", $2 remains "aa" (group (a+) didn't match in this repeat but $2 is the 'last successful match'), $3 is "bbb" and $4 is "c", but it doesn't stop there, so you don't see these values either. The third and final repeat sets $1 to "ac", $2 to "a" leaves $3 as it was at the last successful match (i.e. "bbb") and sets $4 to "c". So, the issue is that the capture groups return the last successful match rather than the last match or failure as the case may be.
In Section
Seekers of Perl Wisdom
|
|