Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Is greediness irrelevant when matching whole lines?

by loris (Hermit)
on Aug 27, 2007 at 07:45 UTC ( #635230=perlquestion: print w/ replies, xml ) Need Help??
loris has asked for the wisdom of the Perl Monks concerning the following question:

Dear all,

Am I right in thinking that, if I am matching whole lines, rather than parts of lines, greediness or not relevant when constructing the regular expression?

Thanks,

loris


"It took Loris ten minutes to eat a satsuma . . . twenty minutes to get from one end of his branch to the other . . . and an hour to scratch his bottom. But Slow Loris didn't care. He had a secret . . ." (from "Slow Loris" by Alexis Deacon)

Comment on Is greediness irrelevant when matching whole lines?
Re: Is greediness irrelevant when matching whole lines?
by Corion (Pope) on Aug 27, 2007 at 07:57 UTC

    For a regular expression to match at all, it does not matter whether the regex engine or the regex subexpressions are greedy or nongreedy. Both will give up or match more if that's neccessary to make the whole match succeed. If you care about how the regular expression matches, greedyness becomes an issue:

    for (<DATA>) { print "Greedy: $1/$2\n"; if /^(a+)(a+)$/; print "Non-greedy: $1/$2\n"; if /^(a+?)(a+)$/; };
Re: Is greediness irrelevant when matching whole lines?
by bart (Canon) on Aug 27, 2007 at 08:46 UTC
    Correct. Greediness determines how it matches, not whether it matches. Greedy and non-greedy regexes will either both match, or neither.

    As you allow only one way to match, due to anchors etc., greediness can barely play any role — apart from how subpatterns match. Do you even care about subpatterns?

Re: Is greediness irrelevant when matching whole lines?
by FunkyMonk (Canon) on Aug 27, 2007 at 08:46 UTC
    dominus wrote an excellent article on how the RE engine works, although it may not be current, I'll think you'll find it interesting.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://635230]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (7)
As of 2014-11-29 09:53 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My preferred Perl binaries come from:














    Results (204 votes), past polls