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:
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.my $a = rand() < .5 ? "f" : "g" my $b = rand() < .5 ? "p" : "o"; my $c = ("foo" =~ /($a)*$b/)[0];
In reply to Re^2: Question on Regex grouping
by JavaFan
in thread Question on Regex grouping
by ajguitarmaniac
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |