To perform case-insensitive matching in Java regular expressions, we can use the Pattern.CASE_INSENSITIVE
flag or the (?i)
flag within the regular expression pattern.
Here’s an example that demonstrates case-insensitive matching using both approaches:
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class CaseInsensitiveMatching {
public static void main(String[] args) {
String text = "Hello World";
String pattern = "(?i)hello";
// Using Pattern.CASE_INSENSITIVE flag
Pattern p1 = Pattern.compile(pattern, Pattern.CASE_INSENSITIVE);
Matcher m1 = p1.matcher(text);
System.out.println("Using Pattern.CASE_INSENSITIVE:");
while (m1.find()) {
System.out.println("Match found at index: " + m1.start());
}
// Using (?i) flag within the pattern
Pattern p2 = Pattern.compile(pattern);
Matcher m2 = p2.matcher(text);
System.out.println("Using (?i) flag within the pattern:");
while (m2.find()) {
System.out.println("Match found at index: " + m2.start());
}
}
}
In the example above, we have a text string containing “Hello World” and we are searching for the word “hello” in a case-insensitive manner.
The first approach demonstrates using the Pattern.CASE_INSENSITIVE
flag when compiling the regular expression pattern. The Pattern.CASE_INSENSITIVE
flag enables case-insensitive matching. We create a Pattern
object p1
and a Matcher
object m1
to perform the matching operation. The find()
method of the Matcher
object is called inside a while
loop to find all occurrences of the pattern. We then print the starting index of each match.
The second approach demonstrates using the (?i)
flag within the regular expression pattern itself. The (?i)
flag at the beginning of the pattern sets the case-insensitive mode for the entire pattern. We create another Pattern
object p2
and a Matcher
object m2
to perform the matching operation. Similar to the first approach, we use the find()
method and print the starting index of each match.
Both approaches will produce the same output:
Using Pattern.CASE_INSENSITIVE:
Match found at index: 0
Using (?i) flag within the pattern:
Match found at index: 0
By using either the Pattern.CASE_INSENSITIVE
flag or the (?i)
flag within the regular expression pattern, we can easily perform case-insensitive matching in Java regular expressions. This technique can be very useful when dealing with textual data that may have inconsistent casing.