All Projects → TaKO8Ki → regexm

TaKO8Ki / regexm

Licence: MIT license
A Rust macro for writing regex pattern matching.

Programming Languages

rust
11053 projects

Projects that are alternatives of or similar to regexm

librxvm
non-backtracking NFA-based regular expression library, for C and Python
Stars: ✭ 57 (+23.91%)
Mutual labels:  pattern-matching, regex
CVparser
CVparser is software for parsing or extracting data out of CV/resumes.
Stars: ✭ 28 (-39.13%)
Mutual labels:  pattern-matching, regex
simplematch
Minimal, super readable string pattern matching for python.
Stars: ✭ 147 (+219.57%)
Mutual labels:  pattern-matching, regex
Rosie Pattern Language
Rosie Pattern Language (RPL) and the Rosie Pattern Engine have MOVED!
Stars: ✭ 146 (+217.39%)
Mutual labels:  pattern-matching, regex
SwiftRegexDSL
A Declarative Structured Language for regular expressions in Swift.
Stars: ✭ 147 (+219.57%)
Mutual labels:  regex
FileDetectionRuleSets
🔎 Rules to detect game engines and other technologies based on Steam depot file lists
Stars: ✭ 106 (+130.43%)
Mutual labels:  regex
compiler-design-lab
These are my programs for compiler design lab work in my sixth semester
Stars: ✭ 47 (+2.17%)
Mutual labels:  regex
r4strings
Handling Strings in R
Stars: ✭ 39 (-15.22%)
Mutual labels:  regex
regex.ao
Coleção de expressões regulares mais populares!
Stars: ✭ 23 (-50%)
Mutual labels:  regex
grime
A language for matching two-dimensional patterns, based on Boolean grammars.
Stars: ✭ 13 (-71.74%)
Mutual labels:  pattern-matching
pcre-heavy
A Haskell regular expressions library that doesn't suck | now on https://codeberg.org/valpackett/pcre-heavy
Stars: ✭ 52 (+13.04%)
Mutual labels:  regex
regex-not
Create a javascript regular expression for matching everything except for the given string.
Stars: ✭ 31 (-32.61%)
Mutual labels:  regex
please
please, a sudo clone
Stars: ✭ 40 (-13.04%)
Mutual labels:  regex
match
Pattern-Matching written by Dan Friedman, Erik Hilsdale and Kent Dybvig
Stars: ✭ 20 (-56.52%)
Mutual labels:  pattern-matching
chemin
🥾 A type-safe pattern builder & route matching library written in TypeScript
Stars: ✭ 37 (-19.57%)
Mutual labels:  pattern-matching
Regaxor
A regular expression fuzzer.
Stars: ✭ 35 (-23.91%)
Mutual labels:  regex
assemblyscript-regex
A regex engine for AssemblyScript
Stars: ✭ 81 (+76.09%)
Mutual labels:  regex
doi-regex
Regular expression for matching DOIs
Stars: ✭ 28 (-39.13%)
Mutual labels:  regex
when-switch
JavaScript functional implementation of switch/case
Stars: ✭ 20 (-56.52%)
Mutual labels:  pattern-matching
extractacy
Spacy pipeline object for extracting values that correspond to a named entity (e.g., birth dates, account numbers, laboratory results)
Stars: ✭ 47 (+2.17%)
Mutual labels:  pattern-matching

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")
    };
}
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].