#macro #regex #lazy_static

lazy-regex

a macro to reduce regex creation boilerplate

4 releases

✓ Uses Rust 2018 edition

new 0.1.3 May 18, 2020
0.1.2 Oct 7, 2019
0.1.1 Oct 7, 2019
0.1.0 Oct 7, 2019

#144 in Text processing

Download history 330/week @ 2020-01-25 656/week @ 2020-02-01 409/week @ 2020-02-08 357/week @ 2020-02-15 281/week @ 2020-02-22 162/week @ 2020-02-29 262/week @ 2020-03-07 218/week @ 2020-03-14 133/week @ 2020-03-21 217/week @ 2020-03-28 233/week @ 2020-04-04 151/week @ 2020-04-11 116/week @ 2020-04-18 146/week @ 2020-04-25 175/week @ 2020-05-02 155/week @ 2020-05-09

830 downloads per month
Used in 2 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

~12KB