Python regex
Describes how recheck checks a regular expression of the python-regex dialect.
Engine
recheck follows the syntax and the semantics of the third-party regex package of Python (2026.11.19), not of the re module (see Python for re).
Settings
typeis the string type of the pattern and its input,str(default) orbytes.
- CLI
- JavaScript
- Scala
$ recheck --dialect=python-regex '/^(a|a)*$/'
$ recheck --dialect=python-regex --python-regex-type=bytes '/^(a|a)*$/'
import { check } from "recheck";
await check("^(a|a)*$", "", { dialect: "python-regex" });
await check("^(a|a)*$", "", {
dialect: { name: "python-regex", type: "bytes" },
});
import codes.quine.labs.recheck.ReDoS
import codes.quine.labs.recheck.common.Parameters
import codes.quine.labs.resyntax.dialect.Dialect
ReDoS.check("^(a|a)*$", "", Parameters(dialect = Dialect.PythonRegex()))
ReDoS.check("^(a|a)*$", "", Parameters(dialect = Dialect.PythonRegex(Dialect.Python.Type.Bytes)))
Flags and Units
The matching unit is code-point for a str pattern (the default type), and byte for a bytes pattern (see Matching Units).
recheck supports the following regex flags, written as their inline letters.
The version flags are two letters, V0 or V1 (e.g. iV1), and the version is V0 without them, as in regex.
| Flag | regex flag | Inline | Notes |
|---|---|---|---|
a | regex.ASCII (regex.A) | (?a) | An error with u or L. |
b | regex.BESTMATCH (regex.B) | (?b) | No effect, since fuzzy matching is not supported. |
e | regex.ENHANCEMATCH (regex.E) | (?e) | No effect, since fuzzy matching is not supported. |
f | regex.FULLCASE (regex.F) | (?f) | Folds a character to several characters under i (e.g. ß matches ss), unless a. |
i | regex.IGNORECASE (regex.I) | (?i) | |
L | regex.LOCALE (regex.L) | (?L) | The C locale. An error with a or u. |
m | regex.MULTILINE (regex.M) | (?m) | |
p | regex.POSIX (regex.P) | (?p) | Takes the longest match at a start position after trying every path there. |
r | regex.REVERSE (regex.R) | (?r) | Not supported. |
s | regex.DOTALL (regex.S) | (?s) | |
u | regex.UNICODE (regex.U) | (?u) | The default of a str pattern, and an error in a bytes pattern out of a group. |
w | regex.WORD (regex.W) | (?w) | Not supported with \b, \B, \m, \M, ., ^ or $. |
x | regex.VERBOSE (regex.X) | (?x) | |
V0 | regex.VERSION0 (regex.V0) | (?V0) | An error with V1. |
V1 | regex.VERSION1 (regex.V1) | (?V1) | Turns f on, and has the set operations and the nested sets in a class. |
As in regex, an inline flag in the middle of a pattern applies up to the end of its group, and b, e, p, r, V0 and V1 apply to the whole pattern wherever they are.
Syntax Limitations
The following constructs are not supported, and they are reported as unsupported.
- Recursion and calls of groups (e.g.
(?R),(?1)and(?&name)). - Conditionals (e.g.
(?(1)a|b),(?(?=a)a|b)and(?(DEFINE)...)). - Fuzzy constraints (e.g.
(?:abc){e<=1}). - Named lists (e.g.
\L<name>), whose strings are given on compiling the pattern. - The backtracking control verbs
(*PRUNE)and(*SKIP). - The flag
r(REVERSE), andw(WORD) with\b,\B,\m,\M,.,^or$. \N{...}(e.g.\N{EM DASH}), as in the other dialects having it.
Approximations
Lfollows the C locale, whatever locale the environment has.- Under
iwithforV1, a run of literals matches the folding of the characters only at the boundaries of the literals (e.g.ffidoes not match"ffi", andsßdoes not match"ßs"), and a class does not join the literals after it (e.g.[ff]idoes not match"ffi"). recheckdoes not reproduce the following behaviors ofregex.- An escape class or a property out of a group after an encoding flag (e.g.
\w(?a)) takes the flag, and a group without an encoding flag in a group with one (e.g.(?a:(?i:\w))) takes the encoding of the whole pattern;rechecktakes the encoding at the position of the escape or the property. \Xuses the encoding of the whole pattern (e.g.(?a:\X)is a grapheme cluster);recheckuses the encoding at its position.- Under
i, a class of only one property or escape class matches as the property alone (e.g.(?i)^[\p{Lu}]$matches"ĸ", but(?i)^[\p{Lu}x]$does not);rechecktakes it as a class. - Under
iwitha, a property of letter cases (e.g.\p{Ll},\p{Lu}and\p{Lowercase}) matches non-ASCII characters (e.g.(?ia)\p{Ll}matches"é", though(?a)\p{Ll}does not);recheckkeeps it within ASCII. - A character matching its folding to several characters can be matched in two ways (e.g.
^(?:ß)*$withiV1takes exponential time on"ß"repeated and"!");recheckmatches it in one way. - Under
L,\p{Cn}matches the characters up toU+00FF, and\p{Assigned}and\p{LC}match none. - In a
bytespattern,\777never matches, and\xDFmatchesssunderiwithV1;rechecktakes\777as\xFF, and folds no byte to several bytes. - A code point that
Bidi_ClassorEast_Asian_Widthhas no value for in the data ofregexhas the first value of the property (RorW);recheckfollows the defaults of the Unicode Character Database. - Without
V1,\g<1>in the group 1 is the literalg<1>;recheckreports it asinvalid, as\1there. VERSION0andVERSION1together raiseKeyError;recheckreports them asinvalid.
- An escape class or a property out of a group after an encoding flag (e.g.
- A repetition with a huge minimum count (e.g.
a{1000000}) takes long forregexto compile, which thetimeoutof the whole check stops.
Optimizations
recheck does not implement the following optimizations specific to regex, so an attack that recheck reports may not work on regex, and the runtime-attack verification checks whether it does.
- A repetition of a single character (e.g.
a+or\w*) remembers the positions where the rest of the pattern failed, which makes^(a+)+$and^(\w+\s?)*$fast. - A pattern fails at once on an input without a string that every match has (e.g.
^(?:a|a)*con an input withoutc).
Runtime-Attack Verification
- Command:
python3 -I -, with theregexpackage installed. - Matching:
searchof a compiled pattern. - Engine limits: none.
- Engine errors:
RecursionError,MemoryError. - Environment variable:
RECHECK_PYTHON_REGEX.
-I ignores PYTHONPATH and the user's site packages, so the Python needs the package in its own site packages, such as the Python of a virtual environment.
$ python3 -m venv .venv
$ .venv/bin/pip install regex
$ RECHECK_PYTHON_REGEX="$PWD/.venv/bin/python" recheck --dialect=python-regex '/^(a|a)*$/'
A Python without the package makes the runtime-attack verification fail with an unexpected error telling it.