-
Notifications
You must be signed in to change notification settings - Fork 0
Alternations
Alternations create alternative for the current container.
REBuilder provides two classes to manage alternations: REBuilder\Pattern\AlternationGroup
and REBuilder\Pattern\Alternation
. These classes allow to group all the alternations in a common container (AlternationGroup) that accepts only alternations as children:
To create alternations you must first create an alternation group using addAlternationGroup
and then you can add multiple alternations to it using addAlternation
:
$regex
->addAlternationGroup() //Create the alternation group
->addAlternation() //Add the first alternation
->addChar("a") //Match the "a" character
->getParent() //Back to assertion
->getParent() //Back to assertion group
->addAlternation() //Add the second alternation
->addChar("b"); //Match the "b" character;
echo $regex; // "/(?:a|b)/"
Note that AlternationGroup
can contain only Alternation
class instances so if you use addChild
and pass a different pattern it will throw an exception. Note also that Alternation
can be added only to AlternationGroup
, adding it to another container or using add addAlternation
on containers different from AlternationGroup
will throw an exception.