On Monday 26 June 2006 01:48 pm, Tom Phoenix wrote:
> On 6/26/06, tom arnall <[EMAIL PROTECTED]> wrote:
> > > >      3  $_ = " [11]  [22] a ";
> > > >
> > > >      6  $re1 = qr/a|\[.*?\d\d\]/;
> > > >      7  $re2 = qr/($re1\s)?$re1/;
> > > >      8  ($f) = /($re2)/;
> >
> > if line 6 is changed to '$re1 = qr/a|\[\d\d\]/', the result is '[11]'.
> > how is '.*?' causing the different output?
>
> When '.*?' is included, the first occurrence of $re1 matches '[11]
> [22]'. (Note the two spaces.) Next the single space before 'a' is
> matched; the second occurrence of $re1 matches the 'a'.
>
> Without '.*?', the optional clause matches the empty string; so the
> second occurrence of $re1 matches '[11]'.
>
> Remember, it matches at the first place in the string where it *can*
> match. Once the RE engine finds a match, it doesn't keep looking for
> another.
>
> Does that clear things up for you? Cheers!
>
> --Tom Phoenix
> Stonehenge Perl Training



#!/usr/bin/perl -w

$bin = "26. -- The White Knight obstructs its own Rook which otherwise would 
be able to strike a death-blow by R(K1)-K7 . It is desperado and makes the 
furious attack 1 X-Q5 . Black replies 1 ... PxX . Now 2 [!R(K1)-K76404] , 
would have won , but ( 2 [!RxP6762]ch. , KxR ; 3 Q-R5ch. , K-X1 ; 4 R-K7 ) is 
still more conclusive .
THE END
";

$pre = qr/[PRXBKQ]/io;

$mtre = qr/
(
$pre{1,3}
[\-x]
$pre{1,3}
[1-8]?
|
\[\!.*?\d\d\d\d\]
)
(ch\.|e\.p\.)?
/xo;

$chre = qr/
((\(\s*)?
\d{1,2}
\s
(
($mtre
|
\.{3,3})
\s
,?
\s?
)?
$mtre)
|
\s\)\s
|THE\sEND
/xo;

while (1) {
        ($f) = $bin =~ /($chre)/;
        $bin = $';
        print "match: $f
"       ;
        last if $f =~ /THE END/;


}


==========================
 
 
The foregoing is the 'real' code that lies behind my previous questions. ;.) i 
would have trotted it out from the beginning except that i like to reduce the 
expression of  a problem to its simplest terms before putting it on the list.

at any rate, the output of the foregoing code is:

        match: 1 X-Q5
        match: 1 ... PxX
        match: 2 [!R(K1)-K76404] , would have won , but ( 2 [!RxP6762]ch. , KxR
        match: 3 Q-R5ch. , K-X1
        match: 4 R-K7
        match:  )
        match: THE END

How do i get the output to be:

        match: 1 X-Q5
        match: 1 ... PxX
        match: 2 [!R(K1)-K76404]
        match: ( 2 [!RxP6762]ch. , KxR
        match: 3 Q-R5ch. , K-X1
        match: 4 R-K7
        match:  )
        match: THE END

i.e., get it to separate the two units with square-bracketed content?

thanks in advance,

tom arnall







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