note
ig
<p>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:</p>
<c>
#!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($string, $-[$_], $+[$_] - $-[$_]);
$something[$_] = '';
}
} else {
printf "Group %d: hasn't matched yet\n", $_;
$something[$_] = '';
}
}
print "$1 = " . join('', @something[2..4]) . "\n";
}
</c>
<p>Which produces</p>
<c>
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
</c>
<p>If you are trying to write something that handles arbitrary REs, this approach is unlikely to work.</p>
1060027
1060046