代码之家  ›  专栏  ›  技术社区  ›  Steve McLeod

如何判断Java字符串是否包含X或Y,以及是否包含Z

  •  4
  • Steve McLeod  · 技术社区  · 17 年前

    如果Java字符串(包含文本“ERROR”或文本“WARNING”)和(包含文本”analysis“),其中所有匹配都不区分大小写,我需要找到什么正则表达式?

    8 回复  |  直到 17 年前
        1
  •  21
  •   RichieHindle    17 年前

    string s = test_string.toLowerCase();
    if (s.contains("parsing") && (s.contains("error") || s.contains("warning")) {
        ....
    

    (?i)(?=.*parsing)(.*(error|warning).*)
    

        2
  •  6
  •   João Silva    17 年前

    正向先行断言

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

    示例:

    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
    
        3
  •  0
  •   Khodor    17 年前

     If((str.indexOf("WARNING") > -1 || str.indexOf("ERROR") > -1) && str.indexOf("parsin") > -1)
    
        4
  •  0
  •   Andrey Adamovich    17 年前

    this applet

    if (str.matches("(?i)^.*?(WARNING|ERROR).*?parsing.*$")) {
    ...
    

        5
  •  0
  •   KJ Saxena    17 年前

    if((string1.toUpperCase().indexOf("ERROR",0) >= 0 ||  
      string1.toUpperCase().indexOf("WARNING",0) >= 0 ) &&
      string1.toUpperCase().indexOf("PARSING",0) >= 0 )
    

        6
  •  0
  •   Pascal Thivent    17 年前

    (.*(ERROR|WARNING).*parsing)|(.*parsing.*(ERROR|WARNING))
    
        7
  •  0
  •   Carlos Tasada    17 年前

    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 :(");
    }
    

        8
  •  0
  •   Stephan    13 年前

    .* 构造解析器将调用数千个“回退并重试”试验匹配。