Java RegEx Case Insensitive example shows how to make Java regex case insensitive. The example also shows how to make Java regular expression case insensitive using (?i) modifier as well as Pattern class.
How to make Java RegEx case insensitive?
By default Java regular expressions are case sensitive. Consider below given example.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
import java.util.regex.Matcher; import java.util.regex.Pattern; public class CaseInsensitivePatternExample { public static void main(String[] args) { String str = "The first Second was alright, but the second Second was tough"; //Create pattern to match Pattern p = Pattern.compile("second"); Matcher m = p.matcher(str); while(m.find()){ System.out.println(m.group()); } } } |
Output
1 |
second |
The string against which we evaluated our expression has a “second” word 3 times, but the pattern only matched once. The reason is two “second” words have the first character in capital letters. Since the Java regular expressions are case sensitive by default they did not match with our pattern which has all the lower case letters.
There are a couple of ways using which you can make your regex case insensitive.
1) Using Pattern class
Instead of using the compile
method of Pattern class with just pattern argument, you can use overloaded compile
method which accepts various flags along with the pattern. For making pattern case insensitive you need to specify Pattern.CASE_INSENSITIVE
flag as given below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
import java.util.regex.Matcher; import java.util.regex.Pattern; public class CaseInsensitivePatternExample { public static void main(String[] args) { String str = "The first Second was alright, but the second Second was tough"; //Create pattern to match along with the flag Pattern p = Pattern.compile("second", Pattern.CASE_INSENSITIVE); Matcher m = p.matcher(str); while(m.find()){ System.out.println(m.group()); } } } |
Output
1 2 3 |
Second second Second |
As you can see from the output, this time we got three matches.
2) Using (?i) modifier
Another option is to use the (?i)
pattern modifier at the start of the pattern itself which makes whole pattern case insensitive as given below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
import java.util.regex.Matcher; import java.util.regex.Pattern; public class CaseInsensitivePatternExample { public static void main(String[] args) { String str = "The first Second was alright, but the second Second was tough"; //Create pattern to match along with the flag Pattern p = Pattern.compile("(?i)second"); Matcher m = p.matcher(str); while(m.find()){ System.out.println(m.group()); } } } |
Output
1 2 3 |
Second second Second |
How to make partial regex case insensitive?
Specifying Pattern.CASE_INSENSITIVE
flag while compiling the pattern makes whole pattern case insensitive. However, if you are using (?i)
modifier, you can turn off the case insensitivity by specifying (?-i)
modifier in between the pattern which makes part of the regex case insensitive. See below given example.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
import java.util.regex.Matcher; import java.util.regex.Pattern; public class CaseInsensitivePatternExample { public static void main(String[] args) { String str = "FIRSTSECONDthirdFourthfirstsecond"; //1. case sensitive pattern, default Pattern p = Pattern.compile("second"); Matcher m = p.matcher(str); System.out.println("1. Default"); while(m.find()){ System.out.println(m.group()); } //2. this will make whole pattern case insensitive p = Pattern.compile("(?i)second"); m = p.matcher(str); System.out.println("2. Case insensitive"); while(m.find()){ System.out.println(m.group()); } //3. Partial or part case insensitive p = Pattern.compile("(?i)second(?-i)third"); m = p.matcher(str); System.out.println("3. Partial Case insensitive"); while(m.find()){ System.out.println(m.group()); } //4. Partial case insensitive p = Pattern.compile("(?i)first(?-i)Second"); m = p.matcher(str); System.out.println("4. Partial Case insensitive"); while(m.find()){ System.out.println(m.group()); } } } |
Output
1 2 3 4 5 6 7 8 |
1. Default second 2. Case insensitive SECOND second 3. Partial Case insensitive SECONDthird 4. Partial Case insensitive |
The first pattern is case sensitive which is the default. The second pattern is case insensitive as we have specified (?i)
modifier at the start of the expression so our pattern matches with strings “SECOND” and “second” both.
In the third pattern we have specified (?i)
at the start just before “second” (will be matched regardless of the case) but turned it off by specifying (?-i)
before “third” (will be matched with exact case). The whole pattern means find all case insensitive words “second” which are followed by case sensitive word “third”. We did a similar thing with our 4th pattern, that is the reason it did not match.
This example is a part of Java RegEx tutorial.
References:
Pattern class Javadoc