use once_cell::sync::Lazy;
use regex::Regex;
static THE_REGEX: Lazy<Regex> = Lazy::new(|| Regex::new(r"[0-9]{1,2}-[0-9]{1}-[0-9]{1,4}").unwrap());
fn main() {
let some_string: &str = "\
You have 2 parcels to Packages Station.
Please with 10-1-3125, 1-5-4453 pick
them up before 17:00.";
for i in THE_REGEX.find_iter(some_string) {
dbg!(i.as_str());
}
}
The usage of the static Lazy<…> demonstrates a common pattern that’s useful when the same regex is needed in a function that’s called multiple times. It helps avoid the overhead of building the same regex multiple times. In this example in the main function, it is not really necessary, but I put it in to demonstrate the pattern.
The {1,4} means one up to four repititions, more details in the docs. The {1} is redundant, I should’ve probably just removed it.