Skip to content
mck89 edited this page Mar 17, 2015 · 1 revision

Repetitions

There are five types of repetitions in the regex syntax, each one has a corresponding class that inherit from REBuilder\Pattern\Repetition\AbstractRepetition. To set the repetition for a pattern you must use the setRepetition method which can take an instance of a repetition class or you case use the alternative syntax for setRepetition which makes things easier and shorter.

Note that not all the pattern supports repetitions. If you want to test if a pattern supports repetition you can use the supportsRepetition method.

Asterisk

The asterisk repetition (*) means that the pattern is repeated 0 or more times. In REBuilder it is represented by the class REBuilder\Pattern\Repetition\ZeroOrMore:

$pattern->setRepetition(new REBuilder\Pattern\Repetition\ZeroOrMore());

//Or

$pattern->setRepetition("*");

Both the class constructor and the second argument of the alternative syntax for setRepetition takes a boolean that, if true, makes the repetition lazy

Plus

The plus repetition (+) means that the pattern is repeated 1 or more times. In REBuilder it is represented by the class REBuilder\Pattern\Repetition\OneOrMore:

$pattern->setRepetition(new REBuilder\Pattern\Repetition\OneOrMore());

//Or

$pattern->setRepetition("+");

Both the class constructor and the second argument of the alternative syntax for setRepetition takes a boolean that, if true, makes the repetition lazy

Question mark

The question mark repetition (?) means that the pattern is repeated 1 or zero times. In REBuilder it is represented by the class REBuilder\Pattern\Repetition\Optional:

$pattern->setRepetition(new REBuilder\Pattern\Repetition\Optional());

//Or

$pattern->setRepetition("?");

Number

The number repetition ({1}) means that the pattern is repeated exactly the specified number of times. In REBuilder it is represented by the class REBuilder\Pattern\Repetition\Number:

$pattern->setRepetition(new REBuilder\Pattern\Repetition\Number(1));

//Or

$pattern->setRepetition(1);

Range

The range repetition ({1,5}) specifies minimum and maximum number of occurencies of the pattern. In REBuilder it is represented by the class REBuilder\Pattern\Repetition\Range:

$pattern->setRepetition(new REBuilder\Pattern\Repetition\Range(1, 5));

//Or

$pattern->setRepetition(1, 5);

Note tha passing null as second argument of both class constructor and setReptetition removes the maximum limit for the repetition:

$pattern->setRepetition(new REBuilder\Pattern\Repetition\Range(5, null));

//Or

$pattern->setRepetition(5, null);  // {5,}
Clone this wiki locally