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.
热门推荐
2 十月简短祝福语
10 简短的祝福语12字
11 长辈生日祝福语简短女性
12 婆婆生日祝福语简短的
13 新疆新年祝福语大全简短
14 2026年春节祝福语简短
15 发廊元旦祝福语大全简短
16 送个姐妹生日祝福语简短
17 男生打球祝福语简短霸气
18 英语简短祝福语给学生