PHPackages                             multihanded/regent - 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. multihanded/regent

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

multihanded/regent
==================

The package for Laravel allows you to write regular expressions declaratively.

1.4.0(1y ago)28181MITPHPPHP ^8.2

Since Jun 5Pushed 1y ago1 watchersCompare

[ Source](https://github.com/YakutD/regent)[ Packagist](https://packagist.org/packages/multihanded/regent)[ RSS](/packages/multihanded-regent/feed)WikiDiscussions master Synced 1mo ago

READMEChangelogDependencies (4)Versions (6)Used By (0)

Regent
======

[](#regent)

About
-----

[](#about)

The package for Laravel allows you to write regular expressions declaratively.

For example:

- contains only letters of the English alphabet or numbers
- length from 5 to 10 characters

Instead of writing the regular expression directly, you can do it in a more elegant way:

```
echo Regent::startsWith()
                ->openAnyOf()
                    ->range('a', 'z')
                    ->digit()
                ->closeAnyOf()
                ->between(5, 10)
                ->endsWith()
                ->caseless(); // ~^[a-z\d]{5,10}$~i
```

In the case of more complex regular expressions, you can separately collect its subpatterns, which is very convenient, especially when it is possible to reuse and modify them in the process of building other regular expressions:

```
    // regex to search for email in text
    $anyOf = Regent::openAnyOf()
                    ->range('a', 'z')
                    ->range(0, 9)
                    ->addPattern('_-')
                ->closeAnyOf()->pattern;

    $startsWith= Regent::openGroup()
                    ->addPattern($anyOf)
                    ->oneOrMore()
                    ->addPattern('.', true)
                  ->closeGroup()->pattern;

    $secondGroup = Regent::openGroup()
                        ->addPattern('.', true)
                        ->addPattern($anyOf)
                        ->oneOrMore()
                   ->closeGroup()->pattern;

    $firstSegment = Regent::zeroOrMore()
                    ->addPattern($anyOf)
                    ->oneOrMore()
                    ->addPattern('@')
                    ->addPattern($anyOf)
                    ->oneOrMore()->pattern;

    $endWith = Regent::zeroOrMore()
                    ->addPattern('.', true)
                    ->openAnyOf()
                        ->range('a', 'z')
                    ->closeAnyOf()
                    ->between(2, 6)->pattern;

    echo Regent::startsWith($startsWith)
                ->addPattern($firstSegment)
                ->addPattern($secondGroup)
                ->endsWith($endWith)
                ->caseless(); // ~^([a-z0-9_-]+\.)*[a-z0-9_-]+@[a-z0-9_-]+(\.[a-z0-9_-]+)*\.[a-z]{2,6}$~i
```

How to install
--------------

[](#how-to-install)

You can install the package with the composer:

```
composer require multihanded/regent

```

How to use
----------

[](#how-to-use)

The package is supposed to be used through the facade:

```
use MultihandED\Regent\Facades\Regent;
```

After constructing the expression, you can simply cast the resulting object to a string.

Note that the facade is configured to **always** return a new instance of the regular expression builder class.

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

[](#documentation)

### Public properties

[](#public-properties)

- **pattern** (string) - regular expression pattern, empty string by default.
- **flags** (string) - regular expression flags, empty string by default.

It is not recommended to set these properties directly, however you are free to do so at your own discretion.

### Public methods

[](#public-methods)

- static **init** (string *$delimiter* = null): static - creates and returns an instance of the regular expression builder. Takes a delimiter as an argument. If no delimiter is provided, the default delimiter will be set.
- **clear**(bool *$withDelimiter* = false): self - sets the **pattern** and **flags** properties to empty strings. If the *$withDelimiter* argument is *true* then the delimiter will be set to its default value. Returns a reference to the call object.

#### Delimiter

[](#delimiter)

- static **setDelimiterDefault**(string *$newDelimiter*) - allows you to set a default delimiter. Once set, all newly created Regent objects will use *$newDelimiter* as their delimiter. Returns nothing.
- static **getDelimiterDefault**(): string - returns the current value of the default delimiter.
- **getDelimiter**(): string - returns the delimiter value of the call object.

**Regent** accepts one of these characters as a valid delimiter: **/**, **~**, **@**, **;**, **%**, **`** **\#**. By default, the default delimiter is **~** (tilde).

You can also set the default delimiter in *AppServiceProvider* of your project:

```
public function boot()
{
    Regent::setDelimiterDefault('%');
}
```

#### Flags

[](#flags)

List of valid flags: **i**, **m**, **s**, **x**, **A**, **D**, **S**, **U**, **X**, **J**, **u**. You can read more in the [official PHP documentation](https://www.php.net/manual/en/reference.pcre.pattern.modifiers.php).

- **flag**(string *$flag*, bool *$add* = true): self - adds the flag specified in the *$flag* argument to the **flags** property. Ignores empty lines and duplicate flags. If the *$add* argument is *false* then the flag will be removed (if this flag is not in **flags** it will be ignored). Returns a reference to the call object.
- **massFlags**(array *$flags*, bool *$add* = true): self - adds the values specified in the *$flags* argument to the **flags** property. Throws an exception when passing an empty array in *$flags*. Otherwise, it works similar to the **flag** method. Returns a reference to the call object.
- **massFlagsAssoc**(array *$flags*): self - adds the values specified in the *$flags* argument to the **flags** property. Throws an exception when passing an empty array in *$flags*. The *$flags* argument must be an associative array, where the key is the flag to add and the value is *$add* as in the **flag** and **massFlags** methods. For example, an array like \["i" =&gt; true, "m" =&gt; false\] would add the **i** flag and remove the **m** flag. Returns a reference to the call object.
- **inlineFlags**(string *$inlineFlags*, bool $add = true): self - adds the flags specified in the *$inlineFlags* argument to the **flags** property. *$inlineFlags* is a string of consecutive flags, such as 'ims'. Otherwise, it works similar to the **flag** method. Returns a reference to the call object.
- **caseless**(bool *$add* = true) : self - adds the **i** (PCRE\_CASELESS) flag to the **flags** property. Otherwise, it works similar to the **flag** method. Returns a reference to the call object.
- **multiline**(bool *$add* = true) : self - adds the **m** (PCRE\_MULTILINE) flag to the **flags** property. Otherwise, it works similar to the **flag** method. Returns a reference to the call object.
- **dotAll**(bool *$add* = true) : self - adds the **s** (PCRE\_DOTALL) flag to the **flags** property. Otherwise, it works similar to the **flag** method. Returns a reference to the call object.
- **extended**(bool *$add* = true) : self - adds the **x** (PCRE\_EXTENDED) flag to the **flags** property. Otherwise, it works similar to the **flag** method. Returns a reference to the call object.
- **anchored**(bool *$add* = true) : self - adds the **A** (PCRE\_ANCHORED) flag to the **flags** property. Otherwise, it works similar to the **flag** method. Returns a reference to the call object.
- **dollarEndOnly**(bool *$add* = true) : self - adds the **D** (PCRE\_DOLLAR\_ENDONLY) flag to the **flags** property. Otherwise, it works similar to the **flag** method. Returns a reference to the call object.
- **extraAnalysisOfPattern**(bool *$add* = true) : self - adds the **S** (extra analysis) flag to the **flags** property. Otherwise, it works similar to the **flag** method. Returns a reference to the call object.
- **ungreedy**(bool *$add* = true) : self - adds the **U** (PCRE\_UNGREEDY) flag to the **flags** property. Otherwise, it works similar to the **flag** method. Returns a reference to the call object.
- **extra**(bool *$add* = true) : self - adds the **X** (PCRE\_EXTRA) flag to the **flags** property. Otherwise, it works similar to the **flag** method. Returns a reference to the call object.
- **infoJChanged**(bool *$add* = true) : self - adds the **J** (PCRE\_INFO\_JCHANGED) flag to the **flags** property. Otherwise, it works similar to the **flag** method. Returns a reference to the call object.
- **utf8**(bool *$add* = true) : self - adds the **u** (PCRE\_UTF8) flag to the **flags** property. Otherwise, it works similar to the **flag** method. Returns a reference to the call object.
- **clearFlags**(): self - sets the **flags** property to an empty string. Returns a reference to the call object.

#### Inline modifiers

[](#inline-modifiers)

List of allowed local (inline) modifiers: **i**, **m**, **s**, **x**, **U**, **J**.

- **insertModifiers**(array *$activeModifiers* = \[\], array *$deactiveModifiers* = \[\]): self - setting inline modifiers by adding a string construction of the form **(?\*-\*)** to the **pattern** property, where **\*** before the hyphen sign are active modifiers (*$activeModifiers*), and \* after it are modifiers to be disabled (*$deactiveModifiers*) . Unlike the **flag** method, passing empty strings as an inline modifier is not allowed. Duplicate values are ignored (*$activeModifiers* takes precedence). If both *$activeModifiers* and *$deactiveModifiers* are empty arrays an exception will be thrown. Returns a reference to the call object.

#### Pattern

[](#pattern)

- ***addPattern***(string *$string*, bool *$quote* = false): self - adds to the **pattern** property the string passed in the *$string* argument. If the *$quote* argument is *true* then the *$string* function will be applied [preg\_quote](https://www.php.net/manual/en/function.preg-quote.php). Returns a reference to the call object.
- **startsWith**(string *$string* = '', bool *$quote* = false): self - declares the start of data (or string in multiline mode) by adding the metacharacter **^** at the beginning of **pattern**. The string passed in the *string* argument is concatenated directly **after** the metacharacter. Otherwise, it works similarly to the **addPattern** method. Returns a reference to the call object.
- **endsWith**(string *$string* = '', bool *$quote* = false): self - declares the end of the data, or before the end of the line (or the end of the line in multiline mode) by appending the metacharacter **$** to the end of **pattern**. The string passed in the *string* argument is concatenated directly **before** the metacharacter. Otherwise, it works similarly to the **addPattern** method. Returns a reference to the call object.
- **or**(string *$string* = '', bool *$quote* = false): self - start of conditional selection branch by adding the **|** metacharacter to the **pattern** property. The string passed in the *string* argument is concatenated directly **after** the metacharacter. Otherwise, it works similarly to the **addPattern** method. Returns a reference to the call object.
- **anyCharacter**(): self - search of matches any character except newline (by default) by adding the metacharacter **.** (dot) to the **pattern** property. Returns a reference to the call object.
- **openGroup**(): self - declares the beginning of subpattern by adding the **(** metacharacter to the **pattern** property. Returns a reference to the call object.
- **closeGroup**(): self - declares the end of subpattern by adding the **)** metacharacter to the **pattern** property. Returns a reference to the call object.
- **alphaNum**(bool *$except* = false): self - search all/except alphanumeric characters by adding the **\\w** metacharacter to the **pattern** property if the *$except* argument is *false* or **\\W** if it is *true*. Returns a reference to the call object.
- **whiteSpace**(bool *$except* = false): self - search all/except whitespace characters by adding the **\\s** metacharacter to the **pattern** property if the *$except* argument is *false* or **\\S** if it is *true*. Returns a reference to the call object.
- **whiteSpaceVertical**(bool *$except* = false): self - search all/except vertical whitespace characters by adding the **\\v** metacharacter to the **pattern** property if the *$except* argument is *false* or **\\V** if it is *true*. Returns a reference to the call object.
- **whiteSpaceHorizontal**(bool *$except* = false): self - search all/except horizontal whitespace characters by adding the **\\h** metacharacter to the **pattern** property if the *$except* argument is *false* or **\\H** if it is *true*. Returns a reference to the call object.
- **digit**(bool *$except* = false): self - search all/except digits by adding the **\\d** metacharacter to the **pattern** property if the *$except* argument is *false* or **\\D** if it is *true*. Returns a reference to the call object.
- **borderOfWord**(bool *$except* = false): self - search all/except word boundary by adding the **\\b** metacharacter to the **pattern** property if the *$except* argument is *false* or **\\B** if it is *true*. Returns a reference to the call object.
- **startOfLine**(): self - declares the start of line by adding the **\\A** metacharacter to the **pattern** property. Returns a reference to the call object.
- **endOfLine**(): self - declares the end of line by adding the **\\Z** metacharacter to the **pattern** property. Returns a reference to the call object.
- **endOfAction**(): self - declares the end of action by adding the **\\G** metacharacter to the **pattern** property. Returns a reference to the call object.
- **openAnyOf**(bool *$except* = false): self - declares the start of character class declaration by adding the **\[** metacharacter to the **pattern** property if the *$except* argument is *false* or **\[^** if it is *true*. Returns a reference to the call object.
- **closeAnyOf**(): self - declares the end of character class declaration by adding the **\]** metacharacter to the **pattern** property. Returns a reference to the call object.
- **backReference**(int *$num*): self - declares the back reference by adding a string construction of the form **\\\\\*** to the **pattern** property, where **\*** is the number passed in the *$num* argument. Returns a reference to the call object.
- **look**(bool *$ahead*, bool *$except* = false, string *$string* = '', bool *$quote* = false): self - search of matches an element only if there is (not) an element before/after it by adding one of the following metacharacters to the **pattern** property depending on the *$ahead* and *$except* arguments:

    - **?=** - *$ahead* is *true*, *except* is *false*;
    - **?!** - *$ahead* is *true*, *except* is *true*;
    - **?&lt;=** - *$ahead* is *false*, *except* is *false*;
    - **?&lt;!** - *$ahead* is *true*, *except* is *true*.

The string passed in the *string* argument is concatenated directly **after** the metacharacter. Otherwise, it works similarly to the **addPattern** method. Returns a reference to the call object.

- **range**(string *$from*, string *$to*): self - declares a character range by adding a string construction of the form **\*-\*** to the **pattern** property, where **\*** before the hyphen sign is the argument *$from* and after is argument *$to*. Returns a reference to the call object.
- **clearPattern**(): self - sets the **pattern** property to an empty string. Returns a reference to the call object.

#### Quantifiers

[](#quantifiers)

The *$lazy* argument specifies the greedy/lazy mode (by default, all quantifiers work in greedy mode).

- **zeroOrMore**(bool *$lazy* = false, string *$string* = '', bool *$quote* = false): self - adds the **\*** (0 or more occurrences) quantifier to the **pattern** property. The string passed in the *string* argument is concatenated directly **before** the quantifier. Otherwise, it works similarly to the **addPattern** method. Returns a reference to the call object.
- **oneOrMore**(bool *$lazy* = false, string *$string* = '', bool *$quote* = false): self - adds the **+** (1 or more occurrences) quantifier to the **pattern** property. The string passed in the *string* argument is concatenated directly **before** the quantifier. Otherwise, it works similarly to the **addPattern** method. Returns a reference to the call object.
- **zeroOrOne**(bool *$lazy* = false, string *$string* = '', bool *$quote* = false): self - adds the **?** (0 or 1 occurrence) quantifier to the **pattern** property. The string passed in the *string* argument is concatenated directly **before** the quantifier. Otherwise, it works similarly to the **addPattern** method. Returns a reference to the call object.
- **exactly**(int *$num*, bool *$lazy* = false): self - adds a string construction of the form **{\*}** (exact number of occurrences) to the **pattern** property, where **\*** is the number passed in the *$num* argument. Returns a reference to the call object.
- **atLeast**(int *$num*, bool *$lazy* = false): self - adds a string construction of the form **{\*,}** (minimal number of occurrences) to the **pattern** property, where **\*** is the number passed in the *$num* argument. Returns a reference to the call object.
- **atMax**(int *$num*, bool *$lazy* = false): self - adds a string construction of the form **{,\*}** (maximal number of occurrences) to the **pattern** property, where **\*** is the number passed in the *$num* argument. Returns a reference to the call object.
- **between**(int *$min*, int *$max*, bool *$lazy* = false): self - adds a string construction of the form **{\*,\*}** (occurrences in a given range) to the **pattern** property, where **\*** before the comma is the number passed in the *$min* argument and after in the *$max* argument. Returns a reference to the call object.

###  Health Score

36

—

LowBetter than 82% of packages

Maintenance47

Moderate activity, may be stable

Popularity16

Limited adoption so far

Community8

Small or concentrated contributor base

Maturity60

Established project with proven stability

 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

Every ~171 days

Total

5

Last Release

392d ago

PHP version history (4 changes)1.0.0PHP &gt;=7.4

1.1.0PHP ^8.0

1.2.0PHP ^8.1

1.3.0PHP ^8.2

### Community

Maintainers

![](https://www.gravatar.com/avatar/a96cdd30c0a9097b3ffecde6c546caedb02f389379e788a1c1371f3f8c180529?d=identicon)[YakutD](/maintainers/YakutD)

---

Top Contributors

[![YakutD](https://avatars.githubusercontent.com/u/23060971?v=4)](https://github.com/YakutD "YakutD (7 commits)")

###  Code Quality

TestsPest

### Embed Badge

![Health badge](/badges/multihanded-regent/health.svg)

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

###  Alternatives

[wireui/wireui

TallStack components

1.8k1.3M16](/packages/wireui-wireui)[livewire/volt

An elegantly crafted functional API for Laravel Livewire.

4195.3M84](/packages/livewire-volt)[ramonrietdijk/livewire-tables

Dynamic tables for models with Laravel Livewire

21147.4k](/packages/ramonrietdijk-livewire-tables)

PHPackages © 2026

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