|
Posted by mosesdinakaran@gmail.com on 01/22/08 14:41
Hi Thanks for the reply,
Can you please tell me what I have under stood is right?
After the start of capture no.2 the pattern starts with the word
r is matched with t (the red king) -> NO Match
r is matched with h -> No Match
r is matched with e -> No Match
..
..
..
r is matched with r -> Match Found
Now the engine moves to the Next character in the pattern ie e
re is matched with re -> Match Found
red is matched with red -> Match Found
Since we have a | it comes out from the #first capture and now we
have an empty
space
red(space) is matched with red(space)
red(space)k is matched with red(space)k
..
..
red(space)king is matched with red(space)king -> Match Found
Now the value is returned and we get the first match
array
(
[0] => the red king
)
Is the above mentioned procedure is correct?
Also I was not able to find out how the other three values
Array
(
[1] => red king
[2] => red
[3] => king
)
are matched.
Thanks in Advance
Moses
On Jan 22, 5:09 pm, "Rik Wasmus" <luiheidsgoe...@hotmail.com> wrote:
> On Tue, 22 Jan 2008 12:41:26 +0100, mosesdinaka...@gmail.com
>
> <mosesdinaka...@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
[Back to original message]
|