如何測試 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.*)"));
   }
}

更新日期: 2020 年 6 月 24 日

275 篇瀏覽量

開啟你的 事業

完成課程認證

開始
廣告
© . All rights reserved.