'what is the best way to match multiple characters [closed]

I have a list of strings and the first one contains only one * and the second one contains two * and third one contains single star and two stars. I'm using conatins function to find the match but if condition goes wrong somehow. what mistake am I making and what is the best way to solve such issue.

link to code https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=89d53c620b58e61b60966d8c12aa7393

code:

let mut list_str:Vec<&str> = Vec::new();
list_str.push("I only contains one *");
list_str.push("I contain two stars **");
list_str.push("I conatain single star * and two stars **");

for filter in list_str{
    if filter.contains("*") && filter.contains("**"){
        println!("String contains multiple stars >>: {} ",filter);
    } 
    else if filter.contains("**"){
     println!("String contains two stars >>: {}",filter);

    }
    else if filter.contains("*"){
     println!("String contains only one star  >>: {}",filter);
    }
    else{ 
      continue;
    }
}


Solution 1:[1]

With substring search (Rust playground)

If you don't want to use RegEx, you can do something like this. Probably it could be written more efficiently, but this should give you an idea of what the intention was.

fn main() {
    let mut list_str:Vec<&str> = Vec::new();
    list_str.push("I only contains one *");
    list_str.push("I contain two stars **");
    list_str.push("I conatain single star * and two stars **");

    for filter in list_str{
        if let Some(i) = filter.find("*") {
            if let Some(_) = filter.find("**") {
                if filter[i+2..].contains("*") {
                    println!("String contains multiple stars >>: {} ",filter);
                } else {
                    println!("String contains two stars >>: {}",filter);
                }
            } else {
                println!("String contains only one star  >>: {}",filter);
            }
        } else { 
          continue;
        }
    }
}

With RegEx (Rust playground)

I would probably prefer this approach. Simple, fairly efficient and more readable. Notice that the order of the if statements matters (from the largest to the smallest match).

use regex::Regex;
fn main() {
    let mut list_str:Vec<&str> = Vec::new();
    list_str.push("I only contains one *");
    list_str.push("I contain two stars **");
    list_str.push("I conatain single star * and two stars **");

    let re1 = Regex::new(r"\*").unwrap();
    let re2 = Regex::new(r"\*\*").unwrap();
    let re12 = Regex::new(r"(\*[^*]+\*\*)|(\*\*[^*]+\*)").unwrap();

    for filter in list_str{
        if re12.is_match(filter) {
            println!("String contains multiple stars >>: {} ",filter);
        } else if re2.is_match(filter) {
            println!("String contains two stars >>: {}",filter);
        } else if re1.is_match(filter) {
            println!("String contains only one star  >>: {}",filter);
        } else { 
          continue;
        }
    }
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1