正则表达式匹配时有两种忽略大小写的方式:
(?i) 之后的字符在匹配时忽略大小写
Patter.compile() 时,指定忽略大小写模式
1、(?i) 之后的字符在匹配时忽略大小写。
比如:
System.out.println(Pattern.compile("(?i)abc").matcher("ABC").matches()); // true
System.out.println(Pattern.compile("a(?i)bc").matcher("ABC").matches()); // false
System.out.println(Pattern.compile("a(?i)bc").matcher("aBC").matches()); // true
其中,(?i) 可以在起始标识 ^ 之前:
System.out.println(Pattern.compile("(?i)^abc$").matcher("ABC").matches()); // true
2、Patter.compile() 时,指定忽略大小写模式
比如:
System.out.println(Pattern.compile("abc", Pattern.CASE_INSENSITIVE).matcher("ABC").matches()); //
替换前后9个字符:
html222 = Regex.Replace(html222, @"[\s\S]{0,9}(?i)中学aBc[\s\S]{0,9}", "@");
原文链接:https://blog.csdn.net/zhy1379/article/details/124696392