匹配多行上的正则表达式不起作用

时间:2013-10-08 00:54:01

标签: java regex

我有以下文件内容,我正在尝试匹配下面解释的注册表:

-- file.txt (doesn't match multi-line) -- 
test

On blah

more blah wrote:
---------------

如果我从上面读取文件内容到String并尝试匹配“On ... write:”部分我无法得到匹配:

    // String text = <file contents from above>
    Pattern PATTERN = Pattern.compile("^(On\\s(.+)wrote:)$", Pattern.MULTILINE);
    Matcher m = PATTERN.matcher(text);
    if (m.find()) {
       System.out.println("Never gets HERE???");
    }

如果文件内容在一行上,则上述正则表达式可以正常工作:

-- file2.txt (matches on single line) -- 
test

On blah more blah wrote: On blah more blah wrote:
---------------

如何在一个正则表达式中使用多行(单行)(或者两个)? THX!

2 个答案:

答案 0 :(得分:3)

Pattern.MULTILINE只是告诉Java接受锚点^$以匹配每行的开头和结尾。

添加Pattern.DOTALL标志以允许点.字符匹配换行符。这是使用bitwise inclusive OR |运算符

完成的
Pattern PATTERN = 
    Pattern.compile("^(On\\s(.+)wrote:)$", Pattern.MULTILINE | Pattern.DOTALL );

答案 1 :(得分:1)

您可以使用匹配\S非空白)和\s空白)的组合

Pattern PATTERN = Pattern.compile("(On\\s([\\S\\s]*?)wrote:)");

请参阅live regex101 demo

示例:

import java.util.regex.*;

class rTest {
  public static void main (String[] args) {
    String s = "test\n\n"
             + "On blah\n\n"
             + "more blah wrote:\n";
    Pattern p = Pattern.compile("(On\\s([\\S\\s]*?)wrote:)");
    Matcher m = p.matcher(s);
    if (m.find()) {
      System.out.println(m.group(2));
    }
  }
}