4

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

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

On blah

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

如果我将文件内容从上面读取到字符串并尝试匹配“On...wrote:”部分,我无法得到匹配:

    // 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:
---------------

如何让多行工作和单行都在一个正则表达式中(或两个)?谢谢!

4

2 回答 2

3

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

添加Pattern.DOTALL标志以允许点.字符匹配换行符。这是使用按位包含 OR |运算符完成的

Pattern PATTERN = 
    Pattern.compile("^(On\\s(.+)wrote:)$", Pattern.MULTILINE | Pattern.DOTALL );
于 2013-10-08T01:04:45.240 回答
1

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

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

查看实时 regex101 演示

例子:

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));
    }
  }
}
于 2013-10-08T02:09:52.603 回答