9951 explained code solutions for 126 technologies

rustHow to use negation in Rust regex?

Negation in Rust regex can be used to match any character except the one specified. To use negation, the ^ character is used inside a character set [].

For example, the following code will match any character except a:

let re = Regex::new(r"[^a]").unwrap();

The code consists of the following parts:

  1. Regex::new(r"[^a]") - creates a new Regex object with the pattern [^a], which matches any character except a
  2. unwrap() - unwraps the Result object returned by Regex::new()
  3. assert!(re.is_match("b")) - checks if the Regex object re matches the string b

For more information, see the Rust Regex documentation.

Edit this code on GitHub