Expand description
§Regex for human beings.
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.
§Available APIs
The main wrapper is the ReadableRe
enum.
There are tow main options to build regexes with it, either using the enum itself:
use readable_regex::ReadableRe;
let query = ReadableRe::Raw("<some regex expression>");
or by using the functions wrappers around it:
use readable_regex::raw_regex;
let query = raw_regex("<some regex expression>");
Also, any combination of them:
use readable_regex::{digit, group};
use readable_regex::ReadableRe::*;
let query = group(digit() + Digit + digit());
println!("{}", query.to_string());
§Examples
How to build a simple date match (as implemented in the datetime
module under presets
feature):
use once_cell::sync::Lazy;
use readable_regex::*;
use readable_regex::ReadableRe::*;
/// Month day, `01`-`31`
pub const DAY: Lazy<ReadableRe> = Lazy::new(|| {
either([
Raw("0") + chars("1-9"),
chars("12") + chars("1-9"),
Raw("3") + chars("01"),
])
});
/// Month numeral, `01`-`12`
pub const MONTH: Lazy<ReadableRe> =
Lazy::new(|| either([Raw("0") + chars("1-9"), Raw("1") + chars("0-2")]));
/// Years from `1000` to `2999`
pub const YEAR: Lazy<ReadableRe> = Lazy::new(|| chars("12") + exactly(3, Digit));
/// Date Format `YYYY-MM-dd`
pub const DATE_Y_M_D: Lazy<ReadableRe> = Lazy::new(|| {
group(
group(YEAR.clone())
+ chars(r"-/.\\")
+ group(MONTH.clone())
+ chars(r"-/.\\")
+ group(DAY.clone()),
)
});
assert!(DATE_Y_M_D.compile().unwrap().is_match("2022/04/18"));
§Features
re
=> Useregex
crate backend.re-fancy
=> Use [fancy_regex
] crate backend and expands this crate functionality.
Re-exports§
pub use readable::ReadableRe;
Modules§
Functions§
- any_
char - anything
- ascii_
alphanumeric - ascii_
letter - ascii_
lowercase - ascii_
non_ alphanumeric - ascii_
non_ letter - ascii_
non_ lowercase - ascii_
non_ numeric - ascii_
non_ uppercase - ascii_
numeric - ascii_
uppercase - asterisk
- at_
least - at_most
- back_
slash - boundary
- caret
- chars
- close_
brace - close_
bracket - close_
parenthesis - concat
- digit
- dollar
- double_
quote - either
- ends_
with - escape
- escape_
str - everything
- exactly
- group
- hexadecimal
- minus_
sign - named_
group - new_
line - non_
capture_ group - non_
digit - non_
hexadecimal - non_
whitespace - non_
word - not_
chars - one_
or_ more - one_
or_ more_ lazy - open_
brace - open_
bracket - open_
parenthesis - optional
- period
- pipe
- plus_
sign - question_
mark - quote
- ranged
- raw_
regex - something
- something_
greedy - starts_
and_ ends_ with - starts_
with - string_
regex - tab
- whitespace
- word
- zero_
or_ more - zero_
or_ more_ lazy