#macro #regex #lazy_static

lazy-regex

a macro to reduce regex creation boilerplate

3 releases

✓ Uses Rust 2018 edition

new 0.1.2 Oct 7, 2019
0.1.1 Oct 7, 2019
0.1.0 Oct 7, 2019

#132 in Text processing

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

~9KB