Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Re: Greediness of * vs. greediness of +

by moritz (Cardinal)
on Sep 08, 2010 at 09:29 UTC ( #859295=note: print w/ replies, xml ) Need Help??


in reply to Greediness of * vs. greediness of +

The overriding principle of the regex engine is to try the left-most possible match first.

In the case of b*, it matches zero times at the start of the string (ie before the first character) - which is as many as it can, because there are not more than 0 leading b's in your string.

In the case of b+, the empty match is not successful, so the regex engine bumps along, and tries to match as many b's as possible, starting from the second position (ie string index 1).

So both are greedy, but neither do a search for longest match of consecutive b's. try

$ perl -lwe '"abbabbbbc"=~/(b+)/ && print "Found: $1"' Found: bb

It used the first position where b+ matched, and there took as many b's as possible.

Perl 6 - links to (nearly) everything that is Perl 6.


Comment on Re: Greediness of * vs. greediness of +
Download Code

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (9)
As of 2014-10-23 00:52 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (123 votes), past polls