9951 explained code solutions for 126 technologies


rustWord boundary example in regex in Rust


Word boundary in regex is a zero-width assertion that matches the position between a word character (a-z, A-Z, 0-9, and _) and a non-word character. In Rust, it is represented by the \b character.

Example code

let re = Regex::new(r"\bword\b").unwrap();
let text = "This is a word";

println!("{}", re.is_match(text));

Output example

true

Code explanation

  • Regex::new(r"\bword\b"): creates a new Regex object with the pattern \bword\b
  • let text = "This is a word": creates a string variable with the text This is a word
  • re.is_match(text): checks if the Regex pattern matches the text

Helpful links

Edit this code on GitHub