首页 > 解决方案 > 在Java正则表达式中剪切多行文本的一部分

问题描述

我有以下文字:

(Some text before...)
display_name = lo
name = lo
is_up = 1
index = 1
is_virtual = 0
is_point_to_point = 0
MTU = 65536
hardware_address = (null)
parent = (null)
ip_counter = 2
inet6 addr = /::1
inet addr = /127.0.0.1
---------------End_of_text---------------------
(Some text after...)

我试图在“display_name = lo”和“---------------End_of_text------ ----",当 "display_name = lo" 和 "End_of_text" 不包括在内时。

我尝试运行以下命令:

String pattern = "^\\sdisplay_name = lo\\s+(.*)---------------End_of_text---------------------";

        Pattern r = Pattern.compile(pattern, Pattern.DOTALL);

        Matcher m = r.matcher(interfaceData);

        if (m.find()){
            System.out.println("Group 0 found value: " + m.group(0) );
            System.out.println("Group 1 found value: " + m.group(1) );
            System.out.println("Group 2 found value: " + m.group(2) );
        }
        else {System.out.println("NO MATCH");}

没有匹配,我错过了什么?

标签: javaregex

解决方案


推荐阅读