Your Docusaurus site did not load properly.

A very common reason is a wrong site baseUrl configuration.

Current configured baseUrl = / (default value)

We suggest trying baseUrl =

Project Logo

T-Regx

Programmer-oriented Regular Expressions wrapper library for PHP

  • Lightweight
  • Reliable
  • Secure
  • Based on exceptions

T-Regx

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.

Installation

composer require rawr/t-regx
$username = $_GET['username'];
// Use either helper function
pattern('^[a-zA-Z][a-zA-Z0-9]{1,15}$')->test($username); // true
// or facade
Pattern::of('^[a-zA-Z][a-zA-Z0-9]{1,15}$')->test($username); // 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.

$pattern = 'https?://(\w+\.\w+)';
pattern($pattern)->match($pattern)->first(function (Detail $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());
});

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::pcre(), Pattern::inject() or Pattern::bind(). The Detail is always the same.

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

$pattern = 'https?://(\w+\.\w+)';
pattern($pattern)->replace($str)->first()->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::prepare(), Pattern::inject(), Pattern::bind(), Pattern::compose(), Pattern::format() and Pattern::template().

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

$link = $_GET['link'];
Pattern::inject('(?<scheme>https?)://(?<domain>@/[^ ]+)', [$link])
->match($message)
->first(function (Detail $detail) {
// scheme
$scheme = $detail->get('scheme');
// domain
$domain = $detail->get('domain');
});
$message = "Files: song.mp3, video.mp4, file, movie.mp4";
$result = Pattern::of('\b\w+(\.(?<ext>\w{3}))?\b')
->replace($message)
->all()
->by()
->group('ext')
->map([
'mp3' => 'Audio',
'mp4' => 'Video'
])
->orElseWith('Unknown');
echo $result; // "Files: Audio, Video, Unknown, Video"

Functional programming

You can use either pattern() or pattern(), or Prepared Patterns.

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.

Try T-Regx online

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

You can use repl.it using your GitHub account, Facebook account or Google account -T-Regx on repl.it

Repl.it login
T-Regx online on Repl.it

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