Beefy Boxes and Bandwidth Generously Provided by pair Networks Frank
Problems? Is your data what you think it is?
 
PerlMonks  

Re^2: brackets matching problem

by lucky8 (Initiate)
on Aug 19, 2004 at 04:45 UTC ( [id://384212]=note: print w/replies, xml ) Need Help??

This is an archived low-energy page for bots and other anonmyous visitors. Please sign up if you are a human and want to interact.


in reply to Re: brackets matching problem
in thread brackets matching problem

Does this mean that leftmost-longest rule is not true for perl?

Replies are listed 'Best First'.
Re^3: brackets matching problem
by Dietz (Curate) on Aug 19, 2004 at 05:11 UTC
    Out of perldoc perlre:
    Alternatives are tried from left to right, so the first alternative found for which the entire expression matches, is the one that is chosen. This means that alternatives are not necessarily greedy. For example: when matching "foo|foot" against "barefoot", only the "foo" part will match, as that is the first alternative tried, and it successfully matches the target string. (This might not seem important, but it is important when you are capturing matched text using parentheses.)

    Let's look at a simply modified test sample:
    /(1|12)(3?)/
    will match 1 because 3 ist optional

    and
    /(1|12)(3?)$/ # use anchor to force the engine to match the entire ex +pression
    will match 12 because it has to satisfy the entire expression and so it will match 3 at the end of the string

    Add:
    For clarifying, the first example will have '1' in $1 and nothing in $2
    Second example will have '12' in $1 and '3' in $2
      Thank you, now it's clear
Re^3: brackets matching problem
by hanenkamp (Pilgrim) on Aug 19, 2004 at 07:56 UTC
    Greedy matching applies to the closures, not to alternatives. Besides, in this case, whether 12 matches or 1 then 2, it's the same length, is it not? From perlre, "alternatives are not necessarily greedy." You can make your matches greedy by arranging them how you like.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://384212]
help
Sections?
Information?
Find Nodes?
Leftovers?
    Notices?
    hippoepoptai's answer Re: how do I set a cookie and redirect was blessed by hippo!
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.