Pine Yan wrote:
A script like this:

        line1:  $string3 = "bacdeabcdefghijklabcdeabcdefghijkl";
        line2:  $string4 = "xxyyzzbatttvv";

        line3:  print "\$1 = $1 [EMAIL PROTECTED],$+[0]}, \$& = $&\n" 
if($string3
=~ /(a|b)*/);
        line4:  print "\$1 = $1 [EMAIL PROTECTED],$+[0]}, \$& = $&\n" 
if($string4
=~ //);

Run and gett result:

        $1 = a @{0,2}, $& = ba
$1 = @{0,0}, $& =


line 3: you are matching the last single letter of the first match
[ ^ba ] as $1.
@+ and @- merely point to the starting points on that string $string3 being characters 1 and 3 (in array-speak it's 0,2) and from perldocs you can find what $& is all about.

If you wanted to match either 'ab' or 'ba' you would do it this way:
(ab|ba) to require two letters.

If I change the code of line3 to:

        print "\$1 = $1 [EMAIL PROTECTED],$+[0]}, \$& = $&\n" if($string3 =~
/(a|b)+/);

and keep everything else the same, I will get:

        $1 = a @{0,2}, $& = ba
        $1 = a @{6,8}, $& = ba

I think you are right in that this doesn't make sense right away.
It appears that the second expression is returning a match for the previous regex and not a regex of //.

So I don't think that the statement of $string =~ // is going to return the first element '', rather it returns the last regex that was applied.


RUN THIS
$string3="This is my favorite day.";
$string4="Some days are better than others.";

print $-[0],"\n" if $string4 =~ //;
print $-[0],"\n" if $string4 =~ /day/;
print $-[0],"\n" if $string3 =~ //;
print $-[0],"\n" if $string3 =~ /day/;

and you get
0
5
20
20

0 because there is no regex expression defined.
but the first '20' acts as if you were matching the last expression run (/day/).

Question:  Bug or Feature?

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to