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

Re-exports

Modules

Structs

  • Represents the state when regular expression is for a single-character ASCII class (the kind surrounded by colons and two layers of square brackets).
  • Represents the state when it is any arbitrary regular expression.
  • Represents the state when regular expression corresponds to a single-character character.
  • Represents the state when regular expression is for a custom single-character class (the kind surrounded by one layer of square brackets).
  • Represents the state when regular expression is a quantifier (e.g., an expression that matches a given number of a target).
  • 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)
  • Represents the state when regular expression is a literal string of characters.

Functions

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