lazy-regex
Rust macros for regular expressions checked at compile time and compiled once via lazy statics.
Repository Health
Technical Analysis
lazy-regex provides a set of Rust macros that make working with regular expressions safer and less verbose. Patterns passed to its macros are validated at compile time with clear error messages, and each regex is wrapped in a once_cell lazy static so it is compiled only once regardless of how often the surrounding code runs.
The regex! macro returns a reference to a normal regex::Regex (or regex::bytes::Regex), so every feature of the underlying regex crate remains available. On top of it, specialized macros — regex_is_match!, regex_find!, regex_captures!, regex_replace!, regex_switch!, and more — cover the most common matching, extraction, replacement, and dispatch patterns with minimal boilerplate.
What You Get
- The
regex!macro returning a lazily-compiled, compile-time-checkedRegexreference - Match and extract helpers:
regex_is_match!,regex_find!,regex_captures!,regex_captures_iter! - Replacement helpers:
regex_replace!,regex_replace_all!,regex_remove!,regex_remove_all! regex_switch!for dispatching over multiple patterns like a match expression- Inline flags as a suffix (e.g.
regex!("ab*"i)) for case-insensitive and other modes bytes_-prefixed variants for buildingregex::bytes::Regexover byte strings
Common Use Cases
- Defining reusable, compile-time-validated regexes without manual lazy-static boilerplate
- Capturing groups and extracting substrings concisely with the capture macros
- Branching over several patterns with
regex_switch!in parsers and tokenizers
Under The Hood
Architecture — The crate is split between a thin runtime library (src/lib.rs, src/remove.rs) and a procedural-macro crate (src/proc_macros/) that parses the pattern and optional flags at compile time, reports errors, and expands to a once_cell::Lazy wrapping a regex::Regex. The specialized macros expand into calls on that lazily-initialized regex, so regex_captures!, regex_replace!, and friends are zero-cost conveniences over the standard regex API.
Tech Stack — Rust (edition 2021, MSRV 1.71) built on the regex crate and once_cell, with an optional regex-lite backend via the lite feature. A large set of feature flags forwards the regex crate’s perf-* and unicode-* options so you can tune build size and capabilities.
Code Quality — A focused, mature crate with a comprehensive integration test suite (tests/replace.rs, tests/remove.rs, tests/regex_switch.rs, tests/regex_if.rs) and runnable examples. The compile-time validation in the proc-macro crate is the core quality feature, turning would-be runtime panics into build errors.
API Design — The macros are highly ergonomic and read like built-in language constructs: regex_is_match!("^\\d+$", s) or let (_, y, m, d) = regex_captures!(...). Because regex! yields a plain Regex reference, there is nothing new to learn about the matching API itself — the macros only remove the lazy-static and validation boilerplate.