PHPackages                             alshahawi/xpressions - 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. [Validation &amp; Sanitization](/categories/validation)
4. /
5. alshahawi/xpressions

ActiveLibrary[Validation &amp; Sanitization](/categories/validation)

alshahawi/xpressions
====================

A fluent php api for regular expressions.

613PHP

Since Mar 2Pushed 9y ago3 watchersCompare

[ Source](https://github.com/AlShahawi/xpressions)[ Packagist](https://packagist.org/packages/alshahawi/xpressions)[ RSS](/packages/alshahawi-xpressions/feed)WikiDiscussions master Synced 4w ago

READMEChangelogDependenciesVersions (1)Used By (0)

Xpressions
==========

[](#xpressions)

A fluent PHP API for regular expressions.

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

[](#installation)

Xpressions now is not ready for production yet, but you can install it using composer via:

```
composer require alshahawi/xpressions:dev-master

```

Examples
--------

[](#examples)

### Creating an instance

[](#creating-an-instance)

```
$matcher = Xpressions::match();
```

### Matchting a string

[](#matchting-a-string)

```
$matcher = Xpressions::match()->exact('foo');

var_dump($matcher->test('bar')); // -> false
var_dump($matcher->test('foo')); // -> true
```

### Inline Matching

[](#inline-matching)

In some situations (like validation) you might want to match something in the start of the line and before the end of that line, in that case you might use `begin()` and `end()` methods. For example:

```
$matcher = Xpressions::match()->begin()->exact('bar')->end();

var_dump($matcher->test('foo bar')); // -> false
var_dump($matcher->test('bar foo')); // -> false
var_dump($matcher->test('bar'));     // -> true
```

> NOTE: without `begin()` and `end()` all of the above tests will match (returning true), because whatever the matched in the start or the end of the line will return true unless you specified the begin and the end of the line (the whole text).

### Matching an optional string

[](#matching-an-optional-string)

```
$matcher = Xpressions::match()->exact('foo')->maybe('bar')->exact('baz');

var_dump($matcher->test('foo'));       // -> false
var_dump($matcher->test('foobar'));    // -> false
var_dump($matcher->test('foobarbaz')); // -> true
var_dump($matcher->test('foobaz'));    // -> true
```

### Matching alphanumeric &amp; underscores

[](#matching-alphanumeric--underscores)

```
$matcher = Xpressions::match()->word();

var_dump($matcher->test('!')); // -> false
var_dump($matcher->test('a')); // -> true
var_dump($matcher->test('1')); // -> true
var_dump($matcher->test('_')); // -> true
```

> You may use `words()` to match a one or more word characters.

> You may use `nonWord()` as an inverse to this method.

### Matching a digit

[](#matching-a-digit)

```
$matcher = Xpressions::match()->digit();

var_dump($matcher->test('!')); // -> false
var_dump($matcher->test('a')); // -> false
var_dump($matcher->test('1')); // -> true
```

> You may use `digits()` to match a one or more word characters.

> You may use `nonDigit()` as an inverse to this method.

### Matching any of a given values

[](#matching-any-of-a-given-values)

```
$matcher = Xpressions::match()->any('foo', 'bar', 'baz');

var_dump($matcher->test('something')); // -> false
var_dump($matcher->test('foo'));       // -> true
var_dump($matcher->test('bar'));       // -> true
var_dump($matcher->test('baz'));       // -> true
```

Advanced Examples
-----------------

[](#advanced-examples)

### Matching an email address

[](#matching-an-email-address)

```
$matcher = Xpressions::match()
    ->begin() // match a line start
    ->oneOrMore(function($xpr) {
        $xpr->word()->or('.');
    })
    ->exact('@')
    ->words()
    ->oneOrMore(function($xpr) {
        $xpr->maybe('.')
            ->word();
    })
    ->end(); // match a line end

var_dump($matcher->test('invalid'));        // ->false
var_dump($matcher->test('me@example.com')); // ->true
```

Interested in more advanced examples?
-------------------------------------

[](#interested-in-more-advanced-examples)

Many of examples will be written and documented soon!, but you can view `tests/XpressionsTest.php` for now.

###  Health Score

22

—

LowBetter than 21% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity10

Limited adoption so far

Community8

Small or concentrated contributor base

Maturity41

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.

### Community

Maintainers

![](https://www.gravatar.com/avatar/0602d9e7baf6a25ba9c191c8bd0c34aed3537de7b3e3076e1ec87f91ddd58dfb?d=identicon)[AlShahawi](/maintainers/AlShahawi)

---

Top Contributors

[![AlShahawi](https://avatars.githubusercontent.com/u/5667648?v=4)](https://github.com/AlShahawi "AlShahawi (31 commits)")

---

Tags

regexregex-validatorregular-expressionvalidation

### Embed Badge

![Health badge](/badges/alshahawi-xpressions/health.svg)

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

###  Alternatives

[chaoswey/taiwan-id-validator

台灣身分證、統一編號驗證

319.9k](/packages/chaoswey-taiwan-id-validator)

PHPackages © 2026

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