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

Re: Greedy regexp matching: differences between man page and implementation?

by Corion (Patriarch)
on Jul 08, 2004 at 13:27 UTC ( #372788=note: print w/replies, xml ) Need Help??


in reply to Greedy regexp matching: differences between man page and implementation?

Both, the documentation and the implementation are correct, because any regex match favours the leftmost interpretation. If you reorder your alternatives in your capturing first parentheses, I expect you will see the other match ab c, but as the single-letter a is already enough to make the first parentheses match once, the alternation won't be tried at all, as the overall match can succeed that way.

With Perls regular expression engine, you will always get the longest leftmost match, with "leftmost" taking precedence over "longest", which is what is biting you in the first capture.

  • Comment on Re: Greedy regexp matching: differences between man page and implementation?

Replies are listed 'Best First'.
Re^2: Greedy regexp matching: differences between man page and implementation?
by perldeveloper (Scribe) on Jul 08, 2004 at 13:47 UTC
    This is an even more obvious:
    $_='abcdefgh'; /^(a|abcdefg)(h|bc)/; print "[$1] [$2]\n";
    It prints [a] [bc]. The regexp engine clearly doesn't run through all possible combinations within (..|..|..) patterns.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (1)
As of 2023-06-01 03:12 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?