Crate simple_regex

Source
Expand description

Β§Simple Regex Library

A simple Rust library for building regular expressions.

Β§Features

  • Expressive Builder Pattern: Easily construct regular expressions using a chainable builder pattern.
  • Concise API: The API is designed to be concise and intuitive, making it easy to build complex regex patterns.
  • Modifier Support: Add modifiers like case-insensitive, global search, multiline, and dot-all to your regex patterns.
  • Quantifiers: Use quantifiers like zero or more, one or more, zero or one, exact repetitions, minimum repetitions, and range repetitions.
  • Ansi Formatting: Includes an ANSI formatting module for adding color to terminal output.

Β§ANSI Module

Β§Colors

  • fg_black(text: String) -> String: Formats the given text with black foreground color.
  • fg_red(text: String) -> String: Formats the given text with red foreground color.
  • fg_green(text: String) -> String: Formats the given text with green foreground color.
  • fg_yellow(text: String) -> String: Formats the given text with yellow foreground color.
  • fg_blue(text: String) -> String: Formats the given text with blue foreground color.
  • fg_purple(text: String) -> String: Formats the given text with purple foreground color.
  • fg_cyan(text: String) -> String: Formats the given text with cyan foreground color.
  • fg_white(text: String) -> String: Formats the given text with white foreground color.

Β§RegexBuilder Struct

Builder for constructing regular expressions.

Β§Methods

FunctionDescriptionExampleResult
new() -> SelfCreates a new instance of RegexBuilder.RegexBuilder::new()RegexBuilder instance
literal(char) -> SelfAppends a literal character to the regex..literal('a')β€œa”
dot() -> SelfAppends a dot (.) to the regex, matching any single character..dot()β€œ.”
escape(char) -> SelfAppends an escaped character to the regex..escape('[')β€œ\[”
start_of_line() -> SelfAppends the start of line anchor (^) to the regex..start_of_line()β€œ^”
end_of_line() -> SelfAppends the end of line anchor ($) to the regex..end_of_line()β€œ$”
character_class(chars: &str) -> SelfAppends a character class to the regex..character_class("abc")β€œ[abc]”
negated_character_class(chars: &str) -> SelfAppends a negated character class to the regex..negated_character_class("abc")β€œ[^abc]”
range_character_class(start: char, end: char) -> SelfAppends a range character class to the regex..range_character_class('a', 'z')β€œ[a-z]”
digit() -> SelfAppends a digit character class to the regex..digit()β€œ\d”
non_digit() -> SelfAppends a non-digit character class to the regex..non_digit()β€œ\D”
word_character() -> SelfAppends a word character class to the regex..word_character()β€œ\w”
non_word_character() -> SelfAppends a non-word character class to the regex..non_word_character()β€œ\W”
whitespace() -> SelfAppends a whitespace character class to the regex..whitespace()β€œ\s”
non_whitespace() -> SelfAppends a non-whitespace character class to the regex..non_whitespace()β€œ\S”
zero_or_more(regex: RegexBuilder) -> SelfAppends a zero or more quantifier to the regex..zero_or_more(RegexBuilder::new().character_class("a"))β€œ[a]*”
one_or_more(regex: RegexBuilder) -> SelfAppends a one or more quantifier to the regex..one_or_more(RegexBuilder::new().character_class("a"))β€œ[a]+”
zero_or_one(regex: RegexBuilder) -> SelfAppends a zero or one quantifier to the regex..zero_or_one(RegexBuilder::new().character_class("a"))β€œ[a]?”
exact_repetitions(regex: RegexBuilder, n: usize) -> SelfAppends an exact repetitions quantifier to the regex..exact_repetitions(RegexBuilder::new().digit(), 3)β€œ\d{3}”
min_repetitions(regex: RegexBuilder, n: usize) -> SelfAppends a minimum repetitions quantifier to the regex..min_repetitions(RegexBuilder::new().digit(), 3)β€œ\d{3,}”
range_repetitions(regex: RegexBuilder, n: usize, m: usize) -> SelfAppends a range repetitions quantifier to the regex..range_repetitions(RegexBuilder::new().digit(), 3, 5)β€œ\d{3,5}”
group(regex: RegexBuilder) -> SelfAppends a group to the regex..group(RegexBuilder::new().character_class("ab"))β€œ(?:[ab])”
backreference(group_number: usize) -> SelfAppends a backreference to a capturing group in the regex..backreference(1)β€œ\1”
word_boundary() -> SelfAppends a word boundary anchor (\b) to the regex..word_boundary()β€œ\b”
non_word_boundary() -> SelfAppends a non-word boundary anchor (\B) to the regex..non_word_boundary()β€œ\B”
case_insensitive(regex: RegexBuilder) -> SelfAppends a case-insensitive modifier to the regex..case_insensitive(RegexBuilder::new().character_class("a"))β€œ(?i[a])”
global_search(regex: RegexBuilder) -> SelfAppends a global search modifier to the regex..global_search(RegexBuilder::new().character_class("a"))β€œ(?g[a])”
multiline(regex: RegexBuilder) -> SelfAppends a multiline modifier to the regex..multiline(RegexBuilder::new().character_class("a"))β€œ(?m[a])”
dot_all(regex: RegexBuilder) -> SelfAppends a dot-all modifier to the regex, allowing β€˜.’ to match newline characters..dot_all(RegexBuilder::new().character_class("a"))β€œ(?s[a])”
alternative(regex1: RegexBuilder, regex2: RegexBuilder) -> SelfAppends an alternative (|) to the regex, allowing either of the provided patterns to match..alternative(RegexBuilder::new().character_class("a"), RegexBuilder::new().character_class("b"))β€œ[a]|[b]”
capturing_group(regex: RegexBuilder) -> SelfAppends a capturing group to the regex..capturing_group(RegexBuilder::new().character_class("a"))β€œ([a])”
to_regex()Converts the current RegexBuilder into a Regex object.β€œReturns a Result<Regex, regex::Error>.”
to_regex_or_panic()Converts the current RegexBuilder into a Regex object or panics if an error occurs.β€œReturns a Regex object.”

Β§Examples

use simple_regex::RegexBuilder;

let regex = RegexBuilder::new().literal('a').build();
assert_eq!(regex, "a");

Please make sure to adjust the version number in the dependency based on the latest release.

ModulesΒ§

ansi

StructsΒ§

RegexBuilder
Builder for constructing regular expressions.