What is Pattern.CASE_INSENSITIVE in Java?

Overview

The CASE_INSENSITIVE field of the Pattern class is used for pattern matching without case considerations. Characters from the US-ASCII charset are matched by default.


For unicode case-insensitive matching, combine the UNICODE_CASE flag with CASE_INSENSITIVE flag

Syntax


Pattern pattern = Pattern.compile(regex, Pattern.CASE_INSENSITIVE);

The Pattern.compile method takes the regexregular expression and match flags as arguments. Case-insensitive pattern matching can be achieved by passing the Pattern.COMMENTS flag as the match flag.

Code

import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Main {
public static void main(String[] args) {
String string = "Hello eduCATive,how aRe yOU? With educative you learn things easily";
Pattern pattern = Pattern.compile("educative", Pattern.CASE_INSENSITIVE);
Matcher match = pattern.matcher(string);
System.out.println("Matched characters:");
while (match.find()) {
System.out.println(match.group());
}
}
}

Free Resources