PHPRegex Documentation

PHPRegex is a static analysis, linter & logic solver for PHP regular expressions. It parses every PCRE pattern into an AST and answers questions about it: validity ruled as PHP’s own engine would rule it, ReDoS safety with a proof or a witness, lint findings with fixes, provably equivalent rewrites, and pattern-to-pattern logic through automata.

It is written for the authors of PHP tools — static-analysis extensions, framework bundles, libraries that carry regexes. Start with your integration below. New to regex? The tutorial teaches regular expressions from scratch, and assumes nothing.

Start by task

I maintain a static-analysis tool or a library. The PHPStan, Psalm and Rector guides show what each extension reports and what it proves. Capture shapes derive what preg_match() writes into $matches straight from the AST, and the correctness contracts say, per feature, what is sound and what is heuristic.

I work on a Laravel or Symfony application. The Laravel and Symfony guides wire the Regex service and its commands; the CLI guide runs regex lint over the code base and in CI; the ReDoS guide reads the verdicts on the patterns your routes and validators accept.

I am new to regex. The tutorial is a ten-chapter walk from the first literal to the patterns running in production PHP.

Documentation map

Learn

Integrations

The guides index lists every integration on one page.

  • PHPStan - The patterns your target PHP refuses, plus lint and ReDoS findings in static analysis.
  • Psalm - $matches typed from the pattern, invalid patterns reported.
  • Rector - preg_* calls rewritten into the string functions they provably equal.
  • Laravel - Service, facade, and artisan commands.
  • Symfony - Bundle, service, and console commands.
  • LSP - Diagnostics, hovers, completions and code actions in any editor.
  • CLI - Sixteen subcommands, configuration, output formats, CI recipes.
  • ReDoS guide - Verdicts, guarantees, confirmed mode, fixes.
  • Cookbook - Practical patterns and examples.
  • Troubleshooting - Common errors and how to fix them.

Concepts

Reference

Internals

How PHPRegex works in brief

PHPRegex treats a regex literal as structured input:

  • The literal is split into pattern and flags.
  • The lexer emits a token stream.
  • The parser builds an AST.
  • Visitors walk the AST to validate, explain, analyze, or transform.

Every example output in these docs was produced by PHPRegex itself, running the same PCRE2 engine it reasons about.

Tips for newcomers

  • Always include delimiters and flags: /pattern/flags (for example, /hello/i).
  • Build patterns step by step, then add constraints.
  • Validate early: vendor/bin/regex validate catches errors quickly.
  • Explain patterns with vendor/bin/regex explain when reviewing code.

Getting help

Edit on GitHub