Regex is useful. But, sincerely, since code it is more read than written regex could be more understandable in a verbose mode.
readable-regex
crate is a set of tools to build those regexes in a verbose way. Which aims
to improve readability of code.
It builds on top on the already excellent regex
and fancy-regex
crates.
Add the dependency to your Cargo.toml
file:
toml
readable-regex = "0.1.0"
The main wrapper is the ReadableRe
enum.
There are tow main options to build regexes with it, either using the enum itself:
rust
use readable_regex::ReadableRe;
let query = ReadableRe::Raw("<some regex expression>");
or by using the functions wrappers around it:
rust
use readable_regex::raw_regex;
let query = raw_regex("<some regex expression>");
Also, any combination of them:
rust
use readable_regex::{digit, group};
use readable_regex::ReadableRe::*;
let query = group(digit() + Digit + digit());
println!("{}", query.to_string());
How to build a simple date match (as implemented in the datetime
module under presets
feature):
```
use oncecell::sync::Lazy;
use readableregex::;
use readable_regex::ReadableRe::;
/// Month day, 01
-31
pub const DAY: Lazy
/// Month numeral, 01
-12
pub const MONTH: Lazy
/// Years from 1000
to 2999
pub const YEAR: Lazy
/// Date Format YYYY-MM-dd
pub const DATEYM_D: Lazy
assert!(DATEYMD.compile().unwrap().ismatch("2022/04/18")); ```
This library was highly inspired by the python Humre
package.