|
Posted by Rik Wasmus on 01/22/08 12:09
On Tue, 22 Jan 2008 12:41:26 +0100, mosesdinakaran@gmail.com =
<mosesdinakaran@gmail.com> wrote:
>
> Can any one explain how the rule is applied for the following Regular
> expression
>
> $Str =3D 'the red king';
>
> $Pattern =3D '/((red|white) (king|queen))/';
$Pattern =3D '/( # start of capture no.1
( # start of capture no.2
red|white # either literal 'red' or literal 'white'
) # end of capture no.2
\s # space in originial, any whitespace for this one
( # start of capture no.3
king|queen # either literal 'king' or literal 'queen'
) # end of capture no.3
) # end of capture no.1
/x';
> Array
> (
> [0] =3D> red king
> [1] =3D> red king
> [2] =3D> red
> [3] =3D> king
> )
Capture (1) is useless, as it will have exactly the same contents as the=
=
whole match in (0), so lose the outside ( and ). If you don't need the =
'red' & 'king' in your match seperately, you can use (?: to create an =
uncaptured subpattern.
$Pattern =3D '/(?:red|white) (?:king|queen)/';
-- =
Rik Wasmus
Navigation:
[Reply to this message]
|