http://qs1969.pair.com?node_id=565078


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