Skip to main content

PCRE

Describes how recheck checks a regular expression of the pcre dialect.

Engine​

recheck follows the syntax and the semantics of PCRE2 10.49 as PHP's preg_* functions use it.

Settings​

The pcre dialect has no settings.

$ recheck --dialect=pcre '/^(a|a)*$/'

Flags and Units​

The matching unit is byte, or scalar-value with the u flag or (*UTF) (see Matching Units).

recheck supports the following pattern modifiers of PHP.

FlagPCRE2 optionInlineNotes
iPCRE2_CASELESS(?i)
mPCRE2_MULTILINE(?m)
nPCRE2_NO_AUTO_CAPTURE(?n)PHP 8.2 or later.
rPCRE2_EXTRA_CASELESS_RESTRICT(?r)PHP 8.4 or later.
sPCRE2_DOTALL(?s)
uPCRE2_UTF and PCRE2_UCP(*UTF)(*UCP)
xPCRE2_EXTENDED(?x)xx is x too, while (?xx) is PCRE2_EXTENDED_MORE.
APCRE2_ANCHOREDThe match starts at the start of the input.
DPCRE2_DOLLAR_ENDONLY$ matches only at the end of the input, unless with m.
JPCRE2_DUPNAMES(?J)
UPCRE2_UNGREEDY(?U)
SNo effect (PHP 7.3 or later).
XNo effect (PHP 7.3 or later, where an unknown escape is always an error).

Syntax Limitations​

The following constructs are not supported.

  • Recursion and subroutine calls (e.g. (?R), (?1), (?&name) and \g<name>), including (?(DEFINE)...) in a pattern with calls, are reported as unsupported.
  • Callouts (e.g. (?C1)), (*scan_substring:...) and script runs (e.g. (*sr:...)) are reported as unsupported.
  • Extended bracketed character classes ((?[ ... ])) are not supported yet, and they are reported as invalid.

Approximations​

  • \G matches only at the start of the input, since recheck models a search from there, not one from a later position (e.g. the second match of preg_match_all).

Optimizations​

recheck does not implement the following optimizations specific to PCRE2, so many attacks that recheck reports do not work on PCRE2.

  • Auto-possessification.
  • The start-of-match optimizations, by which PCRE2 skips the attempts that cannot match (e.g. (*COMMIT)abc matches xyzabc in PCRE2, unless the pattern starts with (*NO_START_OPT)).
  • The JIT compiler (e.g. .+\/$ is safe with the JIT, and vulnerable with RECHECK_PHP='php -d pcre.jit=0').

Enable the runtime-attack verification to report only the patterns that are slow on PCRE2. The result depends on the settings of PHP, so run it with the ones of your environment (see Runtime Command).

Runtime-Attack Verification​

  • Command: php.
  • Matching: preg_match.
  • Engine limits: the backtrack limit.
  • Engine errors: the recursion limit and the JIT stack limit.
  • Environment variable: RECHECK_PHP.

The program runs with PHP's default settings of pcre.backtrack_limit, pcre.recursion_limit and pcre.jit.