PHPackages                             thejano/easy-php-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. thejano/easy-php-regex

ActiveLibrary

thejano/easy-php-regex
======================

A human-readable regex builder for PHP

1.0.0(1y ago)11MITPHPPHP &gt;=7.4CI failing

Since Feb 20Pushed 1y ago1 watchersCompare

[ Source](https://github.com/thejano/easy-php-regex)[ Packagist](https://packagist.org/packages/thejano/easy-php-regex)[ RSS](/packages/thejano-easy-php-regex/feed)WikiDiscussions main Synced 1mo ago

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

EasyRegex
=========

[](#easyregex)

A Human-Readable Regex Builder for PHP
--------------------------------------

[](#a-human-readable-regex-builder-for-php)

EasyRegex is a fluent and human-readable regex builder for PHP that simplifies constructing complex regular expressions without manually writing regex patterns.

---

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

[](#installation)

To install this package using Composer, run:

```
composer require thejano/easy-php-regex
```

---

Usage
-----

[](#usage)

### Creating Regex Patterns

[](#creating-regex-patterns)

Use the `EasyRegex` class to build and generate regex patterns fluently.

#### Example: Matching an Email Address

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

```
use TheJano\EasyRegex\EasyRegex;

$regex = (new EasyRegex())
    ->startAnchor()
    ->word()
    ->oneOrMore()
    ->add('@')
    ->word()
    ->oneOrMore()
    ->add('.')
    ->word()
    ->between(2, 5)
    ->endAnchor()
    ->toRegExp();

// Output: /^\w+@\w+\.\w{2,5}$/
```

#### Example: Validating a Strong Password

[](#example-validating-a-strong-password)

```
$regex = (new EasyRegex())
    ->startAnchor()
    ->hasLetter()
    ->hasDigit()
    ->hasSpecialCharacter()
    ->atLeast(8)
    ->endAnchor()
    ->toRegExp();

// Output: /^(?=.*[a-zA-Z])(?=.*\d)(?=.*[!@#$%^&*]).{8,}$/
```

#### Example: Matching a URL

[](#example-matching-a-url)

```
$regex = (new EasyRegex())
    ->protocol()
    ->www()
    ->word()
    ->oneOrMore()
    ->tld()
    ->path()
    ->toRegExp();

// Output: /https?:\/\/(www\.)?\w+\.[a-zA-Z]{2,}(\/\w*)*/
```

---

Available Methods
-----------------

[](#available-methods)

### Character Classes

[](#character-classes)

- `digit()` - Matches a digit (`\d`)
- `word()` - Matches a word character (`\w`)
- `whitespace()` - Matches a whitespace character (`\s`)
- `nonWhitespace()` - Matches a non-whitespace character (`\S`)
- `letter()` - Matches a letter (`[a-zA-Z]`)
- `anyCharacter()` - Matches any character (`.`)

### Quantifiers

[](#quantifiers)

- `optional()` - Matches zero or one times (`?`)
- `exactly(int $n)` - Matches exactly `n` times (`{n}`)
- `atLeast(int $n)` - Matches at least `n` times (`{n,}`)
- `atMost(int $n)` - Matches up to `n` times
- `between(int $min, int $max)` - Matches between `min` and `max` times (`{min,max}`)
- `oneOrMore()` - Matches one or more times (`+`)
- `zeroOrMore()` - Matches zero or more times (`*`)

### Grouping &amp; Anchors

[](#grouping--anchors)

- `startGroup()` - Starts a non-capturing group (`(?:`)
- `startCaptureGroup()` - Starts a capturing group (`(`)
- `startNamedGroup(string $name)` - Starts a named group (`(?`)
- `endGroup()` - Ends a group (`)`)
- `startAnchor()` - Matches the beginning of the string (`^`)
- `endAnchor()` - Matches the end of the string (`$`)

### Lookaheads &amp; Lookbehinds

[](#lookaheads--lookbehinds)

- `negativeLookahead(string $pattern)` - Negative lookahead (`(?!pattern)`)
- `positiveLookahead(string $pattern)` - Positive lookahead (`(?=pattern)`)
- `positiveLookbehind(string $pattern)` - Positive lookbehind (`(?
