A Rust macro for writing regex pattern matching.

Overview

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.1"

If you want to use Capture groups feature, please use regexm = "0.2.0-beta".

Usage

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
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
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
You might also like...
Rust macro that uses GPT3 codex to generate code at compiletime

gpt3_macro Rust macro that uses GPT3 codex to generate code at compiletime. Just describe what you want the function to do and (optionally) define a f

No-nonsense input!(...) macro for Rust

No-nonsense input!(...) macro for Rust

Yet another geter/setter derive macro.

Gusket Gusket is a getter/setter derive macro. Comparison with getset: gusket only exposes one derive macro. No need to derive(Getters, MutGetters, Se

A proc-macro to get Vecu8 from struct and vise versa

byteme A proc-macro to convert a struct into Vec and back by implemeting From trait on the struct. The conversion is Big Endian by default. We have ma

Derive macro for encoding/decoding instructions and operands as bytecode

bytecoding Derive macro for encoding and decoding instructions and operands as bytecode. Documentation License Licensed under either of Apache License

Derive macro implementing 'From' for structs

derive-from-ext A derive macro that auto implements 'std::convert::From' for structs. The default behaviour is to create an instance of the structure

A proc macro for creating compile-time checked CSS class sets, in the style of classNames

semester Semester is a declarative CSS conditional class name joiner, in the style of React's classnames. It's intended for use in web frameworks (lik

Proc. macro to generate C-like `enum` tags.

Continuous Integration Documentation Crates.io #[derive(EnumTag)] This crate provides a proc. macro to derive the EnumTag trait for the given Rust enu

k-mer counter in Rust using the rust-bio and rayon crates

krust is a k-mer counter written in Rust and run from the command line that will output canonical k-mers and their frequency across the records in a f

Comments
  • Capture groups

    Capture groups

    https://www.reddit.com/r/rust/comments/l1401a/regexm_a_rust_macro_for_writing_regex_pattern/gjxbsui?utm_source=share&utm_medium=web2x&context=3

    regexm::regexm!(match text1 {
        r"^\d{4}$" => println!("y"),
        // capture groups
        capture r"^\d{4}-(\d{2})$" => |caps| println!("{}", caps.get(1).map_or("", |m| m.as_str())),
        // block
        r"^\d{4}-\d{2}-\d{2}$" => {
            let y_m_d = "y-m-d";
            println!("{}", y_m_d);
        }
        _ => println!("default"),
    });
    
    opened by TaKO8Ki 0
  • Reuse pre compiled expression

    Reuse pre compiled expression

    It would be great if the macro would initialize a regexp in a static variable and reuse the pre-compiled expression, instead of doing it each time.

    https://docs.rs/regex/1.4.5/regex/#example-avoid-compiling-the-same-regex-in-a-loop

    Ref: https://www.reddit.com/r/rust/comments/mgdvmp/regexm_v020_a_rust_macro_for_writing_regex/gsvx7xl?utm_source=share&utm_medium=web2x&context=3

    help wanted 
    opened by TaKO8Ki 0
Releases(v0.2.1)
Owner
Takayuki Maeda
Software Engineer?
Takayuki Maeda
The axiom profiler for exploring and visualizing SMT solver quantifier instantiations (made via E-matching).

Axiom Profiler A tool for visualising, analysing and understanding quantifier instantiations made via E-matching in a run of an SMT solver (at present

Viper Project 18 Oct 18, 2022
Propositional logic evaluator and rule-based pattern matcher

Plogic Propositional logic evaluator and pattern transformer written in Rust. Plogic evaluates logic expressions in a REPL (Read, Execute, Print, Loop

Jan 17 Nov 25, 2022
A partial actor pattern with a global orchestrator.

orchestra The orchestra pattern is a partial actor pattern, with a global orchestrator regarding relevant work items. proc-macro The proc macro provid

Parity Technologies 12 Dec 9, 2022
Calcit binding to Rust regex

Rust library for Calcit runtime.

Calcit 1 Nov 19, 2021
This crate allows writing a struct in Rust and have it derive a struct of arrays layed out in memory according to the arrow format.

Arrow2-derive - derive for Arrow2 This crate allows writing a struct in Rust and have it derive a struct of arrays layed out in memory according to th

Jorge Leitao 29 Dec 27, 2022
A rust library that makes reading and writing memory of the Dolphin emulator easier.

dolphin-memory-rs A crate for reading from and writing to the emulated memory of Dolphin in rust. A lot of internals here are directly based on aldela

Madison Barry 4 Jul 19, 2022
A low-ish level tool for easily writing and hosting WASM based plugins.

A low-ish level tool for easily writing and hosting WASM based plugins. The goal of wasm_plugin is to make communicating across the host-plugin bounda

Alec Deason 62 Sep 20, 2022
Macro for Python-esque comprehensions in Rust

Cute Macro for Python-esque list comprehensions in Rust. The c! macro implements list and hashmap comprehensions similar to those found in Python, all

Matt Gathu 306 Jan 6, 2023
Rust crate that provides a convenient macro to quickly plot variables.

Debug Plotter This crate provides a convenient macro to quickly plot variables. Documentation For more information on how to use this crate, please ta

Fabian Bösiger 82 Dec 31, 2022
Rust macro to make recursive function run on the heap (i.e. no stack overflow).

Decurse Example #[decurse::decurse] // ?? Slap this on your recursive function and stop worrying about stack overflow! fn factorial(x: u32) -> u32 {

Wisha W. 18 Dec 28, 2022