On 6/26/06, tom arnall <[EMAIL PROTECTED]> wrote:

> >      1  #!/usr/bin/perl -w
> >      2
> >      3  $_ = " [11]  [22] a ";
> >      4
> >      5  #with .*?
> >      6  $re1 = qr/a|\[.*?\d\d\]/;
> >      7  $re2 = qr/($re1\s)?$re1/;
> >      8  ($f) = /($re2)/;
> >      9  print "with .*? : $f\n";
> >     10
> >     11  #without .*?
> >     12  $re1 = qr/a|\[\d\d\]/;
> >     13  $re2 = qr/($re1\s)?$re1/;
> >     14  ($f) = /($re2)/;
> >     15  print "without .*? : $f\n";
> >
> > gets this result:
> >
> >         with .*? : [11]  [22] a
> >         without .*? : [11]

but it gobbles to the 2nd ']' even if there is only one space between the ']'
and '['.

When you have two spaces, the optional clause in $re2 has to continue
until the space before the 'a' in order to make a match. It can't stop
after '[11]'.

When there's only one space between the brackets, the optional clause
can succeed after "[11] ", allowing "[22]" to match:

 with .*? : [11] [22]
 without .*? : [11] [22]

Perhaps it's confusing because sometimes you've got two matches of
$re1 and sometimes just one? From your earlier code, when you had two
spaces:

> >         with .*? : [11]  [22] a

First $re1 matched '[11]  [22]', second matched 'a'.

> >         without .*? : [11]

First $re1 matched empty string, second matched '[11]'.

Is it getting any clearer? Cheers!

--Tom Phoenix
Stonehenge Perl Training

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