Crates.io | ezregexp |
lib.rs | ezregexp |
version | 0.0.1 |
source | src |
created_at | 2020-12-23 15:05:10.309955 |
updated_at | 2020-12-23 15:05:10.309955 |
description | A fluent API to build and understand regular expressions |
homepage | https://github.com/JPMoresmau/ezregexp |
repository | https://github.com/JPMoresmau/ezregexp |
max_upload_size | |
id | 326533 |
size | 30,200 |
A Rust library to build regular expressions using a human-friendly fluent API.
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());
}