Project Logo


Programmer-oriented Regular Expressions library for PHP

  • Lightweight
  • Reliable
  • Secure
  • Based on exceptions


Quiz about Vanilla-PHP regular expressions

Super easy, see for yourself how well you know Vanilla-PHP regular expressions. Maybe it turns out you don't need T-Regx, after all :)

Start quiz

SafeRegex converts warnings to exceptions

SafeRegex watches for warnings, analyzes preg_() methods return values and looks up preg_last_error() to validate a call. If it fails, an exception is thrown.


composer require rawr/t-regx

Match details

With pattern()->match() and pattern()->replace(), it's trivial to retrieve, iterate, map and filter matches with callbacks and a detailed Match object.

Checkout the documentation about Match object with all Match methods. You can also try it online in 10 seconds - there are examples as well as sandbox for your own tries.

pattern('https?://(\w+\.\w+)')->match($string)->first(function (Match $match) {
// cast to string
echo "I matched: $match";
// capturing group
$domain = $match->get(1);
// use offset (UTF-8 safe)
mb_substr($match, 0, $match->group(1)->offset());
// use offset (bytes)
substr($match, 0, $match->group(1)->byteOffset());

Written with clean design in mind

No Reflection used, No (...varargs), No (boolean arguments, true), (No flags, 1)

Based on exceptions!

If any error occurs while using regexp (invalid pattern, malformed UTF8, backtrack limit, nonexistent group, anything!) T-Regx throws an exception, contrary to vanilla-PHP regexp, which use preg_last_error() or warnings, which can't be try/catched.
$message = "Extensions: .mp3, .mp4, .jpg, jpeg, png, gif";
'mp3' => 'Audio',
'gif' => 'Animation'

Functional programming

T-Regx utilizes chainable, functional programming with methods like filter(), map(), flatMap(), first()/findFirst(), etc.

Apart from standard functional methods, T-Regx provides dedicated methods, suited more for working with strings and capturing groups from T-Regx, like map(), mapIfExists(), mapAndCallback(), etc.