9951 explained code solutions for 126 technologies


rustHow to use captures_iter with regex in Rust?


Using captures_iter with regex in Rust is a powerful way to extract data from strings. captures_iter returns an iterator of all the captures that match a given pattern.

Example code

let re = Regex::new(r"(\d{4})-(\d{2})-(\d{2})").unwrap();
let text = "Today is 2020-04-30";

for cap in re.captures_iter(text) {
    println!("Year: {}, Month: {}, Day: {}", &cap[1], &cap[2], &cap[3]);
}

Output example

Year: 2020, Month: 04, Day: 30

Code explanation

  • Regex::new(r"(\d{4})-(\d{2})-(\d{2})"): creates a new Regex object with the given pattern.
  • captures_iter: returns an iterator of all the captures that match the given pattern.
  • &cap[1], &cap[2], &cap[3]: accesses the captures from the iterator.

Helpful links

Edit this code on GitHub