7 releases
new 0.2.6 | Dec 17, 2024 |
---|---|
0.1.6 | Dec 2, 2024 |
0.1.5 | Nov 29, 2024 |
#343 in Science
626 downloads per month
35KB
966 lines
Regexpr
Rust library for regular expressions
lib.rs
:
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 times[^min_max] |
[a-z] | Matches any character from a to z[^ranged] |
[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 [^group] |
\c | Escapes the character c[^esc] |
\n OR \k<n> | Match the n'th capture group[^capture] |
[^min_max]: 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 *
[^ranged]: 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
[^esc]: Example: "\." Matches a literal dot character.
[^group]: This captured groups can be later referenced
[^capture]: n must be an integer in the range [1,L] where L is the number of capture groups in the expression
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"
No runtime deps
~0–0.8MB