How to Use Regex with Case Insensitive in Java


In this example we will show how to use regex with case insensitive in Java.

Source Code

package com.beginner.examples;

import java.util.regex.Matcher;
import java.util.regex.Pattern;
 
public class RegexCaseExample {
	
    public static void main(String a[]){
    	// regex rule
    	Pattern p = Pattern.compile("java", Pattern.CASE_INSENSITIVE);
    	Matcher m = p.matcher("JAVA EXAMPLE");
        System.out.println(m.find());
        m = p.matcher("JAVA EXAMPLE java example");
        System.out.println(m.find());
    }
}

Output:

true
true

References

Imported packages in Java documentation:

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments