-
Notifications
You must be signed in to change notification settings - Fork 0
Conditional subpatterns
mck89 edited this page Mar 18, 2015
·
2 revisions
Conditional subpatterns are a particular type of non capturing subpattern that implement an if-then-else syntax structure. In REBuilder conditional subpatterns are represented by the REBuilder\Pattern\ConditionalSubpattern
class.
The classes that represent the parts that compose the conditional subpattern are:
-
REBuilder\Pattern\Assertion
: the "if" part is an assertion that can be implemented as described in Assertions page and can be added usingaddAssertion
. -
REBuilder\Pattern\ConditionalThen
: the "then" part is a container that can be added usingaddConditionalThen
. -
REBuilder\Pattern\ConditionalElse
: the optional "else" part is a container that can be added usingaddConditionalElse
.
Example:
- Match "a"
- If followed by "c" match "c"
- Otherwise match "d"
$regex
->addCharAndContinue("a") //Add "a"
->addConditionalSubPattern() //Start the conditional subpattern
->addAssertion() //Add the "if" assertion
->addCharAndContinue("c") //Match "c"
->getParent() //Back to the conditional subpattern
->addConditionalThen() //Add the "then" part
->addCharAndContinue("c") //Match "c"
->getParent() //Back to the conditional subpattern
->addConditionalElse() //Add the "else" part
->addCharAndContinue("d");//Match "d"
echo $regex; // "/a(?(?=c)c|d)/"
If you don't want to use the "chain" syntax to create the regex you can also use some shortcut methods to add every part: setIf
, setThen
, setElse
. These methods accept an instance of the classes listed above. You can also use getters method getIf
, getThen
and getElse
to access every part of the conditional subpattern.