Finding the next subsequence of the input sequence that matches the pattern while ignoring the case of the string in regular expression can simply apply by create a pattern using compile(String regex, int flags)
method and specifies a second argument with PATTERN.CASE_INSENSITIVE
constant.
package org.kodejava.regex;
import java.util.regex.Pattern;
import java.util.regex.Matcher;
public class RegexIgnoreCaseDemo {
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.
Latest posts by Wayan (see all)
- How do I get number of each day for a certain month in Java? - September 8, 2024
- How do I get operating system process information using ProcessHandle? - July 22, 2024
- How do I sum a BigDecimal property of a list of objects using Java Stream API? - July 22, 2024