ezregexp

Crates.ioezregexp
lib.rsezregexp
version0.0.1
sourcesrc
created_at2020-12-23 15:05:10.309955
updated_at2020-12-23 15:05:10.309955
descriptionA fluent API to build and understand regular expressions
homepagehttps://github.com/JPMoresmau/ezregexp
repositoryhttps://github.com/JPMoresmau/ezregexp
max_upload_size
id326533
size30,200
JP Moresmau (JPMoresmau)

documentation

README

ezregexp

A Rust library to build regular expressions using a human-friendly fluent API.

Usage

Add this to your Cargo.toml:

[dependencies]
ezregexp = "0.0.1"

and this to your crate root (if you're using Rust 2015):

extern crate ezregexp;

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

use ezregexp::{start_with, digit};
use regex::Regex;

fn main() {
    let p =start_with(digit().times(4).named("year"))
            .and_then("-")
            .and_then(digit().times(2).named("month"))
            .and_then("-")
            .and_then(digit().times(2).named("day"))
            .to_string();
    let re = Regex::new(&p.to_string()).unwrap();
    let caps = re.captures("2010-03-14").unwrap();

    assert_eq!("2010", &caps["year"]);
    assert_eq!("03", &caps["month"]);
    assert_eq!("14", &caps["day"]);
}

You can also use the library to generate the API calls from a regular expression:

use ezregexp::{ToCode,explain};
use regex::Regex;

fn main() {
    let s = r"(?x)
    (?P<year>\d{4})  # the year
    -
    (?P<month>\d{2}) # the month
    -
    (?P<day>\d{2})   # the day
    ";
    let re = Regex::new(s).unwrap();
    let caps = re.captures("2010-03-14").unwrap();

    assert_eq!("2010", &caps["year"]);
    assert_eq!("03", &caps["month"]);
    assert_eq!("14", &caps["day"]);

    println!("{}",explain(s).unwrap().to_code());
}

Commit count: 7

cargo fmt