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:  <%-{-{-{-<


In reply to Re: Behavior of /g when there are capture groups by AnomalousMonk
in thread Behavior of /g when there are capture groups by ExReg

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.