#macro #lazy-static #regex

lazy-regex

a macro to reduce regex creation boilerplate

6 releases (1 stable)

new 1.0.0 May 4, 2021
0.1.4 Jul 10, 2020
0.1.3 May 18, 2020
0.1.2 Oct 7, 2019

#72 in Text processing

Download history 231/week @ 2021-01-14 184/week @ 2021-01-21 182/week @ 2021-01-28 306/week @ 2021-02-04 363/week @ 2021-02-11 198/week @ 2021-02-18 413/week @ 2021-02-25 306/week @ 2021-03-04 249/week @ 2021-03-11 195/week @ 2021-03-18 177/week @ 2021-03-25 131/week @ 2021-04-01 151/week @ 2021-04-08 222/week @ 2021-04-15 315/week @ 2021-04-22 348/week @ 2021-04-29

1,121 downloads per month
Used in 5 crates

MIT license

3KB

lazy-regex

a macro for when you're tired of the regex creation boilerplate

What it does

It's a shortcut to write static lazily compiled regular expressions as is usually done with lazy_static or once_cell.

It lets you replace

fn some_helper_function(text: &str) -> bool {
    lazy_static! {
        static ref RE: Regex = Regex::new("...").unwrap();
    }
    RE.is_match(text)
}

with

fn some_helper_function(text: &str) -> bool {
    regex!("...").is_match(text)
}

The first code comes from the regex documentation.

FAQ

Is it really useful ?

Regarding the binary, it's exactly as using lazy_static. It just makes some code a little easier to read. You're judge.

Can I have several regex! in the same function ? On the same Line ?

Yes, no problem.

It hides the unwrap(), isn't it concerning ?

Not so much in my opinion as the macro only accepts a litteral: you won't hide a failure occuring on a dynamic string.

I'd like to have flags too

You mean something like regex!("somestring", "i") ? Cool. I was just waiting for somebody's else to ask for it. Create an issue and I'll see if I can easily wrap RegexBuilder to handle flags ala JavaScript.

What's the licence ?

It's MIT. But you're also free to just copy the macro in your code if it's simpler, no attribution is needed.

Dependencies

~13KB