9951 explained code solutions for 126 technologies


rustHow to get a capture group using Rust regex?


Capture groups are used to extract parts of a string that match a regular expression pattern. In Rust, capture groups are created using the capture_group method of the Regex struct.

Example code

let re = Regex::new(r"(\d{4})-(\d{2})-(\d{2})").unwrap();
let caps = re.capture_group("2020-01-01").unwrap();

Output example

[("2020-01-01", "2020", "01", "01")]

Code explanation

  • Regex::new(r"(\d{4})-(\d{2})-(\d{2})"): creates a new Regex struct with a regular expression pattern that matches a 4-digit year, followed by a 2-digit month, followed by a 2-digit day.
  • capture_group("2020-01-01"): uses the Regex struct to capture the parts of the string that match the regular expression pattern.

Helpful links

Edit this code on GitHub