expression

Regex::captures: numbered groups

Parentheses capture submatches. Group zero describes the entire match. Access a required group safely when the pattern or input may change. This example uses the external regex crate; see the staged Cargo manifest. Expected stdout (not executed): BOX-12 BOX 12 Common mistake: Confusing the first explicit group with group zero.

Formal Syntax & Structure

rust syntaxrust
re.captures(text) -> Option<Captures>

Working Example

Example Snippetrust
fn main() {    let re = regex::Regex::new(r"([A-Z]+)-([0-9]+)").unwrap();    let c = re.captures("BOX-12").unwrap();    println!("{} {} {}", &c[0], &c[1], &c[2]);}
Rust Reference