#regex #regular-expression #magic #magic-regexp


A library for creating regular expressions with ease

5 releases

0.1.4 Mar 4, 2023
0.1.3 Feb 26, 2023
0.1.2 Feb 26, 2023
0.1.1 Feb 26, 2023
0.1.0 Feb 26, 2023

#5 in #magic

29 downloads per month

MIT license

235 lines


This library wants to simplify your regex-experience without the need to learn regex everytime you need to work with it. So it implements an easy to read syntactic. Also it can be used to work with the excellent regex crate. So you not have to implement random regex-strings anymore, instead you write native and readable rust-code.


The documentation can be found here.


To bring this crate into your repository, either add magic-regexp to your Cargo.toml, or run cargo add magic-regexp.

Here's a simple example that matches a date in YYYY-MM-DD format and prints the year, month and day:

use magic_regexp::{Digit, Times, create_reg_exp, Exactly, Condition, Text};
use regex::Regex;

fn main() {
    let input = Times(Digit, 4)
        .and(Times(Digit, 2))
        .and(Times(Digit, 2));
    assert_eq!(input.to_string(), r"\d{4}-\d{2}-\d{2}");

    let input = Times(Digit, 4)
        .and(Times(Digit, 2)
        .and(Times(Digit, 2)
    let re = create_reg_exp(input).unwrap();
    const TO_SEARCH: &'static str = "On 2010-03-14, foo happened. On 2014-10-14, bar happened.";
    assert_eq!(re.find_iter(TO_SEARCH).count(), 2);

    // works with lib regex like before
    for caps in re.captures_iter(TO_SEARCH) {
        // Note that all of the unwraps are actually OK for this regex
        // because the only way for the regex to match is if all of the
        // capture groups match. This is not true in general though!
        println!("year: {}, month: {}, day: {}",

Help wanted

This is my first rust library, I'm not sure if I'm doing everything right. So if you have any suggestions or find bugs, please let me know in the github issues.


This library is heavily inspired by magic-regexp.


~60K SLoC