First, do not put parens '()' around things that you have no interest in using later. "Capturing" these things consumes time and resources and to no effect.
Most of the cost is paid by the first parenthesis, that is, there's a significant cost difference between not using capturing parens at all, and using capturing parens. Additional parens don't contribute that much.
In general, I avoid using $1, $5 etc. Use Perl list slice instead.
Careful here. Using a list slice (which I find quite ugly), or assigning the list to puts the match in list context, which will change the behaviour if /g is present.

But more importantly, in certain cases, when using list slices, you do not know whether there was a match or not:

my $a = rand() < .5 ? "f" : "g" my $b = rand() < .5 ? "p" : "o"; my $c = ("foo" =~ /($a)*$b/)[0];
Did it match, or didn't it? If $c is defined, it matched. But what if $c isn't? If $a eq "g", and $b eq "o", there is a match, but $c is undefined.

In reply to Re^2: Question on Regex grouping by JavaFan
in thread Question on Regex grouping by ajguitarmaniac

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.