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

前端 未结 8 1842
孤街浪徒
孤街浪徒 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:44

    I think this regexp will do the trick (but there must be a better way to do it):

    (.*(ERROR|WARNING).*parsing)|(.*parsing.*(ERROR|WARNING))
    
    0 讨论(0)
  • 2021-01-06 11:59

    If you really want to use regular expressions, you can use the positive lookahead operator:

    (?i)(?=.*?(?:ERROR|WARNING))(?=.*?parsing).*
    

    Examples:

    Pattern p = Pattern.compile("(?=.*?(?:ERROR|WARNING))(?=.*?parsing).*", Pattern.CASE_INSENSITIVE); // you can also use (?i) at the beginning
    System.out.println(p.matcher("WARNING at line X doing parsing of Y").matches()); // true
    System.out.println(p.matcher("An error at line X doing parsing of Y").matches()); // true
    System.out.println(p.matcher("ERROR Hello parsing world").matches()); // true       
    System.out.println(p.matcher("A problem at line X doing parsing of Y").matches()); // false
    
    0 讨论(0)
提交回复
热议问题