Quick Start

This page is the shortest path from installation to a useful result. It does not teach regular expressions; the tutorial does that. When this page ends, the linked guide picks up the deeper workflow.

Installation

composer config minimum-stability dev
composer config prefer-stable true
composer require --dev php-regex/regex-cli:2.x-dev

You need PHP 8.1 or later with the mbstring and tokenizer extensions. The first two Composer commands run once: while the focused packages are 2.x-dev, they allow those prerelease versions while still preferring stable releases for everything else. The CLI package provides the regex binary and pulls in the toolkit facade, so this page can use both the terminal and the PHP API. For another surface, choose the right package.

Verify the binary:

vendor/bin/regex --version

Run your first analysis

Start with a date pattern you can read at a glance:

vendor/bin/regex validate '/^\d{4}-\d{2}-\d{2}$/'
vendor/bin/regex explain '/^\d{4}-\d{2}-\d{2}$/' --no-visuals

validate answers whether the target PHP and its PCRE2 release accept the pattern; its report ends with Status : OK. explain walks the tree the parser built from the pattern and describes each part in plain English, in order:

Regex matches
  Anchor: the start of the subject
  Character Type: A digit [0-9], exactly 4 times
  '-'
  Character Type: A digit [0-9], exactly 2 times
  '-'
  Character Type: A digit [0-9], exactly 2 times
  Anchor: the end of the subject, or just before a newline (\n) that ends it

See the same pattern as a tree in the playground — the tree, the diagnostics and the step-through of the match, in your browser.

Now try a pattern with catastrophic backtracking:

vendor/bin/regex analyze '/^(?:a+)+$/' --no-visuals

The verdict comes first, before the explanation of the pattern:

  Pattern: /^(?:a+)+$/
  Parse: OK
  Status: OK
  Status: Exponential backtracking (proven)
  Severity: CRITICAL (score 10)
  Mode: THEORETICAL
  Confidence: MEDIUM
  Attack: "a" x n . "!"
  Hotspot:   4-6

Attack: is the input that makes the pattern blow up, written as PHP: "a" x n . "!" means str_repeat("a", $n) . "!". The longer the run of a, the longer one match attempt takes: the time roughly doubles with every extra character. Watch it backtrack, step by step — the Step through tab draws every retry the engine makes.

The important distinction is the verdict label:

  • Proven: the analyzer built a model of how the engine retries this pattern and checked every case the model covers.
  • Heuristic: the pattern uses a construct, or needs a budget, outside that model, so conservative structural rules decided.
  • Unknown: the analysis refuses to guess.

Read the scope before acting on a security finding: correctness contracts.

Try the PHP API

The Regex facade is the fastest way to explore the API:

use PHPRegex\Toolkit\Regex;

$regex = Regex::create();

$validation = $regex->validate('/^\d{4}-\d{2}-\d{2}$/');
echo $validation->isValid ? "valid\n" : $validation->error."\n";

$redos = $regex->redos('/^(?:a+)+$/');
echo $redos->headline(), "\n";

If redos() proves the pattern vulnerable, the result also carries its witness: the input that makes the pattern backtrack, the one the Attack: line prints. Use that witness in a regression test before you rewrite the pattern.

For lower-level work, call Regex::parser() and reuse one configured parser instead of building a new one for every analysis:

$parser = $regex->parser();
$ast = $parser->parse('/(?<year>\d{4})/');
$result = $parser->validate('/(?<year>\d{4})/');

Pick your integration

You want findings in… Start with…
A terminal or CI job CLI and lint guide
PHPStan PHPStan extension guide
Psalm Psalm plugin guide
Your editor Language server guide
Laravel Laravel integration guide
Symfony Symfony bundle guide

For a codebase-wide first run:

vendor/bin/regex lint src/ --redos

Start without --redos on a large legacy project, then enable it once the ordinary validation and lint findings are under control. Use --format=github in GitHub Actions or --format=json when another tool consumes the report.

Where next?

Edit on GitHub Feedback