New from Chevere is the Regex package. This software enables to work with validated regular expression.
The package source is available at chevere/regex.
The Regex package enables to create a Regex
object from a regular expression with valid syntax check:
use Chevere\Regex\Regex;
$regex = new Regex('/^Hello World!$/');
From there it provides handy methods to read the original regular expression:
$regex->__toString();
// /^Hello World!$/
$regex->noDelimiters();
// ^Hello World!$
$regex->noDelimitersNoAnchors();
// Hello World!
It provides methods returning array for value matching:
$match = $regex->match($value);
$matches = $regex->matchAll($value);
And it provides assertion matching which throws an exception on no matches:
$regex->assertMatch($value);
$regex->assertMatchAll($value);