加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

正则表达式知识详解之匹配时忽略大小写 (java版示例)

发布时间:2020-12-14 04:26:12 所属栏目:百科 来源:网络整理
导读:正则表达式知识详解系列,通过代码示例来说明正则表达式知识 源代码下载地址:http://download.csdn.net/detail/gnail_oug/9504094 示例功能: 1、区分大小写匹配给定字符串 2、不区分大小写匹配给定字符串 String str= "Hello world,hello java" ; System .

正则表达式知识详解系列,通过代码示例来说明正则表达式知识
源代码下载地址:http://download.csdn.net/detail/gnail_oug/9504094

示例功能:
1、区分大小写匹配给定字符串
2、不区分大小写匹配给定字符串

String str="Hello world,hello java";

        System.out.println("===========区分大小写===========");
        Pattern p=Pattern.compile("hello");
        Matcher m=p.matcher(str);
        while(m.find()){
            System.out.println(m.group()+" 位置:["+m.start()+","+m.end()+"]");
        }

        System.out.println("===========不区分大小写===========");
        p=Pattern.compile("hello",Pattern.CASE_INSENSITIVE);
        m=p.matcher(str);
        while(m.find()){
            System.out.println(m.group()+" 位置:["+m.start()+","+m.end()+"]");
        }

运行结果:

===========区分大小写=========== hello   位置:[12,17]
===========不区分大小写=========== Hello   位置:[0,5]
hello   位置:[12,17]

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读