On Jun 26, 2006, at 11:42 AM, tom arnall wrote:

 On Monday 26 June 2006 10:49 am, Peter Cornelius wrote:
     ...
      x|a      #followed by an 'x' or an 'a'

aren't the alternates (1) everything to the left of '|' and (2) 'a'? thus - to
take another example - the following:

        $_='abcde'
        print /(abx|c)/

produces:

        c

not:

        abc


Yes, I think you're right.  I guess my internal parser hiccuped :-)


do you have any idea why:

        $_ = " x11x  x22x a ";

        $re1 = qr/x.*?\d\dx|a/;
        $re2 = qr/($re1\s)?$re1/;
        ($_) = /($re2)/;
        print $_;

doesn't produce 'x11x' ? (note btw that if you insert '\n' between the first two tokens of the target string, the result >does become 'x11x'. note also
that if you drop '|a' from $re1 you also get 'x11x'.)

i read this example as follows:

        $re1 = qr/              
        x                                       #find an 'x'
        .*?                             #find whatever of whatever length
        \d\d                            #find two digits
        x                                       #find an 'x'
        |                                       #or, instead of all the 
foregoing,
        a                                       #find an 'a'
        /x;
        
        $re2 = qr/
        (
        $re1                            #find $re1
        \s                              #and whitespace
        )?                              #or maybe none of the foregoing
        $re1                            #find for sure $re1
                                                #in sum, find $re1 possibly 
preceded by $re1+whitespace
        /x;

Check this out:

#!/usr/bin/perl

$_ = " x11x  x22x a ";

#with '.*?'
$re1 = qr/x.*?\d\dx|a/;
($j) = /($re1\s)?/;
($k) = /($re1\s){1}/;
print "j:$j:$/";
print "k:$k:$/";


Gives you:
j::
k:x11x :

I think this lines up with Tom's comments about the optional '?' operator making the first occurrence be the empty string. What's confusing me now is this next bit.

#!/usr/bin/perl

$_ = " x11x  x22x a ";

#with '.*?'
$re1 = qr/x.*?\d\dx|a/;
($g,$h) = /($re1\s)?($re1)/;
print "g:$g:$/";
print "h:$h:$/";

Gives you:
g:x11x  x22x :
h:a:

I guess I expected it to give me something like
g::
h:x11x :
where the first match was the empty string and the second was the pattern match.

Hrmmm,
Peter Cornelius
Sr. Applications Engineer
LiveOps http://www.liveops.com



--
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