Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

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

by GrandFather (Saint)
on Aug 01, 2006 at 20:44 UTC ( [id://565078]=note: print w/replies, xml ) Need Help??


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

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

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (3)
As of 2024-04-25 09:23 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found