expression
Named capture groups
Name fields that have a meaningful role. Look up a group by its stable name rather than its position in the pattern. This example uses the external regex crate; see the staged Cargo manifest. Expected stdout (not executed): size 24 Common mistake: Assuming a capture name exists in every possible pattern.
Formal Syntax & Structure
rust syntaxrust
(?P<name>pattern); captures.name("name")Working Example
Example Snippetrust
fn main() { let re = regex::Regex::new(r"(?P<key>[a-z]+)=(?P<value>[0-9]+)").unwrap(); let c = re.captures("size=24").unwrap(); println!("{} {}", c.name("key").unwrap().as_str(), c.name("value").unwrap().as_str());}