note
ig
<p>From [doc://perlvar]:</p>
<blockquote>$-[0] is the offset of the start of the last successful match.</blockquote>
<p>You have <c>my $re1 = qr/((a+)?(b+)?(c))*/;</c></p>
<p>Your outer capture group may be repeated zero or more times. In the case of your test string, "aacbbbcac", it matches three times:</p>
<p>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.</p>
<p>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.</p>
<p>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".</p>
<p>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.</p>
1060027
1060038