Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number

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

by GrandFather (Saint)
on Aug 01, 2006 at 20:44 UTC ( #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:


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?

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

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (3)
As of 2022-08-12 06:46 GMT
Find Nodes?
    Voting Booth?

    No recent polls found