Tom Arnall 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]
>
> The difference between the two sections of code is in Lines 6/12, i.e., 
> the
> presence/absence of '.*?'. The regex in Line 6 seems to be using the
> second ']' in the target string to satisfy its '\]'. But shouldn't the '?'
> in '.*?' cause the search to terminate at the first ']' and yield the same
> result as the expression in Line 12?
>
> also, why should removal of the 'a|' in $re1 make any difference in the
> behavior of the expression? in fact, the removal causes the regex to
> return '[11]', i.e., allows the '?' quantifier to work.

Hi Tom

Your problem is that there are two spaces between [11] and [22] yet you have
coded only a single space with ($re1\s). In the first case the first $re1 
matches
the string '[11]  [22] ' as the .*? is forced to stretch to the single space
after [22] to let the second $re1 match just the trailing 'a'. In the second 
case
the first $re1 matches nothing, as it is optional and the earliest match for 
the
entire regex is to leave it out and match '[11]' with the second $re1. 
Change
lines 7 and 13 to:

  $re2 = qr/($re1\s+)?($re1)/;

and both cases give the same result:

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

You have given yourself a complicated task approaching the problem like 
this.
What are you trying to do and what does the real data look like? It looks
from this like a case for split().

HTH,

Rob





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