Skip to content
mck89 edited this page Mar 17, 2015 · 7 revisions

Regex class

The class REBuilder\Pattern\Regex represents the entire regex, everything inside the regex is a child of this class. You can create an empty Regex object using the create method on the base REBuilder class:

$regex = REBuilder\Rebuilder::create("mi", "#");

The optional arguments of this function are, in order, the modifiers and the delimiter of the regex.

Modifiers

You can assign modifiers to the regex by passing them to the create method of REBuilder class or using the setModifiers method on the Regex object and you can get them using the getModifiers method on the Regex object.

In the example above passing the "mi" as first argument will create a multiline case-insensitive regex.

Only valid modifiers are accepted as described in PHP documentation, if invalid modifiers are supplied an exception will be thrown.

Delimiters

You can specify regex delimiter by passing it as second argument of the create method of REBuilder class or using the setDelimiter method on the Regex object and you can get it using the getStartDelimiter and getEndDelimiter methods on the Regex object.

These methods return the same delimiter, except if you use bracket style delimiters.

Only valid delimiters are accepted as described in PHP documentation, if an invalid delimiter is supplied an exception will be thrown.

Use the regex

When the regex is complete you can get it as a string using the render method or casting it to string. You can also execute it directly using these methods:

  • test: tests if the given string is matched by the regex
  • exec: executes the regex on the given string and returns the matched patterns
  • split: splits the given string using the regex
  • replace: replaces the matches of the regex with the supplied replacement or function on the given string
  • greps: filters the given array using the regex

For example:

$regex = REBuilder\Rebuilder::create();
$regex->addChar("a");
echo $regex->replace("b", "abc"); //bbc
Clone this wiki locally