regex/
lib.rs

1pub use regex_inner::*;
2
3#[macro_export]
4macro_rules! regex {
5    ($re:expr) => {
6        {
7            static RE: std::sync::LazyLock<$crate::Regex> = std::sync::LazyLock::new(|| $crate::Regex::new($re).unwrap());
8            &*RE
9        }
10    };
11    ($re:expr, $flags:literal) => {
12        // use regex builder
13        {
14            static RE: std::sync::LazyLock<$crate::Regex> = std::sync::LazyLock::new(|| {
15                let mut r = $crate::RegexBuilder::new($re.as_ref());
16                for f in $flags.chars() {
17                    match f {
18                        'i' => r.case_insensitive(true),
19                        'm' => r.multi_line(true),
20                        _ => panic!("Invalid regex flag: {}", f),
21                    };
22                }
23                r.build().unwrap()
24            });
25            &*RE
26        }
27    };
28}
29
30#[cfg(test)]
31mod tests {
32    use super::*;
33    #[test]
34    fn test_regex() {
35        let re = regex!(r#"pay\sto\sthe\sorder\sof"#);
36        assert_eq!(re.is_match("pay to the order of"), true);
37        let re = regex!(r#"pay\sto\sthe\sorder\sof"#, "i");
38        assert_eq!(re.is_match("PAY TO THE ORDER OF"), true);
39        assert_eq!(re.is_match("PAY TO THE "), false);
40
41    }
42}