PHPackages                             hoels/regex - 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. [Utility &amp; Helpers](/categories/utility)
4. /
5. hoels/regex

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

hoels/regex
===========

A wrapper around regular expressions.

1.2(2y ago)0592MITPHPPHP &gt;=8.1

Since Dec 10Pushed 3mo ago1 watchersCompare

[ Source](https://github.com/hoels/regex)[ Packagist](https://packagist.org/packages/hoels/regex)[ RSS](/packages/hoels-regex/feed)WikiDiscussions main Synced today

READMEChangelog (1)Dependencies (3)Versions (2)Used By (0)

RegEx Wrapper
=============

[](#regex-wrapper)

This is a wrapper around regular expressions.

Regular expressions in PHP are a common point of failure and working with match groups in particular can be a major headache. Inspired by the functionality Kotlin offers with its `Regex` class, I decided to start implementing a similar functionality for PHP. Hoping that one day, the PHP team will improve the `preg_*` functions, this library provides a very simple wrapper around regular expressions in PHP.

In contrast to the Kotlin implementation, the delimiters (e.g. `/` at the beginning and end of the expression) have to be set. This allows for greater flexibility.

Installation
------------

[](#installation)

#### Requirements

[](#requirements)

- PHP 8.1+

#### Composer

[](#composer)

```
composer require hoels/regex
```

Documentation
-------------

[](#documentation)

### Regex class

[](#regex-class)

The main class of this library is the `Regex` class. The class currently provides the following functions:

#### containsMatchIn

[](#containsmatchin)

```
Regex::containsMatchIn(string $regex, string $input): bool
```

Indicates whether there is at least one match in `$input` for the regex given in `$regex`.

#### find

[](#find)

```
Regex::find(string $regex, string $input): MatchResult|null
```

Returns the first match in `$input` for the regex given in `$regex`. Returns `null` if there is no match and an `MatchResult` object if there is a match.

#### findAll

[](#findall)

```
Regex::findAll(string $regex, string $input): MatchResult[]
```

Returns an array of `MatchResult` objects for all matches in `$input` for the regex given in `$regex`. Returns an empty array if there is no match.

#### matchAt

[](#matchat)

```
Regex::matchAt(string $regex, string $input, int $index): MatchResult|null
```

Returns the first match in `$input` for the regex given in `$regex`, only if the match starts at `$index`. Returns `null`if there is no match and an `MatchResult` object if there is a match at `$index`.

#### replace

[](#replace)

```
Regex::replace(string $regex, string $input, string $replacement): string
```

Replaces all occurrences of `$regex` in `$input` by the replacement expression `$replacement`. Example:

```
echo Regex::replace("/(\\d\\.\\d)\\.\\d+/", "We support PHP 8.1.26 and 8.2.13.", "$1"); // We support PHP 8.1 and 8.2.
```

#### replaceFirst

[](#replacefirst)

```
Regex::replaceFirst(string $regex, string $input, string $replacement): string
```

Replaces the first occurrence of `$regex` in `$input` by the replacement expression `$replacement`. Example:

```
echo Regex::replaceFirst("/(\\d\\.\\d)\\.\\d+/", "We support PHP 8.1.26 and 8.2.13.", "$1"); // We support PHP 8.1 and 8.2.3.
```

### MatchResult class

[](#matchresult-class)

The `MatchResult` class has three properties:

- `value` (`string`): The matched string.
- `offset` (`int`): The offset of the matched string within the input string.
- `groups` (`MatchGroup[]`): An array of all matched groups.

#### Groups

[](#groups)

The `groups` property contains an array of `MatchGroup` objects.

The first element (index 0) will always represent the matched string, therefore the `value` and `offset` properties of the `MatchGroup` object will be the same as the ones of its `MatchResult` parent.

If the regular expression did not declare any groups, the first element will be the only one in the array.

If the regular expression does declare groups, there will be a `MatchGroup` object for every group in the order of appearance. Therefore, the first group will have the index 1, the second the index 2, and so on. If there are named groups, there will be additional array elements with the name as index. Note that the `MatchGroup` object will have the same properties as the one with the numeric index, but the object reference will not be the same.

### MatchGroup class

[](#matchgroup-class)

The `MatchGroup` class has two properties:

- `value` (`string|null`): The matched string or `null` if the declared group is outside the matched string.
- `offset` (`int`): The offset of the matched string within the input string. -1 if the declared group is outside the matched string.

Usage
-----

[](#usage)

### `Regex::containsMatchIn`

[](#regexcontainsmatchin)

```
use Regex\Regex;

if (Regex::containsMatchIn(pattern: "/^[a-z]+$/", subject: $_GET["input"])) {
    ...
}
```

### `Regex::find`

[](#regexfind)

```
use Regex\Regex;

$matchResult = Regex::find(pattern: "/(?P\d{4})\/(?P\d{2})\/(?P\d{2})/", subject: $input);
if ($matchResult !== null) {
    $year = $matchResult->getGroup("year")?->getValue();
    $month = $matchResult->getGroup("month")?->getValue();
    $day = $matchResult->getGroup("day")?->getValue();
}
```

###  Health Score

34

—

LowBetter than 75% of packages

Maintenance52

Moderate activity, may be stable

Popularity15

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity51

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% 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

Unknown

Total

1

Last Release

935d ago

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/51371415?v=4)[Kai Hölscher](/maintainers/hoels)[@hoels](https://github.com/hoels)

---

Top Contributors

[![hoels](https://avatars.githubusercontent.com/u/51371415?v=4)](https://github.com/hoels "hoels (3 commits)")

###  Code Quality

TestsPHPUnit

Static AnalysisPHPStan

Code StylePHP\_CodeSniffer

Type Coverage Yes

### Embed Badge

![Health badge](/badges/hoels-regex/health.svg)

```
[![Health](https://phpackages.com/badges/hoels-regex/health.svg)](https://phpackages.com/packages/hoels-regex)
```

###  Alternatives

[cloudstudio/resource-generator

Resource Generator for Laravel Nova

107139.6k1](/packages/cloudstudio-resource-generator)[inpsyde/disable-comments

Entirely ditches comments as a WordPress feature.

1722.6k1](/packages/inpsyde-disable-comments)

PHPackages © 2026

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