Crate pretty_regex

Source
Expand description

§🔮 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})?
ⓘ
digit() * 5 + (just("-") + digit() * 4).optional()
^(?:\d){4}(?:(?:\-)(?:\d){2}){2}$
ⓘ
beginning() + digit() * 4
            + (just("-") + digit() * 2) * 2
            + ending()
rege(x(es)?|xps?)
ⓘ
just("rege") + (just("x") + just("es").optional())
             | (just("xp") + 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"));

Modules§

logic
prelude
unicode

Structs§

Ascii
Represents the state when regular expression is for a single-character ASCII class (the kind surrounded by colons and two layers of square brackets).
Chain
Represents the state when it is any arbitrary regular expression.
CharClass
Represents the state when regular expression corresponds to a single-character character.
Custom
Represents the state when regular expression is for a custom single-character class (the kind surrounded by one layer of square brackets).
PrettyRegex
Quantifier
Represents the state when regular expression is a quantifier (e.g., an expression that matches a given number of a target).
Standard
Represents the state when regular expression is a standard single-character class (the kind in most cases starts with a backslash followed by a letter)
Text
Represents the state when regular expression is a literal string of characters.

Functions§

alphabetic
Matches alphabetic characters (in Letter Unicode category).
alphanumeric
Matches alphanumeric characters (in Letter and Number Unicode categories).
any
Matches any character, except for newline (\n).
ascii_alphabetic
Matches ascii alphabetic characters (a-zA-Z).
ascii_alphanumeric
Matches ascii alphanumeric characters (a-zA-Z0-9).
ascii_lowercase
Matches ascii lowercase characters (a-z).
beginning
Matches the beginning of the text or SOF with multi-line mode off (^).
digit
Matches digit character class (\d).
ending
Matches the end of the text or EOF with multi-line mode on ($).
just
Adds a matching text into a PrettyRegex.
lowercase
Matches lowercase characters (in Lowercase_Letter Unicode category).
nonescaped
Makes regex from unescaped text. It allows to add a regex string directly into a PrettyRegex object.
one_of
Establishes an OR relationship between regular expressions.
text_beginning
Matches the beginning of the text even with multi-line mode on (\A).
text_ending
Matches the end of the text even with multi-line mode on (\z).
whitespace
Matches whitespace character class (\s).
within
Matches anything within a specified set of characters.
within_char_range
Matches characters within a given range.
without
Matches anything outside of a specified set of characters.
without_char_range
Matches characters outside of a given range.
word
Matches word character class (\w) - any alphanumeric character or underscore (_).
word_boundary
Matches a word boundary (\b).