How to find if a Java String contains X or Y and contains Z

前端 未结 8 1961
孤街浪徒
孤街浪徒 2021-01-06 11:03

I\'m pretty sure regular expressions are the way to go, but my head hurts whenever I try to work out the specific regular expression.

What regular expression do I ne

8条回答
  •  耶瑟儿~
    2021-01-06 11:43

    If you've a variable number of words that you want to match I would do something like that:

    String mystring = "Text I want to match";
    String[] matchings = {"warning", "error", "parse", ....}
    int matches = 0;
    for (int i = 0; i < matchings.length(); i++) {
      if (mystring.contains(matchings[i]) {
        matches++;
      }
    }
    
    if (matches == matchings.length) {
       System.out.println("All Matches found");
    } else {
       System.out.println("Some word is not matching :(");
    }
    

    Note: I haven't compiled this code, so could contain typos.

提交回复
热议问题