mck89/rebuilder

REBuilder is a PHP library to build and parse regular expressions

dev-master / 1.0.x-dev 2016-09-15 15:11 UTC

This package is auto-updated.

Last update: 2024-10-29 04:41:59 UTC


README

REBuilder is a PHP 5.3+ library to build and parse regular expressions

Installation

Include the following requirement to your composer.json:

{
	"require": {
		"mck89/rebuilder": "dev-master"
	}
}

Run composer install and include the autoloader:

require_once "vendor/autoload.php";

Parse

REBuilder's parser builds a tree structure starting from a regular expression. For example this code:

//Parse the regular expression
$regex = REBuilder\REBuilder::parse("/parse\s+me/");

Generates this structure:

REBuilder\Pattern\Regex
    getStartDelimiter() => "/"
    getEndDelimiter() => "/"
    getChildren() => array(
        REBuilder\Pattern\Char
            getChar() => "parse"
            
        REBuilder\Pattern\GenericCharType
            getIdentifier() => "s"
            getRepetition() => REBuilder\Pattern\Repetition\OneOrMore
            
        REBuilder\Pattern\Char
            getChar() => "me"
    )

Build

REBuilder allows you to build regular expressions with object oriented PHP:

//Create an empty regular expression object
$regex = REBuilder\REBuilder::create();

$regex->addCharAndContinue("parse")
      ->addGenericCharType("s")
          ->setRepetition("+")
          ->getParent()
      ->addCharAndContinue("me");

echo $regex->render(); //"/parse\s+me/"