PHPackages                             rawr/t-regx - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Search &amp; Filtering](/categories/search)
4. /
5. rawr/t-regx

ActiveLibrary[Search &amp; Filtering](/categories/search)

rawr/t-regx
===========

PHP regular expression brought up to modern standards.

0.41.5(3y ago)452169.9k—7.2%18[1 issues](https://github.com/t-regx/T-Regx/issues)5MITPHPPHP &gt;=7.1.0CI passing

Since May 25Pushed 10mo ago6 watchersCompare

[ Source](https://github.com/t-regx/T-Regx)[ Packagist](https://packagist.org/packages/rawr/t-regx)[ GitHub Sponsors](https://github.com/Danon)[ RSS](/packages/rawr-t-regx/feed)WikiDiscussions master Synced 2w ago

READMEChangelog (10)Dependencies (3)Versions (81)Used By (5)

 [![T-Regx](t.regx.png)](https://phpregex.org)

 [![Build status](https://github.com/t-regx/T-Regx/workflows/build/badge.svg)](https://github.com/t-regx/T-Regx/actions/) [![Unit tests](https://camo.githubusercontent.com/528ea62efdc53e5479c2d7eacfb8263a1c110af515a5ba13c26c1759a6eda0d5/68747470733a2f2f636f766572616c6c732e696f2f7265706f732f6769746875622f742d726567782f542d526567782f62616467652e737667)](https://coveralls.io/github/t-regx/T-Regx) [![latest: 0.41.5](https://camo.githubusercontent.com/aac4384b4e0208054bbd4b818316542cacc0b333063b8f52f6a51c1ac13b2940/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c61746573742d302e34312e352d627269676874677265656e2e7376673f7374796c653d706f706f7574)](https://github.com/t-regx/T-Regx/releases) [![dependencies: 0](https://camo.githubusercontent.com/20f4b99a958aadb02ff273ac6428c17cf55c6b817657ed64b1c39c7f71955a0e/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f646570656e64656e636965732d302d627269676874677265656e2e737667)](https://github.com/t-regx/T-Regx)

T-Regx | Regular Expressions library
====================================

[](#t-regx--regular-expressions-library)

PHP regular expressions brought up to modern standards.

[See documentation](https://phpregex.org/) at [phpregex.org](https://phpregex.org/).

[![last commit](https://camo.githubusercontent.com/9105c2cf1033e8ee86e9628e1e652114792ddd97d2156bf361ab8a223de9c99a/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6c6173742d636f6d6d69742f742d726567782f542d526567782f646576656c6f702e737667)](https://github.com/t-regx/T-Regx/commits/develop)[![Unit tests](https://camo.githubusercontent.com/e10161c00494f4344817b8a1526ca66435c01c7413a51b758d9c2ce6a90a5577/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f556e697425323074657374732d333836372d627269676874677265656e2e737667)](https://github.com/t-regx/T-Regx)[![Unit tests](https://camo.githubusercontent.com/391eae1cd460ca5a7589bdc17e04267c5f9459fae6e3feabc5699d88381e59fe/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f417373657274696f6e732d363037392d627269676874677265656e2e737667)](https://github.com/t-regx/T-Regx)[![FQN](https://camo.githubusercontent.com/c7a578c7d4ffa09d1213972bd98a0d095979ece88548a0b1e75ebf8080c077af/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f46514e2d757365642d626c75652e737667)](https://github.com/kelunik/fqn-check)[![PRs Welcome](https://camo.githubusercontent.com/f521b8a3c3a0d013c2d37bfdc56de0a513e491926e02c6166a9dd36097d62145/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f50522d77656c636f6d652d627269676874677265656e2e7376673f7374796c653d706f706f7574)](http://makeapullrequest.com)[![Gitter](https://camo.githubusercontent.com/818eb588e820267c5dca4a6728e5aabe5f5d082fdc5d0d95e1ca28dbe7c9b0ed/68747470733a2f2f6261646765732e6769747465722e696d2f542d526567782f636f6d6d756e6974792e737667)](https://gitter.im/T-Regx/community?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge)

[![OS Arch](https://camo.githubusercontent.com/a7e782a1cdca8d8392497a8bdb76d7cd8f8a66f2fe0c378e4888c6df5923c10c/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f4f532d33322545322538302539306269742d627269676874677265656e2e737667)](https://github.com/t-regx/T-Regx/actions)[![OS Arch](https://camo.githubusercontent.com/d1c7d6413563f2ee4731af711c18517b06a0b074a0750b5a61fcd05c080c39d0/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f4f532d36342545322538302539306269742d627269676874677265656e2e737667)](https://github.com/t-regx/T-Regx/actions)[![OS Arch](https://camo.githubusercontent.com/bd1a4c64a5accafe6b686d741d4c3e912acf66bd338d725b3b5a7d5ea1d52a1f/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f4f532d57696e646f77732d626c75652e737667)](https://github.com/t-regx/T-Regx/actions)[![OS Arch](https://camo.githubusercontent.com/9623a22f5968448327cab1a2a85272ec4411c60513a07986ea6d27b06177d627/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f4f532d4c696e75782f556e69782d626c75652e737667)](https://github.com/t-regx/T-Regx/actions)

[![PHP Version](https://camo.githubusercontent.com/d29ceb6206232af72a55b5ed3e6fab0a1f2d6cb6c728a088c9c18c4b035a0765/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f5048502d372e312d626c75652e737667)](https://github.com/t-regx/T-Regx/actions)[![PHP Version](https://camo.githubusercontent.com/5180a354acf28fb748fbeddd95219b60e0145d1e0eac9f976ca9a1bc9bc0337b/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f5048502d372e322d626c75652e737667)](https://github.com/t-regx/T-Regx/actions)[![PHP Version](https://camo.githubusercontent.com/de6ad351fa59d9e301ffc48de85e4ca23954b4ba5746f324991a43aab522f893/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f5048502d372e332d626c75652e737667)](https://github.com/t-regx/T-Regx/actions)[![PHP Version](https://camo.githubusercontent.com/8a0234efb25bffbee74e0444a52c7796dee73c2785c9908a8c05ecf6eb0f5ff3/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f5048502d372e342d626c75652e737667)](https://github.com/t-regx/T-Regx/actions)[![PHP Version](https://camo.githubusercontent.com/82887c22962d0022d20f61e17b76e949b7c813ca52cd0b00e794487a1a951169/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f5048502d382e302d626c75652e737667)](https://github.com/t-regx/T-Regx/actions)[![PHP Version](https://camo.githubusercontent.com/55119e0553f52e25200843d20c0b7f94802872deee3c7737d4dcd41a851167c6/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f5048502d382e312d626c75652e737667)](https://github.com/t-regx/T-Regx/actions)[![PHP Version](https://camo.githubusercontent.com/731c0a72386365632f9a88847c496d04aa121df13cdcb8a3749ed01e8a7df8d7/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f5048502d382e322d626c75652e737667)](https://github.com/t-regx/T-Regx/actions)

1. [Installation](#installation)
    - [Composer](#installation)
2. [Examples](#examples)
3. [Documentation](#documentation)
4. [T-Regx fiddle - Try online](#try-it-online-in-your-browser)
5. [API](#api)
    1. [For standard projects -`pattern()`](#written-with-clean-api)
    2. For legacy projects - `preg::match_all()`
6. [Overview](#why-t-regx-stands-out)
    1. [Prepared patterns](#prepared-patterns)
    2. [Working with the developer](#working-with-the-developer)
    3. [Clean API](#written-with-clean-api)
    4. [Fatal errors](#protects-you-from-fatal-errors)
    5. [Clean Code](#t-regx-follows-the-philosophy-of-uncle-bob-and-clean-code)
    6. [Exceptions vs. errors](#exceptions-over-warningserrors)
7. [Comparison](#comparison)
    1. [Exceptions over warnings/errors](#exceptions-over-warningserrors)
    2. [Working with the developer](#working-with-the-developer)
    3. [Written with clean API in mind](#written-with-clean-api)
    4. [Philosophy of Uncle Bob and "Clean Code"](#t-regx-follows-the-philosophy-of-uncle-bob-and-clean-code)
8. [Plans for the future](#current-work-in-progress)
9. [Sponsors](#sponsors)
10. [License](#license)

[Buy me a coffee!](https://www.buymeacoffee.com/danielwilkowski)

Installation
============

[](#installation)

Installation for PHP 7.1 and later (PHP 8 as well):

```
composer require rawr/t-regx
```

T-Regx only requires `mb-string` extension. No additional dependencies or extensions are required.

Examples
========

[](#examples)

Illustration of methods `match()`, `test()` and `count()`.

```
$pattern = Pattern::of("ups"); // pattern("ups") also works
$matcher = $pattern->match('yay, ups');

foreach ($matcher as $detail) {
   $detail->text();    // (string) "ups";
   $detail->offset();  // (int) 0
}

if (!$matcher->test()) {
   echo "No occurrances found";
} else {
   echo "Found {$matcher->count()} occurrences";
}
```

Documentation
=============

[](#documentation)

Full API documentation is available at [phpregex.org](https://phpregex.org/). List of changes is available in [ChangeLog.md](https://github.com/t-regx/T-Regx/blob/develop/ChangeLog.md).

Quick links:

- [Introduction](https://phpregex.org/docs/introduction) -
    - [Matching overview](https://phpregex.org/docs/match) -
    - [Replacing](https://phpregex.org/docs/replace) -
    - [Splitting](https://phpregex.org/docs/split) -
- [Prepared patterns](https://phpregex.org/docs/prepared-patterns) -
    - [Template syntax](https://phpregex.org/docs/template-syntax) -
    - [Pattern list](https://phpregex.org/docs/pattern-list) -

Try it online, in your browser!
===============================

[](#try-it-online-in-your-browser)

Open [T-Regx fiddle](https://repl.it/github/T-Regx/fiddle) and start playing around right in your browser. [Try now!](https://repl.it/github/T-Regx/fiddle)

API
===

[](#api)

Choose the interface:

- I choose the **modern regex API**:

    [Scroll to see](#written-with-clean-api) - `pattern()->test()`, `pattern()->match()`, `pattern()->replace()`
- I choose to **keep PHP methods** *(but protected from errors/warnings)*:

    [Scroll to see](#exceptions-over-warningserrors) - `preg::match_all()`, `preg::replace_callback()`, `preg::split()`

For standard projects, we suggest `pattern()`. For legacy projects, we suggest `preg::match_all()`.

- Standard T-Regx

    ```
    $pattern = Pattern::of("ups"); // pattern("ups") also works
    $matcher = $pattern->match('yay, ups');

    if (!$matcher->test()) {
      echo "Unmatched subject :/";
    }

    foreach ($matcher as $detail) {
      $detail->text();    // (string) "ups";
      $detail->offset();  // (int) 0
    }

    $pattern->replace('well, ups')->with('heck') // (string) "well, heck";
    ```
- Legacy API

    ```
    try {
        preg::match_all('/?ups/', 'ups', $match, PREG_PATTERN_ORDER);
        echo $match[0][0];
    } catch (\TRegx\Exception\MalformedPatternException $exception) {
        echo "Invalid pattern";
    }
    ```

Why T-Regx stands out?
======================

[](#why-t-regx-stands-out)

💡 [See documentation at phpregex.org](https://phpregex.org/)

- ### Prepared patterns

    [](#prepared-patterns)

    Using user data isn't always safe with PCRE (even with `preg_quote()`), as well as just not being that convenient to use. T-Regx provides dedicated solution for building patterns with unsafe user input. Choose `Pattern::inject()` for simply including user data as literals. Use `Pattern::mask()` to convert user-supplied masks into full-fledged patterns, safely. Use `Pattern::template()` for constructing more complex patterns.

    ```
    function makePattern($name): Pattern {
      if ($name === null) {
        return Pattern::of("name[:=]empty");
      }
      return Pattern::inject("name[:=]@;", [$name]); // inject $name as @
    }

    $gibberish = "(my?name)";
    $pattern = makePattern($gibberish);

    $pattern->test('name=(my?name)'); // (bool) true
    ```
- ### Working **with** the developer

    [](#working-with-the-developer)

    - Simple methods
        - T-Regx exposes functionality by simple methods, which return `int`, `string`, `string[]` or `bool`, which aren't nullable. If you wish to do something with your match or pattern, there's probably a method for that, which does exactly and only that.
    - Strings:
        - [Fixing error with multibyte offset (utf-8 safe)](https://phpregex.org/docs/match-details#offsets).
        - Separate methods for positions:
            - `offset()` - which returns position of a match in characters in UTF-8
            - `byteOffset()` - which returns position of a match in bytes, regardless of encoding
    - Groups:
        - When using `preg::match_all()`, we receive an array, of arrays, of arrays. In contrast, T-Regx returns an array of groups: `Group[]`. Object `Group` contains all the information about the group.
        - Group errors:

            - When invalid group named is used `get('!@#')`, T-Regx throws `\InvalidArgumentException`.
            - When attempt to read a missing group, T-Regx throws `NonexistentGroupException`.
            - When reading a group that happens not to be matched, T-Regx throws `GroupNotMatchedException`.
- ### Written with clean API

    [](#written-with-clean-api)

    - Descriptive, simple interface
    - Unicode support out-of-the-box
    - No Reflection used, `No (...varargs)`, `No (boolean arguments, true)`, `(No flags, 1)`, `[No [nested, [arrays]]]`
    - Inconsistencies between PHP versions are eliminated in T-Regx
- ### Protects you from fatal errors

    [](#protects-you-from-fatal-errors)

    Certain arguments cause fatal errors with `preg_()` methods, which terminate the application and can't be caught. T-Regx will predict if given argument would cause a fatal error, and will throw a catchable exception instead.
- ### T-Regx follows the philosophy of Uncle Bob and "Clean Code"

    [](#t-regx-follows-the-philosophy-of-uncle-bob-and-clean-code)

    Function should do one thing, it should do it well. A function should do exactly what you expect it to do.
- ### Compatible with other tools and libraries

    [](#compatible-with-other-tools-and-libraries)

    Granted, `Pattern::of()` accepts undelimited pattern (`(Foo){3,4}}`) is not suitable with other PHP libraries, which work with delimited patterns (`/(Foo){3,4}/`), for example Laravel and Routing. For that case, use `PcrePattern::of()` which accepts plain-old standard PHP syntax.
- ### Exceptions over warnings/errors

    [](#exceptions-over-warningserrors)

    - Unlike PHP methods, T-Regx doesn't use warnings/notices/errors for unexpected inputs: ```
        try {
          preg::match_all('/([a3]+[a3]+)+3/', 'aaaaaaaaaaaaaaaaaaaa 3');
        } catch (\TRegx\SafeRegex\Exception\CatastrophicBacktrackingException $exception) {
          // caught
        }
        ```
    - Detects **malformed patterns** in and throws `MalformedPatternException`. This is impossible to catch with `preg_last_error()`. ```
        try {
          preg::match('/?ups/', 'ups');
        } catch (\TRegx\Exception\MalformedPatternException $exception) {
          // caught
        }
        ```
    - Not every error in PHP can be read from `preg_last_error()`, however T-Regx throws dedicated exceptions for those events.

Comparison
==========

[](#comparison)

[![Ugly api](https://camo.githubusercontent.com/2a9b3a1528cd6e5af9721f5dfd830df7ebfe53996bdde98d60890fdc7370c757/68747470733a2f2f70687072656765782e6f72672f696d672f65787465726e616c2f726561646d652f707265672e706e67)](https://camo.githubusercontent.com/2a9b3a1528cd6e5af9721f5dfd830df7ebfe53996bdde98d60890fdc7370c757/68747470733a2f2f70687072656765782e6f72672f696d672f65787465726e616c2f726561646d652f707265672e706e67)

or

[![Pretty api](https://camo.githubusercontent.com/b43d8d8478e1a18d37fb733d075795f68dd039ca1544bbf9b6be55532c224f32/68747470733a2f2f70687072656765782e6f72672f696d672f65787465726e616c2f726561646d652f742d726567782e706e673f)](https://camo.githubusercontent.com/b43d8d8478e1a18d37fb733d075795f68dd039ca1544bbf9b6be55532c224f32/68747470733a2f2f70687072656765782e6f72672f696d672f65787465726e616c2f726561646d652f742d726567782e706e673f)

Current work in progress
========================

[](#current-work-in-progress)

Current development priorities, regarding release of 1.0:

- Add documentation to each T-Regx public method #17 \[in progress\]
- Revamp of [phpregex.org](https://phpregex.org/) documentation \[in progress\]
- Release 1.0

Sponsors
========

[](#sponsors)

- [Andreas Leathley](https://github.com/iquito) - developing [SquirrelPHP](https://github.com/squirrelphp)
- [BarxizePL](https://github.com/BarxizePL) - Thanks!

T-Regx is developed thanks to
=============================

[](#t-regx-is-developed-thanks-to)

[ ![JetBrains](https://camo.githubusercontent.com/8bd2bd7542437947620fba7d80249e24f395d68490a1bfb03cc8f96283638237/68747470733a2f2f70687072656765782e6f72672f696d672f65787465726e616c2f6a6574627261696e732d76617269616e742d342e737667)](https://www.jetbrains.com/?from=T-Regx)License
-------

[](#license)

T-Regx is [MIT licensed](LICENSE).

###  Health Score

48

—

FairBetter than 94% of packages

Maintenance39

Infrequent updates — may be unmaintained

Popularity53

Moderate usage in the ecosystem

Community26

Small or concentrated contributor base

Maturity61

Established project with proven stability

 Bus Factor1

Top contributor holds 97.4% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~27 days

Recently: every ~165 days

Total

78

Last Release

573d ago

Major Versions

0.41.5 → 1.0.0-alpha2024-05-08

PHP version history (3 changes)0.9.0PHP &gt;=7.1.0

0.10.0PHP ~8.0.0 | &gt;=7.1.0

1.0.0-alphaPHP &gt;=7.4.0

### Community

Maintainers

![](https://www.gravatar.com/avatar/0f388dbecf863b7cf5131dbac972514dab843f609977315694b9f25e0e63332d?d=identicon)[danon](/maintainers/danon)

---

Top Contributors

[![danon](https://avatars.githubusercontent.com/u/13367735?v=4)](https://github.com/danon "danon (2064 commits)")[![Danonk](https://avatars.githubusercontent.com/u/26869692?v=4)](https://github.com/Danonk "Danonk (48 commits)")[![iquito](https://avatars.githubusercontent.com/u/973653?v=4)](https://github.com/iquito "iquito (5 commits)")[![bartko-s](https://avatars.githubusercontent.com/u/675883?v=4)](https://github.com/bartko-s "bartko-s (1 commits)")[![peter279k](https://avatars.githubusercontent.com/u/9021747?v=4)](https://github.com/peter279k "peter279k (1 commits)")

---

Tags

pcrephpprepared-patternsregexregexpregular-expressionssearchexpressionregexregularPCREpregpatternMatchsplitregexppatternsregexpsprepareddelimiterst-regxprepared patternstregx

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/rawr-t-regx/health.svg)

```
[![Health](https://phpackages.com/badges/rawr-t-regx/health.svg)](https://phpackages.com/packages/rawr-t-regx)
```

###  Alternatives

[elasticsearch/elasticsearch

PHP Client for Elasticsearch

5.3k190.3M1.1k](/packages/elasticsearch-elasticsearch)[composer/pcre

PCRE wrapping library that offers type-safe preg\_\* replacements.

709360.8M68](/packages/composer-pcre)[ruflin/elastica

Elasticsearch Client

2.3k53.1M237](/packages/ruflin-elastica)[spatie/regex

A sane interface for php's built in preg\_\* functions

1.1k20.9M72](/packages/spatie-regex)[solarium/solarium

PHP Solr client

93235.0M125](/packages/solarium-solarium)[jasny/twig-extensions

A set of useful Twig filters

10611.3M13](/packages/jasny-twig-extensions)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
