-
Notifications
You must be signed in to change notification settings - Fork 0
Introduction
REBuilder is a PHP library to build and parse regular expressions
REBuilder comes with a custom autoloader that manages project's classes. To enable it, simply include the base class file (lib/REBuilder/REBuilder.php) and register the autoloader:
require_once "lib\REBuilder\REBuilder.php";
REBuilder\Rebuilder::registerAutoloader();
To parse a regular expression you can use the parse
method on REBuilder base class:
$regex = REBuilder\Rebuilder::parse("/regex to parse/");
Now the $regex
variable contains a REBuilder\Pattern\Regex
object that is the main container of the regex structure.
To build a regular expression you can use the create
method on REBuilder base class:
$regex = REBuilder\Rebuilder::create();
Now the $regex
variable contains an empty REBuilder\Pattern\Regex
object to which you can add any pattern to compose the final regular expression.
Patterns are the parts that compose the regular expression.
In REBuilder patterns are grouped under the Pattern
namespace and inherit from REBuilder\Pattern\AbstractPattern
class, they have some common methods like render
to get their string representation or setRepetition
to set their repetition.
Some patterns can contain child patterns, these are called containers and inherit from the REBuilder\Pattern\AbstractContainer
class, for example SubPatterns, Assertions and even the Regex class are containers. Containers has some methods to manipulate and access their children like: getChildren
, addChild
, removeChild
etc.
Containers have also some special methods to add child patterns, these methods are in the format add(Pattern type) to add a child pattern and return it and add(Pattern type)AndContinue to add the pattern and return the container, in this way you can chain methods to compose the regex:
$regex = REBuilder\Rebuilder::create();
$regex
->addChar("a") //Add the character "a"
->setRepetition("*") //Set a repetition for the character
->getParent() //Back to the regex object
->addCharAndContinue("b"); //Add the character "b"
Alternatively you can call addChild and pass an instance of the pattern to add:
$regex = REBuilder\Rebuilder::create();
$char = new REBuilder\Pattern\Char("a");
$char->setRepetition("*");
$regex->addChild($char);