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
Function | Description | Example | Result |
---|---|---|---|
new() -> Self | Creates a new instance of RegexBuilder . | RegexBuilder::new() | RegexBuilder instance |
literal(char) -> Self | Appends a literal character to the regex. | .literal('a') | βaβ |
dot() -> Self | Appends a dot (.) to the regex, matching any single character. | .dot() | β.β |
escape(char) -> Self | Appends an escaped character to the regex. | .escape('[') | β\[β |
start_of_line() -> Self | Appends the start of line anchor (^) to the regex. | .start_of_line() | β^β |
end_of_line() -> Self | Appends the end of line anchor ($) to the regex. | .end_of_line() | β$β |
character_class(chars: &str) -> Self | Appends a character class to the regex. | .character_class("abc") | β[abc]β |
negated_character_class(chars: &str) -> Self | Appends a negated character class to the regex. | .negated_character_class("abc") | β[^abc]β |
range_character_class(start: char, end: char) -> Self | Appends a range character class to the regex. | .range_character_class('a', 'z') | β[a-z]β |
digit() -> Self | Appends a digit character class to the regex. | .digit() | β\dβ |
non_digit() -> Self | Appends a non-digit character class to the regex. | .non_digit() | β\Dβ |
word_character() -> Self | Appends a word character class to the regex. | .word_character() | β\wβ |
non_word_character() -> Self | Appends a non-word character class to the regex. | .non_word_character() | β\Wβ |
whitespace() -> Self | Appends a whitespace character class to the regex. | .whitespace() | β\sβ |
non_whitespace() -> Self | Appends a non-whitespace character class to the regex. | .non_whitespace() | β\Sβ |
zero_or_more(regex: RegexBuilder) -> Self | Appends a zero or more quantifier to the regex. | .zero_or_more(RegexBuilder::new().character_class("a")) | β[a]*β |
one_or_more(regex: RegexBuilder) -> Self | Appends a one or more quantifier to the regex. | .one_or_more(RegexBuilder::new().character_class("a")) | β[a]+β |
zero_or_one(regex: RegexBuilder) -> Self | Appends a zero or one quantifier to the regex. | .zero_or_one(RegexBuilder::new().character_class("a")) | β[a]?β |
exact_repetitions(regex: RegexBuilder, n: usize) -> Self | Appends an exact repetitions quantifier to the regex. | .exact_repetitions(RegexBuilder::new().digit(), 3) | β\d{3}β |
min_repetitions(regex: RegexBuilder, n: usize) -> Self | Appends a minimum repetitions quantifier to the regex. | .min_repetitions(RegexBuilder::new().digit(), 3) | β\d{3,}β |
range_repetitions(regex: RegexBuilder, n: usize, m: usize) -> Self | Appends a range repetitions quantifier to the regex. | .range_repetitions(RegexBuilder::new().digit(), 3, 5) | β\d{3,5}β |
group(regex: RegexBuilder) -> Self | Appends a group to the regex. | .group(RegexBuilder::new().character_class("ab")) | β(?:[ab])β |
backreference(group_number: usize) -> Self | Appends a backreference to a capturing group in the regex. | .backreference(1) | β\1β |
word_boundary() -> Self | Appends a word boundary anchor (\b) to the regex. | .word_boundary() | β\bβ |
non_word_boundary() -> Self | Appends a non-word boundary anchor (\B) to the regex. | .non_word_boundary() | β\Bβ |
case_insensitive(regex: RegexBuilder) -> Self | Appends a case-insensitive modifier to the regex. | .case_insensitive(RegexBuilder::new().character_class("a")) | β(?i[a])β |
global_search(regex: RegexBuilder) -> Self | Appends a global search modifier to the regex. | .global_search(RegexBuilder::new().character_class("a")) | β(?g[a])β |
multiline(regex: RegexBuilder) -> Self | Appends a multiline modifier to the regex. | .multiline(RegexBuilder::new().character_class("a")) | β(?m[a])β |
dot_all(regex: RegexBuilder) -> Self | Appends 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) -> Self | Appends 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) -> Self | Appends 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Β§
StructsΒ§
- Regex
Builder - Builder for constructing regular expressions.