Re: Regex to matching Parenthesis



On 9/7/07, John W. Krahn <krahnj@xxxxxxxxx> wrote:
Aruna Goke wrote:

print $&, if $f=~m/\S+$/;

perldoc perlre
[ snip ]
WARNING: Once Perl sees that you need one of $&, $`, or $' anywhere in the
program, it has to provide them for every pattern match. This may
^^^^^^^^
substantially slow your program. Perl uses the same mechanism to produce
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
$1, $2, etc, so you also pay a price for each pattern that contains
capturing parentheses. (To avoid this cost while retaining the grouping
behaviour, use the extended regular expression "(?: ... )" instead.) But
if you never use $&, $` or $', then patterns without capturing parentheses
will not be penalized. So avoid $&, $`, and $' if you can, but if you
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
can't (and some algorithms really appreciate them), once you've used them
once, use them at will, because you've already paid the price. As of
5.005, $& is not so costly as the other two.


Why is there a comma after the variable? Did you forget to include another
variable?



I'm actually a little confused about this. I never really thought
about it, but the doc seems to indicate that $1 is a s bad as $& ("the
same mechanism"). But then it implies that capturing parentheses only
affect particular patterns ("pay a price for each pattern...").

So does capturing parentheses anywhere affect the every pattern, like
$&, or just patterns that capture?

Best,

-- jay
--------------------------------------------------
This email and attachment(s): [ ] blogable; [ x ] ask first; [ ]
private and confidential

daggerquill [at] gmail [dot] com
http://www.tuaw.com http://www.downloadsquad.com http://www.engatiki.org

values of β will give rise to dom!


Relevant Pages

  • FAQ 6.23 How do I match a regular expression thats in a variable?
    ... How do I match a regular expression that's in a variable? ... We don't have to hard-code patterns into the match operator (or anything ... have the pattern in $regex, you use that variable in the match operator. ... and the pattern still has to be valid or Perl will complain. ...
    (comp.lang.perl.misc)
  • FAQ 6.23 How do I match a regular expression thats in a variable?
    ... How do I match a regular expression that's in a variable? ... We don't have to hard-code patterns into the match operator (or anything ... have the pattern in $regex, you use that variable in the match operator. ... and the pattern still has to be valid or Perl will complain. ...
    (comp.lang.perl.misc)
  • FAQ 6.23 How do I match a regular expression thats in a variable?
    ... How do I match a regular expression that's in a variable? ... We don't have to hard-code patterns into the match operator (or anything ... have the pattern in $regex, you use that variable in the match operator. ... and the pattern still has to be valid or Perl will complain. ...
    (comp.lang.perl.misc)
  • FAQ 6.23 How do I match a regular expression thats in a variable?
    ... How do I match a regular expression that's in a variable? ... We don't have to hard-code patterns into the match operator (or anything ... have the pattern in $regex, you use that variable in the match operator. ... and the pattern still has to be valid or Perl will complain. ...
    (comp.lang.perl.misc)
  • FAQ 6.23 How do I match a regular expression thats in a variable?
    ... How do I match a regular expression that's in a variable? ... We don't have to hard-code patterns into the match operator (or anything ... have the pattern in $regex, you use that variable in the match operator. ... and the pattern still has to be valid or Perl will complain. ...
    (comp.lang.perl.misc)