Afficher un message
Vieux 22/01/2008, 13h09   #2
Rik Wasmus
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Regular Expression: How the rule is applied for the Pattern '/((red|white) (king|queen))/'

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
  Réponse avec citation
 
Page generated in 0,05238 seconds with 9 queries