PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.lang.php > Regular Expression: How the rule is applied for the Pattern'/((red|white) (king|queen))/'
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
Regular Expression: How the rule is applied for the Pattern'/((red|white) (king|queen))/'

Réponse
 
LinkBack Outils de la discussion
Vieux 22/01/2008, 11h41   #1
mosesdinakaran@gmail.com
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Regular Expression: How the rule is applied for the Pattern'/((red|white) (king|queen))/'


Can any one explain how the rule is applied for the following Regular
expression

$Str = 'the red king';

$Pattern = '/((red|white) (king|queen))/';

preg_match($Pattern,$Str,$Val);

Result:

Array
(
[0] => red king
[1] => red king
[2] => red
[3] => king
)

Thanks in Advance
Moses
  Réponse avec citation
Vieux 22/01/2008, 12h09   #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
Vieux 22/01/2008, 14h41   #3
mosesdinakaran@gmail.com
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))/'

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












  Réponse avec citation
Vieux 22/01/2008, 15h03   #4
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 15:41:34 +0100, mosesdinakaran@gmail.com
<mosesdinakaran@gmail.com> wrote:
> Hi Thanks for the reply,


Thank me by not top-posting .

> 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?


Not exactly how it happens 'under water', but as far as the outcome, yes,
that's correct. I take it the internals of the regex engine are not the
concern, but the use of regular expressions themselves?

> Also I was not able to find out how the other three values
>
> Array
> (
> [1] => red king
> [2] => red
> [3] => king
> )


Euhm, I even numbered the captures for you in my previous post... Suffice
to see every subpattern between () gets its own entry in the match array,
unless you specifiy an uncaptured subpattern (?:
--
Rik Wasmus
  Réponse avec citation
Vieux 22/01/2008, 18h45   #5
AnrDaemon
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))/'

Greetings, mosesdinakaran@gmail.com.
In reply to Your message dated Tuesday, January 22, 2008, 17:41:34,

> Hi Thanks for the reply,


Hi and no thanks for the top-posting.

>> > 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)/';


> 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


Actually, the pattern is started to match "the red king" with "(red|white)"
(mean, two matches, one with "red" and one with "white") and advance by
character till string will be matched, "the red king" -> "he red king" ->
"e red king" -> "<space>red king" -> "red king" -> Match (red) found!
As far as we have this match in brackets, we saving the (red) as #1 match with
intention to return it as 1st substring.

> 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)


Nop, as far as we have captured (red) we start the mew roundtrip.
"<space>king" will be matched against space in the pattern -> it matches, so
we saving it as #2 match (no return) and start third trip.

> red(space)k is matched with red(space)k
> .
> .
> red(space)king is matched with red(space)king -> Match Found


"king" will be matched against "(king|queen)" and it matches.
Let's save it as #3 match and remember to return it as 2nd subpattern
There is no more atoms in pattern, so let's look to see what we need else.
Oh, we have an outer brackets contains both subpatterns.
So we storing #1+#2+#3 as [1] and shifting numbers of #1 and #3.

> Now the value is returned and we get the first match


> array
> (
> [0] => the red king
> )


Yup, the [0] subpattern is the whole string where the match occured.

> 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.


As described.

Array
(
[0] => 'the red king' // Whole string where match occured
[1] => 'red king' // The 1st subpattern ((red)<space>(king))
[2] => 'red' // The 2nd subpattern (red)
[3] => 'king' // The 3rd subpattern (king)
)


--
Sincerely Yours, AnrDaemon <anrdaemon@freemail.ru>

  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 03h41.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,16676 seconds with 13 queries