With lazy-regex macros, regular expressions
once_cell
lazy static initializers so that they're compiled only oncelet case_insensitive_regex = regex!("ab*"i);
The regex!
macro returns references to normal instances of regex::Regex
or regex::bytes::Regex
so all the usual features are available.
Other macros are specialized for testing a match, replacing with concise closures, or capturing groups as substrings in some common situations:
regex_is_match!
regex_find!
regex_captures!
regex_replace!
regex_replace_all!
All of them support the B
flag for the regex::bytes::Regex
variant.
Some structs of the regex crate are reexported to ease dependency managment.
```rust use lazy_regex::regex;
// build a simple regex let r = regex!("sa+$"); asserteq!(r.ismatch("Saa"), false);
// build a regex with flag(s) let r = regex!("sa+$"i); asserteq!(r.ismatch("Saa"), true);
// you can use a raw literal let r = regex!(r#"^"+$"#); asserteq!(r.ismatch("\"\""), true);
// or a raw literal with flag(s) let r = regex!(r#"^\s("[a-t]"\s*)+$"#i); asserteq!(r.ismatch(r#" "Aristote" "Platon" "#), true);
// build a regex that operates on &[u8] let r = regex!("(byte)?string$"B); asserteq!(r.ismatch(b"bytestring"), true);
// there's no problem using the multiline definition syntax
let r = regex!(r#"(?x)
(?P
compile_fail
// this line doesn't compile because the regex is invalid:
let r = regex!("(unclosed");
``
Supported regex flags:
i,
m,
s,
x,
U`.
See regex::RegexBuilder.
```rust use lazyregex::regexis_match;
let b = regexismatch!("[ab]+", "car"); assert_eq!(b, true); ```
```rust use lazyregex::regexfind;
let fword = regexfind!(r#"\bf\w+\b"#, "The fox jumps."); asserteq!(fword, Some("fox")); let fword = regexfind!(r#"\bf\w+\b"#B, b"The forest is silent."); asserteq!(fword, Some(b"forest" as &[u8])); ```
```rust use lazyregex::regexcaptures;
let (, letter) = regexcaptures!("([a-z])[0-9]+"i, "form A42").unwrap(); assert_eq!(letter, "A");
let (whole, name, version) = regexcaptures!( r#"(\w+)-([0-9.]+)"#, // a literal regex "This is lazyregex-2.0!", // any expression ).unwrap(); asserteq!(whole, "lazyregex-2.0"); asserteq!(name, "lazyregex"); assert_eq!(version, "2.0"); ```
There's no limit to the size of the tuple. It's checked at compile time to ensure you have the right number of capturing groups.
You receive ""
for optional groups with no value.
The [regexreplace!] and [regexreplace_all!] macros bring once compilation and compilation time checks to the replace
and replace_all
functions.
```rust use lazyregex::regexreplace_all;
let text = "Foo8 fuu3";
let text = regexreplaceall!(
r#"\bf(\w+)(\d)"#i,
text,
|, name, digit| format!("F<{}>{}", name, digit),
);
asserteq!(text, "F
If it doesn't match you get, at compilation time, a clear error message.
rust
use lazy_regex::regex_replace_all;
let text = "UwU";
let output = regex_replace_all!("U", text, "O");
assert_eq!(&output, "OwO");
When a regular expression is used in several functions, you sometimes don't want to repeat it but have a shared static instance.
The regex!
macro, while being backed by a lazy static regex, returns a reference.
If you want to have a shared lazy static regex, use the lazy_regex!
macro:
```rust use lazy_regex::*;
pub static GLOBALREX: Lazy
Like for the other macros, the regex is static, checked at compile time, and lazily built at first use.