如何检验 Java 字符串是否包含不区分大小写的正则表达式模式


语法?i:x 使字符串搜索不区分大小写。例如

public class RegCaseSense {
   public static void main(String[] args) {
      String stringSearch = "HI we are at java class.";

      // this won't work because the pattern is in upper-case
      System.out.println("Try this 1: " + stringSearch.matches(".*CLASS.*"));
 
      // the magic (?i:X) syntax makes this search case-insensitive, so it returns true
      System.out.println("Try this 2: " + stringSearch.matches("(?i:.*CLASS.*)"));
   }
}

更新日期:24-6-2020

275 次浏览

开启你的 职业生涯

通过完成课程获得认证

开始
广告
© . All rights reserved.