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

正则表达式知识详解之多行模式 (java版示例)

发布时间:2020-12-14 04:26:07 所属栏目:百科 来源:网络整理
导读:正则表达式知识详解系列,通过代码示例来说明正则表达式知识 源代码下载地址:http://download.csdn.net/detail/gnail_oug/9504094 java启用多行模式只需要在编译模式时增加参数Pattern.MULTILINE即可 示例功能: 1、多行模式匹配字符串开头 2、多行模式匹配

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

java启用多行模式只需要在编译模式时增加参数Pattern.MULTILINE即可

示例功能:
1、多行模式匹配字符串开头
2、多行模式匹配字符串结尾

/** * 多行模式 * @date 2016-04-20 15:31:19 * @author sgl */
    public static void multiline(){
        //注意里面的换行符
        String str="hello worldrnhello javarnhello java";

        System.out.println("===========匹配字符串开头(多行模式)===========");
        Pattern p=Pattern.compile("^hello",Pattern.MULTILINE);
        Matcher m=p.matcher(str);
        while(m.find()){
            System.out.println(m.group()+" 位置:["+m.start()+","+m.end()+"]");
        }

        System.out.println("===========匹配字符串结尾(多行模式)===========");
        p=Pattern.compile("java$",Pattern.MULTILINE);
        m=p.matcher(str);
        while(m.find()){
            System.out.println(m.group()+" 位置:["+m.start()+","+m.end()+"]");
        }

    }

运行结果:

===========匹配字符串开头(多行模式)=========== hello   位置:[0,5]
hello   位置:[13,18]
hello   位置:[25,30]
===========匹配字符串结尾(多行模式)=========== java   位置:[19,23]
java   位置:[31,35]

(编辑:李大同)

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

    推荐文章
      热点阅读