http://qs1969.pair.com?node_id=1060050


in reply to Re^4: Explain a regexp matched group result
in thread Explain a regexp matched group result

I think your conclusion is correct in general, but if you know the structure of the RE then there are workarounds to the way the capture groups work. Consider:

#!C:/strawberry/perl/bin/perl.exe # use strict; use warnings; my $string = "aacbbbcac"; my $re1 = qr/((a+)?(b+)?(c))*/; #my $re1 = qr/((a*)(b*)(c))*/; #my $re1 = qr/((a+)?(b*)(c))*/; if ($string =~ $re1) { my $start = 0; my @something; foreach (0..$#-) { if(defined($-[$_])) { $start = $-[$_] if($-[$_] > $start); if($-[$_] >= $start) { printf "Group %d: <%s>\n", $_, substr($string, $-[$_], $+[ +$_] - $-[$_]); $something[$_] = substr($string, $-[$_], $+[$_] - $-[$_]); } else { printf "Group %d: <%s> - but ignore it because it is from +a previous iteration of the outer capture group\n", $_, substr($strin +g, $-[$_], $+[$_] - $-[$_]); $something[$_] = ''; } } else { printf "Group %d: hasn't matched yet\n", $_; $something[$_] = ''; } } print "$1 = " . join('', @something[2..4]) . "\n"; }

Which produces

Group 0: <aacbbbcac> Group 1: <ac> Group 2: <a> Group 3: <bbb> - but ignore it because it is from a previous iteration + of the outer capture group Group 4: <c> ac = ac

If you are trying to write something that handles arbitrary REs, this approach is unlikely to work.