9951 explained code solutions for 126 technologies

rustHow to match a URL with a regex in Rust?

Matching a URL with a regex in Rust is a simple process. The following example code block shows how to do this:

let re = Regex::new(r"^(https?://)?([\da-z\.-]+)\.([a-z\.]{2,6})([\/\w \.-]*)*\/?$").unwrap();
let url = "https://www.example.com/path/to/page";

if re.is_match(url) {
    println!("URL matches the regex!");

The output of the example code is:

URL matches the regex!

Code explanation

  • let re = Regex::new(r"^(https?://)?([\da-z\.-]+)\.([a-z\.]{2,6})([\/\w \.-]*)*\/?$").unwrap();: This line creates a new Regex object with the given regex pattern. The pattern matches URLs with the following format: protocol://domain.tld/path/to/page.
  • let url = "https://www.example.com/path/to/page";: This line creates a string variable with the URL to be matched.
  • if re.is_match(url) {: This line checks if the URL matches the regex pattern.
  • println!("URL matches the regex!");: This line prints a message if the URL matches the regex pattern.

Helpful links

Edit this code on GitHub