How to declare a RegEx with character repetition to match the strings "http", "htttp", "httttp", etc in Rust

1 Answer

0 votes
use regex::Regex;

fn main() {
    // Declare the regex pattern
    let pattern = Regex::new(r"^htt+p$").unwrap();

    // Test strings
    let test_strings = ["http", "htttp", "httttp", "httpp", "htp"];

    // Check matches
    for test in test_strings.iter() {
        let matches = pattern.is_match(test);
        println!("Matches \"{}\": {}", test, matches);
    }
}

// Matches "httpp": True or false, depending on how matches() method works
 
 
/*
run:
 
Matches "http": true
Matches "htttp": true
Matches "httttp": true
Matches "httpp": false
Matches "htp": false
 
*/

 



answered May 16, 2025 by avibootz
...