lazy-regex
Use the regex!
macro to build regexes:
- they're checked at compile time
- they're wrapped in
once_cell
lazy static initializers so that they're compiled only once - they can hold flags as suffix:
let case_insensitive_regex = regex!("ab*"i);
- regex creation is less verbose
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!
Build Regexes
use regex;
// build a simple regex
let r = regex!;
assert_eq!;
// build a regex with flag(s)
let r = regex!;
assert_eq!;
// 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!;
assert_eq!;
// or a raw literal with flag(s)
let r = regex!;
assert_eq!;
// 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.
Test
use regex_is_match;
let b = regex_is_match!;
assert_eq!;
Extract
use regex_find;
let f_word = regex_find!;
assert_eq!;
Capture
use regex_captures;
let = regex_captures!.unwrap;
assert_eq!;
let = regex_captures!.unwrap;
assert_eq!;
assert_eq!;
assert_eq!;
The size of the tuple is checked at compile time and ensures you have the right number of capturing groups.