• Java经典实例:在文本中匹配换行符


    默认情况下,正则表达式 ^ 和 $ 忽略行结束符,仅分别与整个输入序列的开头和结尾匹配。如果激活 MULTILINE 模式,则 ^输入的开头行结束符之后(输入的结尾)才发生匹配。处于 MULTILINE 模式中时,$ 仅在行结束符之前输入序列的结尾处匹配。

    import java.util.regex.Pattern;
    
    /**
     * Created by Frank
     * 使用正则表达式在文本中查找换行符
     */
    public class NLMatch {
        public static void main(String[] args) {
            String input = "I dream of engines
    more engines, all day long";
            System.out.println("INPUT:" + input);
            System.out.println();
            String[] patt = {"engines.more engines", "ines
    more", "engines$"};
            for (int i = 0; i < patt.length; i++) {
                System.out.println("PATTERN:" + patt[i]);
                boolean found;
                Pattern p1l = Pattern.compile(patt[i]);
                found = p1l.matcher(input).find();
                System.out.println("DEFAULT match " + found);
                // .代表任何符号(DOT ALL),
                Pattern pml = Pattern.compile(patt[i], Pattern.DOTALL | Pattern.MULTILINE);
                found = pml.matcher(input).find();
                System.out.println("Multiline match " + found);
                System.out.println();
            }
        }
    }
  • 相关阅读:
    自动化测试模型介绍
    接口测试
    adb 命令
    测试思路
    软件测试基础
    页面元素定位
    环境搭建
    自动化
    使用python操作mysql数据库
    mysql索引原理
  • 原文地址:https://www.cnblogs.com/frankyou/p/6050892.html
Copyright © 2020-2023  润新知