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
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.
|