Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

brackets matching problem

by lucky8 (Initiate)
on Aug 19, 2004 at 08:03 UTC ( [id://384205]=perlquestion: print w/replies, xml ) Need Help??

lucky8 has asked for the wisdom of the Perl Monks concerning the following question:

Can anyone explain the following behaviour? When I run the script below the results of bracket matches are different: "1 2" and "12"
$_ = '123'; if(/(1|12)(2?)/) { print "$1 $2\n"; } if(/(12|1)(2?)/) { print "$1 $2\n"; }

Replies are listed 'Best First'.
Re: brackets matching problem
by tachyon (Chancellor) on Aug 19, 2004 at 08:23 UTC

    What do you find odd?. With alternation | Perl will match the first element if it can, if not the next ... so in the first case it finds a 1 which satisfies the first capture and then the second capture takes the optional 2. In the second case you get 12 as the first capture and then the second captuere 2? 'matches' the 3 as 3 is 0 or more 2s..... (actually it matches the null string between 2 and 3 to be technically correct.

    cheers

    tachyon

      Does this mean that leftmost-longest rule is not true for perl?
        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
        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.
Re: brackets matching problem
by PodMaster (Abbot) on Aug 19, 2004 at 08:22 UTC
    Try
    $_ = '123'; if(/(1|12)(2?)/) { print "1=[$1] 2=[$2]\n"; } if(/(12|1)(2?)/) { print "1=[$1] 2=[$2]\n"; }
    and compare (left to right).

    MJD says "you can't just make shit up and expect the computer to know what you mean, retardo!"
    I run a Win32 PPM repository for perl 5.6.x and 5.8.x -- I take requests (README).
    ** The third rule of perl club is a statement of fact: pod is sexy.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://384205]
Approved by Happy-the-monk
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (3)
As of 2025-02-14 08:22 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found