# 🔮 Write readable regular expressions
The crate provides a clean and readable way of writing your regex in the Rust programming language:
Without `pretty_regex` | With `pretty_regex` |
``` \d{5}(-\d{4})? ``` | ```rs digit() .repeats(5) .then( just("-") .then(digit().repeats(4)) .optional() ) ``` |
``` ^(?:\d){4,}(?:(?:\-)(?:\d){2,}){2,}$ ``` | ```rs beginning() .then(digit().repeats(4)) .then( just("-") .then(digit().repeats(2)) .repeats(2) ) .then(ending()) ``` |
``` rege(x(es)?|xps?) ``` | ```rs just("rege") .then(one_of(&[ just("x").then(just("es").optional()), just("xp").then(just("s").optional()), ])) ``` |
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
:
```rs use pretty_regex::digit;
let regex = digit().toregexor_panic();
assert!(regex.is_match("3")); ```