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

Strange result from "abbbbbc" =~ /(b)*?(b*?)c/

by mcovic (Initiate)
on Aug 01, 2006 at 20:25 UTC ( [id://565067]=perlquestion: print w/replies, xml ) Need Help??

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

Hi, When I do "abbbbbc" =~ /(b)*?(b*?)c/ as a result I get $1="a" $2="bbbbb" which is not what I have expected. How did (b) manage to match "a"? Is this a bug or a documented behavior of the regular expressions? p.s. "abbbbbc" =~ /(b)*(b*?)c/ nicely returns $1="b" $2=""

Replies are listed 'Best First'.
Re: Strange result from "abbbbbc" =~ /(b)*?(b*?)c/
by rodion (Chaplain) on Aug 01, 2006 at 20:56 UTC
    To explain further how your regular expression is problematic, in case you find it helpful:

    What you're saying with "/(b)*?/" is that you want to match "b" and store it in $1, and do this zero or more times, but no more times than necessary. As ikegami notes, what you may mean is "/(b*?)/, which says that you want to match "b" zero or more times, but no more than necessary, and store the collection of characters matched in $1.

    The difference is that the first might be trying to store any number of different single instances of "b" in $1, all of which are matched, it may even store no instances of "b" in $1. It may have trouble deciding which single instance to put in $1. It looks like back in 5.6.1 the regular expression engine gets really confused by this, as well it might.

    In 5.8.x versions it looks like it decides to take the first instance, which is "no" occurances of "b".

    Adendum for those who know the RE engine: I know this is not how the RE engine works on the string, it's meant as an explanation of why the expression is ambiguous, rather than how the RE engine works (which the general reader of an RE shouldn't have to know, IMO). The 5.6.1 engine has a bug here, but it is a lot to ask of the little engine that could.

      Thank you all for you help. It just confirmed my suspicion that this was indeed a bug. The Version I have is 5.8.0. As for those who tried to explain why this regular expression is problematic, this regular expression is fine and was specifically designed to test this very specific thing. And there is a nice use of (X)* (where X is any other expression) because it gives you the last occurence of pattern X (at least in my version of perl).

        It's not evident that that is a "nice use if (X)*" given that different versions of Perl handle the rather bogus expression differently. The same result can be achieved using zero width assertions:

        "babbbbbcbbbcx" =~ /(?:b)(?!.*b)(..)/; print ">$1<\n"; # >cx<

        Update s/\Q(?!(?=.*b))\E/(?!.*b)/. Thanks to ikegami for pointing out the redundancy.


        DWIM is Perl's answer to Gödel
        How much different it would have been to have this background information in the original post, where it would have helped us figure out how to be of help, instead of puting our work into gueses of what was needed.

        With this statement showing up afterward like this, with the "yes, yes just as I thought" tone, I'm feeling less like I've been of help, and more like I've been mislead for someone's amusement.

        Oh well, win some, lose some.

Re: Strange result from "abbbbbc" =~ /(b)*?(b*?)c/
by GrandFather (Saint) on Aug 01, 2006 at 20:44 UTC

    If indeed that is what you get then there is a bug (what version of Perl?). If I run:

    "abbbbbc" =~ /(b)*?(b*?)c/; print "($1)($2)\n";

    it prints:

    ()(bbbbb)

    as expected. However the following may provide a little illumination:

    "abbbbbc" =~ /(b)*?(b*?)c/; print "\$1: $1\n" if defined $1; print "\$2: $2\n" if defined $2; Prints: $2: bbbbb

    Note no $1 printed - it's undefined because the minimum number of times it could match is 0. Perhaps you need to tell us something about the bigger picture. It looks like you are asking for the answer to the wrong question.

    Update: "as expected" because (b*?) doesn't backtrack. It matches the first b found and the minimum number more that it can and still make a match. Consider:

    "babbbbbcbbbcx" =~ /(b*?)/; print ">$1<\n"; # '><' - matched none "babbbbbcbbbcx" =~ /(b*?)c/; print ">$1<\n"; # '>bbbbb<' - matched b following a through b preceedi +ng c

    DWIM is Perl's answer to Gödel
Re: Strange result from "abbbbbc" =~ /(b)*?(b*?)c/
by japhy (Canon) on Aug 01, 2006 at 20:37 UTC
    What version of Perl gave you such things? I get $1 = undef, $2 = "bbbb".

    Jeff japhy Pinyan, P.L., P.M., P.O.D, X.S.: Perl, regex, and perl hacker
    How can we ever be the sold short or the cheated, we who for every service have long ago been overpaid? ~~ Meister Eckhart

      I can reproduce his results with ActivePerl 5.6.1

      $1 = 'a' $2 = 'bbbbb' @- = (1, 0, 1) @+ = (7, 1, 6)
        Perl 5.8.8 doesn't have this problem. Whatever it is, it appears to have been fixed. 5.6.1 is kind of "old".

        Jeff japhy Pinyan, P.L., P.M., P.O.D, X.S.: Perl, regex, and perl hacker
        How can we ever be the sold short or the cheated, we who for every service have long ago been overpaid? ~~ Meister Eckhart
Re: Strange result from "abbbbbc" =~ /(b)*?(b*?)c/
by ikegami (Patriarch) on Aug 01, 2006 at 20:36 UTC

    (...)?, (...)*, etc don't really make any sense.
    ((?:...)?), ((?:...)*), etc would properly capture.

    Did you mean (b*?)?

    if ("abbbbbc" =~ /(b*?)(b*?)c/) { print("\$1 eq '$1'\n"); # $1 eq '' print("\$2 eq '$2'\n"); # $2 eq 'bbbbb' } else { print("No match\n"); }

    I'd file this under GIGO, but Perl has been changed to return a more sensible result in newer versions.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others taking refuge in the Monastery: (6)
As of 2024-04-18 17:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found