Java如何在不区分大小写的情况下匹配正则表达式模式?
通过使用compile(Stringregex,intflags)方法创建模式并指定带有PATTERN.CASE_INSENSITIVE常量的第二个参数,可以简单地应用查找与模式匹配的输入序列的下一个子序列,而忽略正则表达式中字符串的情况。
package org.nhooo.example.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());
}
}
}这是程序的结果:
Text "brown" found at 10 to 15. Text "BROWN" found at 24 to 29.
热门推荐
10 商务感恩祝福语大全简短
11 晚来的生日祝福语简短
12 爸爸生日的祝福语简短
13 牛年企业祝福语简短大气
14 对新人祝福语大全简短
15 客户新房祝福语简短大气
16 白族结婚祝福语简短大全
17 生日祝福语对妈妈简短
18 送男友2026祝福语简短