Use the regex!
macro to build regexes:
once_cell
lazy static initializers so that they're compiled only oncelet case_insensitive_regex = regex!("ab*"i);
This macro builds normal instances of regex::Regex
so all the usual features are available.
You may also use shortcut macros for testing a match or capturing groups as substrings:
regex_is_match!
regex_find!
regex_captures!
```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);
// supported regex flags: 'i', 'm', 's', 'x', 'U' // see https://docs.rs/regex/1.5.4/regex/struct.RegexBuilder.html
// 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);
// this line wouldn't compile: // let r = regex!("(unclosed");
```
What you really get from this macro call is a reference to a regex::Regex
, statically checked, and behind a static once_cell
lazy initializer.
```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")); ```
```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"); ```
The size of the tuple is checked at compile time and ensures you have the right number of capturing groups.