Reply to Re: Regular Expression: How the rule is applied for the Pattern '/((red|white) (king|queen))/'

Your name:

Reply:


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

[Back to original message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация