How to test if a Java String contains a case insensitive regex pattern



the syntax? i:x makes the string search case-insensitive. for eg

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.*)"));
   }
}

Advertisements