fancy-regex
A Rust regex library supporting backreferences and look-around via hybrid backtracking
Repository Health
Technical Analysis
fancy-regex extends Rust’s regex ecosystem with “fancy” features that pure NFA-based engines (like the regex crate or RE2) can’t support: backreferences and look-around assertions. It does this with a hybrid engine that runs the fast NFA-based regex-automata/regex-syntax machinery for the parts of a pattern that support it, and falls back to a backtracking VM only for the fancy constructs that require it — keeping common patterns fast while still enabling the richer syntax many developers expect from Perl/PCRE-style regex engines. It also supports an Oniguruma-compatible syntax mode for closer compatibility with regex dialects used by tools like Textmate/Sublime grammars.
The project ships an in-browser playground for experimenting with fancy regex features interactively, an extensive tests/ suite (including dedicated Oniguruma compatibility tests and byte-oriented tests), fuzzing targets, and criterion-based benchmarks comparing hybrid vs. individual regex-set matching performance — reflecting a deliberate focus on both correctness and the performance tradeoffs backtracking engines are known for.
What You Get
- Full backreference and look-around (lookahead/lookbehind) support, absent from purely NFA-based engines like
regex - A hybrid engine: NFA-fast-path matching for standard constructs, backtracking VM only for fancy features
- An optional Oniguruma-compatible syntax mode for closer parity with Textmate/Sublime-style grammar engines
- Variable-length lookbehind support via reverse DFA search (feature-gated)
- An interactive browser playground plus criterion benchmarks and a fuzzing harness for regression safety
Common Use Cases
- Parsing or validating text where a regex needs backreferences (e.g. matching repeated/balanced tokens) unsupported by linear-time engines
- Implementing syntax-highlighting or grammar engines that must stay compatible with Oniguruma-based
.tmLanguage/Textmate grammar files - Building developer tools (linters, formatters, search) that need look-around assertions for context-sensitive matching
- Replacing a slower general-purpose backtracking regex implementation with one that still runs fast-path matching for the non-fancy majority of a pattern
Under The Hood
Architecture - src/parse.rs (4,423 lines) parses a fancy-regex pattern into an internal AST, src/analyze.rs (1,999 lines) determines which sub-expressions can be delegated to the fast NFA path versus which require backtracking, src/compile.rs (1,972 lines) compiles the backtracking portion into bytecode for the custom src/vm.rs (1,489 lines) virtual machine, and src/optimize.rs applies pre-execution optimizations; src/regexset.rs (989 lines) layers multi-pattern matching on top of the same core, and src/seek.rs/src/input.rs abstract over string vs. byte-slice input for the separate bytes module.
Tech Stack - Pure Rust (edition 2018, MSRV 1.66) built directly on the regex-automata and regex-syntax crates (feature-gated to alloc/syntax/meta/nfa/dfa/hybrid for the fast path) plus bit-set for compact set tracking during backtracking; feature flags (unicode, perf, std, variable-lookbehinds) let consumers opt in/out of Unicode tables and reverse-DFA-based variable-length lookbehind support.
Code Quality - The tests/ directory (16 files, including dedicated oniguruma/ and bytes/ subdirectories) exercises Oniguruma-compatibility edge cases and byte-oriented matching separately from the standard str-based API, backed by quickcheck property tests, a fuzz/ harness with dedicated fuzz targets, and criterion benchmarks (bench, regexset_vs_individual) that explicitly track the performance cost of the hybrid/backtracking approach.
API Design - The public API deliberately mirrors the standard regex crate’s Regex/Captures/RegexSet types and method names, so switching from regex to fancy-regex for the subset of patterns needing backreferences or look-around requires minimal code changes; the interactive playground and docs.rs documentation lower the learning curve for the fancy-syntax additions, though understanding when patterns fall back to (potentially slower) backtracking still requires some awareness of the hybrid execution model.
Used by 3 apps in this directory
AppFlowy
Productivity · Project Management · Collaboration
The open-source AI workspace that puts your data, your rules — with local LLMs, CRDT collaboration, and full self-hosting built in.
Laminar
AI Development · Monitoring
Open-source observability platform purpose-built for AI agents — trace, evaluate, debug, and monitor at scale with SQL access and real-time replay.
monty
AI Development · Developer Tools
Run LLM-generated Python code safely inside your agent—no containers, no CPython, no compromise—with sub-microsecond startup.