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 withCASE_INSENSITIVE
flag
Pattern pattern = Pattern.compile(regex, Pattern.CASE_INSENSITIVE);
The Pattern.compile
method takes the Pattern.COMMENTS
flag as the match flag.
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());}}}