Skip to main content
Project Logo


Programmer-oriented Regular Expressions wrapper 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
// instantiate pattern from string
$pattern = Pattern::of('^[a-zA-Z][a-zA-Z0-9]{1,15}$');
// test subject against a pattern
$pattern->test($_GET['username']); // (bool) true

Match your subject against a pattern

Use pattern()->test() to check whether your subject matches a given regular expression.

In this case whether ^[a-zA-Z][a-zA-Z0-9]{1,15}$ matches $username.

// instantiate pattern from string
$pattern = Pattern::of('https?://(\w+\.\w+)');
// match a pattern against a subject
$match = $pattern->match('This is my link€:');
if (!$match->test()) {
echo "Subject not matched";
foreach ($match as $detail) {
// cast to string
echo "I matched: $detail";
// capturing group
$domain = $detail->get(1); // (string) ""
// offset in characters (UTF-8 safe)
$detail->group(1)->offset(); // (int) 26
// offset in raw bytes (bytes)
$detail->group(1)->byteOffset(); // (int) 28

Match details

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

It doesn't matter whether the pattern was constructed with Pattern::of(), pattern(), Pattern::inject() or Pattern::template(). The Detail is always the same.

Checkout the documentation about Detail, which describes every Detail method.

// instantiate pattern from string
$pattern = Pattern::of('https?://(\w+\.\w+)');
// start replacements
$replace = $pattern->replace($stringWithLinks);
// replace first match by callback
$replaced = $replace->callback(function (Detail $match) {
// cast to string
echo "I matched: $match";
// capturing group
$domain = $match->get(1);
// replace with
return "new domain: $domain";

Uniform API for matching and replacing

pattern()->match() and pattern()->replace() callbacks receive the same interface Detail.

Detail used for matching and replacing has exactly the same methods and returns the same values for given $pattern and $subject.

You can also try it online in 10 seconds - there are examples in the as sandbox for your own tries.

Prepared patterns

With Prepared Patterns you can safely build your regular expressions, without worrying about it becoming malformed or dangerous.

T-Regx provides a wide variety of prepared patterns, for different needs: Pattern::inject(), Pattern::list(), Pattern::mask() and Pattern::template().

For constant patterns use Pattern::of() or simply pattern().

// instantiate pattern from a prepared pattern and a user supplied value
$link = $_GET['link'];
$pattern = Pattern::inject('(?<scheme>https?)://(?<domain>@/[^ ]+)', [$link]);
// ↑
// match a pattern against a subject
$match = $pattern->match($message);
// call Detail for the first match
$detail = $match->first();
// scheme
$scheme = $detail->get('scheme');
// domain
$domain = $detail->get('domain');
function getPattern(): Pattern {
if ($condition) {
return pattern('^[a-zA-Z][a-zA-Z0-9]{1,15}$'); // helper-style
return Pattern::of('^[a-zA-Z][a-zA-Z0-9]{1,15}$'); // facade-style
// instantiate pattern
$pattern = getPattern();
// test a subject

Different ways of instantiating Patterns

Regardless of whether you build your pattern using Pattern::of(), pattern() helper, or maybe using prepared patterns like Pattern::inject()/Pattern::alternate(), the interface of Pattern is the same.

Additionally, building patterns using Pattern::mask() or Pattern::template() also share the same Pattern interface.

Try T-Regx online

Before you install, you can try T-Regx online, right in your browser.

You can use using your GitHub account, Facebook account or Google account -T-Regx on login
T-Regx online on

Written with clean design in mind

T-Regx utilises well designed interfaces, so your IDE will aid you as you type! We follow real SOLID and OOP.

We love static typing and IDE suggestions
Written with clean design in mind