How do I match a regex pattern in case insensitive?

Finding the next subsequence of the input sequence that matches the pattern while ignoring the case of the string in regular expression can simply applied by create a pattern using compile(String regex, int flags) method and specifies a second argument with PATTERN.CASE_INSENSITIVE constant.

package org.kodejava.example.util.regex;

import java.util.regex.Pattern;
import java.util.regex.Matcher;

public class IgnoreCaseDemo {
    public static void main(String[] args) {
        String sentence =
                "The quick brown fox and BROWN tiger jumps " +
                "over the lazy dog";

        Pattern pattern = Pattern.compile("brown", 
                Pattern.CASE_INSENSITIVE);
        Matcher matcher = pattern.matcher(sentence);

        while (matcher.find()) {
            System.out.format("Text "%s" found at %d to %d.%n",
                matcher.group(), matcher.start(), matcher.end());
        }
    }
}

Here is the result of the program:

Text "brown" found at 10 to 15.
Text "BROWN" found at 24 to 29.
Wayan Saryada

Wayan Saryada

A programmer, runner, recreational diver, currently living in the island of Bali, Indonesia. Mostly programming in Java, creating web based application with Spring Framework, JPA, etc. If you need help on Java programming you can hire me on Fiverr.
Wayan Saryada

Leave a Reply