Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Quantifiers

Control how many times to match a pattern.

Basic Quantifiers

SyntaxDescriptionExample
*Zero or moreab* matches “a”, “ab”, “abb”
+One or moreab+ matches “ab”, “abb”
?Zero or oneab? matches “a”, “ab”
{n}Exactly na{3} matches “aaa”
{n,}At least na{2,} matches “aa”, “aaa”
{n,m}Between n and ma{2,4} matches “aa”, “aaa”, “aaaa”

Lazy Quantifiers

Add ? to make quantifiers lazy (match as few as possible):

fn main() {
    use fuzzy_regex::FuzzyRegex;

    // Greedy: matches as much as possible
    let re1 = FuzzyRegex::new(r"<.+>").unwrap();

    // Lazy: matches as little as possible  
    let re2 = FuzzyRegex::new(r"<.+?>").unwrap();

    let text = "<tag>more</tag>";
    assert!(re1.find(text).unwrap().as_str() == "<tag>more</tag>");
    assert!(re2.find(text).unwrap().as_str() == "<tag>");
}

Possessive Quantifiers

Prevent backtracking (useful for performance):

fn main() {
    use fuzzy_regex::FuzzyRegex;

    // a*+ doesn't backtrack
    let re = FuzzyRegex::new("a*+b").unwrap();
    println!("{}", re.is_match("ab"));
}

Quantifiers with Fuzzy Matching

fn main() {
    use fuzzy_regex::FuzzyRegex;

    // Match "ab" with up to 1 error, repeated 1-3 times
    let re = FuzzyRegex::new("(?:ab){e<=1}{1,3}").unwrap();
    println!("{}", re.is_match("ab"));
}