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

正则表达式知识详解系列,通过代码示例来说明正则表达式知识
源代码下载地址: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]

你可能感兴趣的:(regex)