Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Re^3: When greedy constructs do battle, can I choose the winner?

by duff (Parson)
on Sep 20, 2007 at 18:45 UTC ( #640210=note: print w/replies, xml ) Need Help??


in reply to Re^2: When greedy constructs do battle, can I choose the winner?
in thread When greedy constructs do battle, can I choose the winner?

For a number such as 243210890000, the non greedy {1,5} grabs the least it can, the 9, correct?

Incorrect. The string is processed left to right, so the non-greedy {1,5} will match one character then try to match zeroes to the end of the string. Since this fails the overall match, the RE engine will backtrack and try two characters, then three and four and five. When the five character version fails, backtracking will cause the starting point of the {1,5} match to move over one character and try again. This continues until either there's a match or the RE engine determines that it can't match.

  • Comment on Re^3: When greedy constructs do battle, can I choose the winner?

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (3)
As of 2021-10-24 00:59 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My first memorable Perl project was:







    Results (88 votes). Check out past polls.

    Notices?