Skip to main content

What's T-Regx?

T-Regx (T-Rex and RegExp) is a lightweight, high-level Regular Expressions library.

Its main features are:

  • Standard or new functions:

  • API:

  • Removing headaches of PHP:

    • You only use try/catch.

      T-Regx is doing all the complex ifology around preg under the hood, to unify all PHP corner-cases and map them to exceptions (see "Comparison").

    • All false positives and false negatives are handled

  • If anything - Exceptions!:

    • Magic values (like null, false, '', [], -1) aren't used -> Exceptions
    • Handles every warning/error/fatal error/notice -> Exception
    • preg_last_error() isn't required to verify errors -> Exception
    • errors/bugs/gotcha's aren't silently ignored -> Exception
    • supplied invalid arguments aren't silently ignored -> Exception
    • returned invalid values aren't silently ignored -> Exception


  • Each function obeys SRP
  • Functionalities are represented with methods() (not FLAGS or arguments)
  • No default parameters=null
  • No PREG_FLAGS, 1
  • No varargs...

PHP's magic values#

In vanilla PHP, different methods (preg_match(), preg_replace(), preg_split(), etc.) return different types and values, that are symbolic. For some it's null or "", for other [] or false, 0 for others it's -1 - they're all supposed to mean "error". For example, preg_match() returns false on error, but preg_replace() returns null on error.

Of course, you have to remember which is === false and which === null.

T-Regx will never return null, false an empty array or other magic value. MalformedPatternException is thrown if $pattern is malformed.

And it's not just the return values, $match results also contain magic values like "" which can either mean the group wasn't matched, or it matched an empty string in PHP. In SafeRegex the false negative is left as it is, we don't change the API to allow simple migration of preg_match() to preg::match().

In CleanRegex this is handled, so you don't have to worry about it - "" always means an empty string.

Last updated on