9951 explained code solutions for 126 technologies


rustHow to match a string with regex in Rust?


Matching a string with regex in Rust is done using the regex crate. To use it, you must first add it to your Cargo.toml file.

[dependencies]
regex = "1.3.9"

Then you can use the regex crate in your code.

extern crate regex;

use regex::Regex;

fn main() {
    let re = Regex::new(r"\d+").unwrap();
    let text = "The answer is 42";

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

The code above will print true to the console, since the regex \d+ matches the number 42 in the string The answer is 42.

The code consists of the following parts:

  1. extern crate regex; - This line imports the regex crate.
  2. use regex::Regex; - This line imports the Regex type from the regex crate.
  3. let re = Regex::new(r"\d+").unwrap(); - This line creates a new Regex object from the given regex pattern.
  4. let text = "The answer is 42"; - This line creates a string to match against.
  5. println!("{}", re.is_match(text)); - This line prints the result of the regex match to the console.

For more information, see the Regex documentation.

Edit this code on GitHub