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 爱生日祝福语简短独特