Re: Regex to matching Parenthesis
- From: daggerquill@xxxxxxxxx (Jay Savage)
- Date: Thu, 13 Sep 2007 10:20:08 -0400
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!
- Follow-Ups:
- Re: Regex to matching Parenthesis
- From: Chas Owens
- Re: Regex to matching Parenthesis
- References:
- Regex to matching Parenthesis
- From: Chris E. Rempola
- Re: Regex to matching Parenthesis
- From: Aruna Goke
- Re: Regex to matching Parenthesis
- From: John W. Krahn
- Regex to matching Parenthesis
- Prev by Date: RE: adding data to a file before it gets regexed
- Next by Date: Re: adding data to a file before it gets regexed
- Previous by thread: Re: Regex to matching Parenthesis
- Next by thread: Re: Regex to matching Parenthesis
- Index(es):
Relevant Pages
|