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
Rule | Meaning |
---|---|
. | 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 | B | Maches A or B |
(ABC) | Groups rules A B and C 3 |
\c | Escapes 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"
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 * ↩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 ↩This captured groups can be later referenced ↩
Example: “\.” Matches a literal dot character. ↩
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
- Regex
Conf - Regex
Error - Regex
Match - Represents a match of a string on a Regex
- Regex
Matcher - Iterator over all the matches of a string in a Regex
Traits§
- Regex
Testable - This trait is used to add an extension method
matches_regex
to &str - Replace
Regex