in reply to Behavior of /g when there are capture groups

There're only two ways I know to avoid "spurious" capture groups in Perl 5.8.x regexes: don't use capture groups at all, or grep them away. Here's another approach that might or might not fit with your "... expressions [that] are much more complicated ..." in the pairs of regexes (update: tested under ActiveState 5.8.9):

c:\@Work\Perl>perl -wMstrict -MData::Dump -le "print qq{Perl version: $] \n}; ;; my $fc = 'abcdfoofrobnicatebardefforspambazghixyproblemaaaxyzzylettuce'; ;; my $min = qr{ .*? }xms; ;; my @groups = ( [ qr{ fo. }xms, $min, qr{ ba. }xms, ], [ qr{ xy. }xms, $min, qr{ le. }xms, ], ); ;; my ($find) = map qr{ $_ }xms, join q{ | }, map qr{ @$_ }xms, @groups ; print qq{$find \n}; ;; my ($capture) = map qr{ $_ }xms, join q{ | }, map { map qr{ $_ }xms, join ' ', map qr{ ($_) }xms, @$_ } @groups ; print qq{$capture \n}; ;; my @excerpts = $fc =~ m{ $find }xmsg; dd \@excerpts; ;; for my $exrpt (@excerpts) { my @parts = grep defined, $exrpt =~ m{ $capture }xmsg; printf qq{'$_' } for @parts; print ''; } " Perl version: 5.008009 (?msx-i: (?msx-i: (?msx-i: fo. ) (?msx-i: .*? ) (?msx-i: ba. ) ) | (?m +sx-i: (?msx-i: xy. ) (?msx-i: .*? ) (?msx-i: le. ) ) ) (?msx-i: (?msx-i: (?msx-i: ((?msx-i: fo. )) ) (?msx-i: ((?msx-i: .*? ) +) ) (?msx-i: ((?msx-i: ba. )) ) ) | (?msx-i: (?msx-i: ((?msx-i: xy. ) +) ) (?msx-i: ((?msx-i: .*? )) ) (?msx-i: ((?msx-i: le. )) ) ) ) ["foofrobnicatebar", "forspambaz", "xyproblem", "xyzzylet"] 'foo' 'frobnicate' 'bar' 'for' 'spam' 'baz' 'xyp' 'rob' 'lem' 'xyz' 'zy' 'let'
(Please forgive the wrap-around in the long regex print statements. I've tried to mitigate the effects; I hope I didn't introduce any | too much cruft.)

Update: Of course, this is an attempt to generalize tye's approach above to deal with unlimited pairs of regexes. Whether it applies to your situation is a separate question.


Give a man a fish:  <%-{-{-{-<