Skip to main content

RE2 in pure Python: linear-time, ReDoS-safe regular expressions - no C extension, no backtracking

Project description

purere2

CI PyPI Python RE2 conformance License: MIT

RE2 in pure Python: linear-time, ReDoS-safe regular expressions — no C extension, no backtracking.

Python's built-in re (like PCRE and Perl) backtracks, so a pattern like (a+)+$ against a non-matching string can run for years on a few dozen characters — the classic ReDoS denial-of-service. purere2 compiles every pattern to an NFA and runs it with a Pike VM, so matching is always linear in the input and no pattern can blow up. That guarantee is exactly why RE2 exists — and why it has no backreferences or lookaround.

pip install purere2
import purere2

# linear time: this returns instantly; re.search would hang for minutes
purere2.search(r"(a+)+$", "a" * 50 + "!")          # None, in microseconds

purere2.search(r"(\w+)@(\w+)", "x@y").groups()      # ('x', 'y')
purere2.findall(r"\d{4}-\d\d-\d\d", "2026-06-19")   # ['2026-06-19']

The API mirrors the common subset of the stdlib re module (compile, search, match, fullmatch, finditer, findall, sub, subn, split, flags I/M/S, named groups), so it is close to a drop-in replacement for running untrusted or LLM-generated patterns safely.

Why pure Python

google-re2 and pyre2 already wrap RE2 — but they need the RE2 C++ library (and a compiler, or a matching binary wheel). There was no pure Python RE2, even though RE2/J (Java) and RE2JS (JavaScript) have existed for years. purere2 is the missing one: zero dependencies, zero binaries, runs anywhere Python runs — Pyodide/WASM, AWS Lambda, locked-down sandboxes — exactly where you most want to run a pattern you don't trust.

The trade-off, stated honestly: on ordinary patterns purere2 is slower than the C-backed re (a pure-Python NFA can't beat a C engine). Its value is safety and portability, not raw speed — use it where a pattern is untrusted, or where a C extension isn't an option, not as a blanket re replacement.

Verified against the real RE2

Conformance is differential, the same way purefzf checks itself against the fzf binary: random RE2 patterns and inputs are run through both purere2 and google-re2 and compared byte-for-byte. Across 150,000+ random checks, agreement is ~99.996%; the residue is one documented edge (a lazy quantifier nested in a greedy loop) — see EXPECTED_DIVERGENCES.md. The conformance test locks that level, so any regression fails CI. There is also a ReDoS-safety suite of patterns that hang stdlib re and must finish in milliseconds here.

Supported syntax (v0.1)

Literals, ., character classes [...] with ranges / negation / POSIX [[:alpha:]], perl classes \d \w \s (ASCII, per RE2) and negations, anchors ^ $ \A \z \b \B, groups (...) / (?:...) / (?P<name>...), alternation |, quantifiers * + ? {m} {m,n} greedy and lazy, inline flags (?i) (?m) (?s) and scoped (?i:...), escapes including \xHH / \x{...}.

Intentionally absent (this is what makes it safe): backreferences and lookaround. (a)\1 raises RegexError. Deferred to a later version: Unicode property classes \p{...} and full Unicode case folding.

License

MIT

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

purere2-0.1.1.tar.gz (19.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

purere2-0.1.1-py3-none-any.whl (16.0 kB view details)

Uploaded Python 3

File details

Details for the file purere2-0.1.1.tar.gz.

File metadata

  • Download URL: purere2-0.1.1.tar.gz
  • Upload date:
  • Size: 19.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for purere2-0.1.1.tar.gz
Algorithm Hash digest
SHA256 1644de804714357be562cf39fea24481824a48789de2c89aee1a29cc963a9d28
MD5 b1706236b083a2490c77720b593f3d05
BLAKE2b-256 2d13ba3c5e3feff05b1b92f046db76de33baefca4912d85fd6a4a4e517d9e786

See more details on using hashes here.

Provenance

The following attestation bundles were made for purere2-0.1.1.tar.gz:

Publisher: release.yml on adam2go/purere2

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file purere2-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: purere2-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 16.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for purere2-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 5ddf8c245a23294032b2822edaa309bfb0e81cc7fba6074dc50cb63de371114c
MD5 8eb20aac5a0d17bf3fe892880d07b301
BLAKE2b-256 c2956b91e794989af784aaa63892eb35af59a9f0ce8acf88e2871c753511512a

See more details on using hashes here.

Provenance

The following attestation bundles were made for purere2-0.1.1-py3-none-any.whl:

Publisher: release.yml on adam2go/purere2

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page