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

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

by andreas1234567 (Vicar)
on Sep 20, 2007 at 18:41 UTC ( [id://640209]=note: print w/replies, xml ) Need Help??


in reply to When greedy constructs do battle, can I choose the winner?

perlretut says:
Principle 3: If there are two or more elements in a regexp, the leftmost greedy quantifier, if any, will match as much of the string as possible while still allowing the whole regexp to match. The next leftmost greedy quantifier, if any, will try to match as much of the string remaining available to it as possible, while still allowing the whole regexp to match. And so on, until all the regexp elements are satisfied.
--
Andreas
  • Comment on Re: When greedy constructs do battle, can I choose the winner?

Replies are listed 'Best First'.
Re^2: When greedy constructs do battle, can I choose the winner?
by amarquis (Curate) on Sep 20, 2007 at 18:54 UTC

    What I had figured, after reading that in the docs, was "Welp, you can do just about anything in Perl just about any way you want to, I wonder if I can break that behavior somehow?"

    While we're on the subject, let me run my interpretation of what's going on "behind the scenes" with the engine by y'all, to see if I understand it correctly. Let's take the simple pattern:

    "444440000" =~ /(/d*)(0{3,10})/

    So, the first thing it tries to do is give the /d* as much as possible, so the /d* is trying to match the whole string. Then there aren't enough zeros to match the second part, so it gives up and backs up, trying to put one less character into (/d*). It continues to fail and back up one character less until it comes out with a match.

    So in the above example, it tries things out like the following?

    /d* 0{3,10} 444440000 nothing 44444000 0 4444400 00 444440 000 (Success!)

      Yep. That's it. (Aside from your slash leaning the wrong direction).

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (6)
As of 2024-04-19 11:36 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found