RegEx匹配从多行到字符串缓冲区的末尾并替换为“”

时间:2013-10-16 22:50:04

标签: java regex

我有以下文件内容,我正在尝试匹配下面解释的reg并将匹配的开头(“On .... write”)替换为字符串缓冲区的末尾,并带有空白“” :

-- file.txt (Before regx match and replace) -- 
test

On blah

more blah wrote:

So, this should be stripped all out and all that left should be the above test contents.
-- EOF -- 


-- file.txt (After regex mach and replace) -- 
test
-- EOF -- 

如果我从上面读取文件内容到一个字符串并尝试匹配“On ... write:”部分我似乎无法替换文件的末尾“On ...写道: ” ...

    // String text = <file contents from above...the Before contents>
    Pattern PATTERN = 
      Pattern.compile("^(On\\s(.+)wrote:)$", Pattern.MULTILINE | Pattern.DOTALL );
    Matcher m = PATTERN.matcher(text);
    if (m.find()) {
       // This matches but I want to strip from "On....wrote:  -> <end of string>
       text = m.replaceAll("");  // This should only contain "test"

    }

1 个答案:

答案 0 :(得分:2)

你不需要做匹配,你可以直接替换。如果替换中使用的模式与任何东西都不匹配,那么什么都不会发生。

尝试以下方法:

// String text = <file contents from above...the Before contents>
String text = text.replaceAll("^(On.*?wrote:).*$", "");

注意:您可能需要从正则表达式中转换Pattern.MULTILINEPattern.DOTALL的标记,您可以这样做:

String text = text.replaceAll("(?sm)^(On.*?wrote:).*$", "");

编辑:当然可以:

// String text = <file contents from above...the Before contents>
Pattern PATTERN = 
  Pattern.compile("^(On.*?wrote:).*$", Pattern.MULTILINE | Pattern.DOTALL );
Matcher m = PATTERN.matcher(text);
if (m.find()) {
   text = m.replaceAll("");  // This should only contain "test"

}