regexm

Crates.ioregexm
lib.rsregexm
version0.2.1
sourcesrc
created_at2021-01-19 15:40:31.753764
updated_at2021-03-30 16:08:25.252262
descriptionA Rust macro for writing regex pattern matching.
homepagehttps://github.com/TaKO8Ki/regexm
repositoryhttps://github.com/TaKO8Ki/regexm
max_upload_size
id343991
size29,991
Takayuki Maeda (TaKO8Ki)

documentation

https://docs.rs/regexm

README

regexm

A Rust macro for writing regex pattern matching.

github workflow status crates docs

Usage | Examples | Docs

Features

  • Capture groups

Dependencies

[dependencies]
regex = "1"
regexm = "0.2.1"

Usage

Simple pattern matching

fn main() {
    let text1 = "2020-01-01";
    regexm::regexm!(match text1 {
        r"^\d{4}$" => println!("yyyy"),
        r"^\d{4}-\d{2}$" => println!("yyyy-mm"),
        // block
        r"^\d{4}-\d{2}-\d{2}$" => {
            let yyyy_mm_dd = "yyyy-mm-dd";
            println!("{}", yyyy_mm_dd);
        }
        _ => println!("default"),
    });
}

Output:

yyyy-mm-dd

the generated code will be the following:

fn main() {
    let text1 = "2020-01-01";
    if regex::Regex::new(r"^\d{4}$").unwrap().is_match(text1) {
        println!("yyyy")
    } else if regex::Regex::new(r"^\d{4}-\d{2}$").unwrap().is_match(text1) {
        println!("yyyy-mm")
    } else if regex::Regex::new(r"^\d{4}-\d{2}-\d{2}$")
        .unwrap()
        .is_match(text1)
    {
        let yyyy_mm_dd = "yyyy-mm-dd";
        println!("{}", yyyy_mm_dd);
    } else {
        println!("default")
    };
}

Let match

fn main() {
    let text2 = "foo";
    let foo = regexm::regexm!(match text2 {
        r"^\d{4}-\d{2}-\d{2}$" => "yyyy-mm-dd",
        r"^\d{4}-\d{2}$" => "yyyy-mm",
        // block
        r"^\d{4}-\d{2}-\d{2}$" => {
            let yyyy_mm_dd = "yyyy-mm-dd";
            yyyy_mm_dd
        }
        _ => "default",
    });
    println!("{}", foo);
}

Output:

default

the generated code will be the following:

fn main() {
    let text2 = "foo";
    let foo = if regex::Regex::new(r"^\d{4}-\d{2}-\d{2}$")
        .unwrap()
        .is_match(text2)
    {
        "yyyy-mm-dd"
    } else if regex::Regex::new(r"^\d{4}-\d{2}$").unwrap().is_match(text2) {
        "yyyy-mm"
    } else if regex::Regex::new(r"^\d{4}-\d{2}-\d{2}$")
        .unwrap()
        .is_match(text2)
    {
        let yyyy_mm_dd = "yyyy-mm-dd";
        yyyy_mm_dd
    } else {
        "default"
    };
    println!("{}", foo);
}

Capture Groups

fn main() {
    let text1 = "2020-01-02";
    regexm::regexm!(match text1 {
        // capture groups
        captures(r"^(\d{4})-(\d{2})-(\d{2})$") => |caps| println!(
            "year: {}, month: {}, day: {}",
            caps.get(1).map_or("", |m| m.as_str()),
            caps.get(2).map_or("", |m| m.as_str()),
            caps.get(3).map_or("", |m| m.as_str())
        ),
        _ => println!("default"),
    });
}

Output:

2020
01
02

the generated code will be the following:

fn main() {
    let text1 = "2020-01-02";
    if regex::Regex::new(r"^(\d{4})-(\d{2})-(\d{2})$")
        .unwrap()
        .is_match(text1)
    {
        let closure = |caps: regex::Captures| {
            println!(
                "year: {}, month: {}, day: {}",
                caps.get(1).map_or("", |m| m.as_str()),
                caps.get(2).map_or("", |m| m.as_str()),
                caps.get(3).map_or("", |m| m.as_str())
            )
        };
        closure(
            regex::Regex::new(r"^(\d{4})-(\d{2})-(\d{2})$")
                .unwrap()
                .captures(text1)
                .unwrap(),
        )
    } else {
        println!("default")
    };
}

Commit count: 52

cargo fmt