欢迎来到悦读文库! | 帮助中心 分享价值,成长自我!
悦读文库

正则表达式匹配忽略大小写

正则表达式匹配时有两种忽略大小写的方式:


(?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


分享到微信 分享到微博 分享到QQ空间

本文标签

关于我们 - 网站声明 - 网站地图 - 资源地图 - 友情链接 - 网站客服 - 联系我们

客服QQ:1356877308

copyright@ 2008-2023 悦读文库网站版权所有

备案ICP备案号:京ICP备18064502号-6