正则表达式匹配时有两种忽略大小写的方式:
(?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()); // true
本文介绍了Java中进行正则表达式匹配时如何忽略大小写。方法包括使用模式修饰符(?i)以及在Pattern.compile()时指定CASE_INSENSITIVE标志。通过示例代码展示了这两种方式的用法及其效果,强调了在不同位置使用(?i)的影响。
6万+

被折叠的 条评论
为什么被折叠?



