expression

Greedy and lazy repetition

Greedy repetitions favor longer matches subject to the rest of the expression; lazy repetitions favor shorter matches. Neither is a parser for arbitrary nested delimiters. This example uses the external regex crate; see the staged Cargo manifest. Expected stdout (not executed): [a][b] [a] Common mistake: Using a lazy expression as a full parser for nested structures.

Formal Syntax & Structure

rust syntaxrust
.* versus .*?

Working Example

Example Snippetrust
fn main() {    let text = "[a][b]";    let greedy = regex::Regex::new(r"\[.*\]").unwrap();    let lazy = regex::Regex::new(r"\[.*?\]").unwrap();    println!("{} {}", greedy.find(text).unwrap().as_str(), lazy.find(text).unwrap().as_str());}
Rust Reference