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.0.tar.gz (18.4 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.0-py3-none-any.whl (14.7 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: purere2-0.1.0.tar.gz
  • Upload date:
  • Size: 18.4 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.0.tar.gz
Algorithm Hash digest
SHA256 c5f94426c592440dac6c188495e4160e83362a4217c08b4b21780c283cc70fdd
MD5 e2dc6fae690bfdca9a5e899f41bb91aa
BLAKE2b-256 1ae48dca19d7ff30a9846328ddc242c6ed34c4d16cf5ada0deac65b121eaf67f

See more details on using hashes here.

Provenance

The following attestation bundles were made for purere2-0.1.0.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.0-py3-none-any.whl.

File metadata

  • Download URL: purere2-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 14.7 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.0-py3-none-any.whl
Algorithm Hash digest
SHA256 7422490e21795dce6bdce18286864a587a175492b1443bf53885477d7da66ed9
MD5 294449b7ea2a8df8b0295b425846ff4b
BLAKE2b-256 401a833a4d75b562fb94b3c1a926d6f97cdf76410a90a3d930210e6c0e5856ed

See more details on using hashes here.

Provenance

The following attestation bundles were made for purere2-0.1.0-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