The regex crate searches UTF-8 text and byte slices with finite automata, favoring predictable worst-case time over features like look-around and backreferences.
Overview
regex is Rust’s mainstream crate for matching and searching text with regular expressions. It compiles patterns into finite automata and guarantees worst-case linear time in both pattern size and input length for searches it supports.
The supported syntax resembles common regex dialects but omits constructs that are hard to implement efficiently, including look-around and backreferences. The primary API works on valid UTF-8 strings; a parallel bytes API matches arbitrary byte slices and can disable Unicode semantics where invalid UTF-8 appears in haystacks.
You compile a Regex once and reuse it. Helpers include iterators over matches and capture groups, a RegexSet to test multiple patterns in one pass, optional compile-time regex! macro usage, and LazyLock-friendly patterns for functions. Related crates in the same project expose the parser, syntax analysis, and lower-level automata engines.
Cargo features let you tune binary size versus Unicode tables and optimizations. Benchmarks for the engine are maintained in a separate rebar harness for comparing regex implementations.
Key features
- Linear worst-case search time
- UTF-8 and byte matching APIs
- Captures and find iterators
- RegexSet multi-pattern matching
- regex! compile-time macro
- Configurable crate features
- regex-automata internals crate
- regex-syntax parser crate
Use cases
- Log and line parsing in Rust
- Validation and extraction pipelines
- Byte-oriented binary scanning
- Multi-pattern filtering
- Building custom regex tooling