9951 explained code solutions for 126 technologies


rustHow to use regex lookbehind in Rust?


Regex lookbehinds are a powerful tool for matching patterns in strings. In Rust, lookbehinds are supported through the regex crate.

Example code

use regex::Regex;

let re = Regex::new(r"(?<=\d)\s+").unwrap();
let text = "1 2 3 4 5";

for cap in re.captures_iter(text) {
    println!("{}", &cap[0]);
}

Output example

 2
 3
 4

The code above uses a lookbehind to match any whitespace character (\s) preceded by a digit (\d). The lookbehind is specified with (?<=\d). The Regex::new function is used to create a new Regex object from the pattern. The captures_iter method is then used to iterate over all matches in the text.

Code explanation

  • Regex::new: creates a new Regex object from the pattern
  • captures_iter: iterates over all matches in the text
  • (?<=\d): lookbehind to match any whitespace character (\s) preceded by a digit (\d)

Helpful links

Edit this code on GitHub