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 = 'the red king';
>
> $Pattern = '/((red|white) (king|queen))/';
$Pattern = '/( # 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] => red king
> [1] => red king
> [2] => red
> [3] => 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 = '/(?:red|white) (?:king|queen)/';
--
Rik Wasmus