🔮
Write readable regular expressions
The crate provides a clean and readable way of writing your regex in the Rust programming language:
Without |
With |
|
digit()
.repeats(5)
.then(
just("-")
.then(digit().repeats(4))
.optional()
)
|
|
beginning()
.then(digit().repeats(4))
.then(
just("-")
.then(digit().repeats(2))
.repeats(2)
)
.then(ending())
|
|
just("rege")
.then(one_of(&[
just("x").then(just("es").optional()),
just("xp").then(just("s").optional()),
]))
|
How to use the crate?
To convert a PrettyRegex
struct which is constructed using all these then
, one_of
, beginning
, digit
, etc. functions into a real regex (from regex
crate), you can call to_regex
or to_regex_or_panic
:
use pretty_regex::digit;
let regex = digit().to_regex_or_panic();
assert!(regex.is_match("3"));