Crate regexpr

Source
Expand description

§Regular Expressions

This crate provides a Regex struct that compiles and tests regular expressions.

§Example

use regexpr::Regex;

let regex = Regex::compile(r#"^a(.)c\1.*$"#).unwrap();
assert!(regex.test("abcb"));
assert!(regex.test("abcbde"));
assert!(!regex.test("bcdsd"));
assert!(!regex.test("abcd"));

§Rules

RuleMeaning
.Matches any character
*Matches the previous rule zero or more times
+Matches the previous rule one or more times
?Makes the previous rule optional
{n,m}Matches the previous rule a minimun of n times and a maximun of m times1
[a-z]Matches any character from a to z2
[agf]Matches any of the characters inside
[^…]Same as the rules above but negated
A | BMaches A or B
(ABC)Groups rules A B and C 3
\cEscapes the character c4
\n OR \k<n>Match the n’th capture group5

§Greedy vs. Lazy

“Lazy” versions of * and + exist.
*? and +? work just as * and +, but they stop as soon as possible.

§Example

    Regex: .*b
    Input: aaaaaabaaaaab
    Matches: One match "aaaaaabaaaaab"

    Regex: .*?b
    Input: aaaaaabaaaaab
    Matches: Two matches "aaaaaab" and "aaaaab"

  1. If min or max are not present, it means there’s no limit on that size.
    Examples:
    {,12} matches a rule up to 12
    {3,} matches a rule at least 3 times.
    {,} is the same as * 

  2. The ranges can be mixed.
    Examples:
    [a-z123]: Matches any character in the ranges a-z , 1, 2 or 3
    [^0-9ab]: Matches a character that IS NOT a number or a or b 

  3. This captured groups can be later referenced 

  4. Example: “\.” Matches a literal dot character. 

  5. n must be an integer in the range [1,L] where L is the number of capture groups in the expression 

Structs§

Regex
Main Regex struct
RegexConf
RegexError
RegexMatch
Represents a match of a string on a Regex
RegexMatcher
Iterator over all the matches of a string in a Regex

Traits§

RegexTestable
This trait is used to add an extension method matches_regex to &str
ReplaceRegex