正则表达式从多行匹配到字符串缓冲区的末尾,并替换为" "



我有以下文件内容,我正在尝试匹配下面解释的注册并替换直到比赛开始("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...写道:"部分我似乎无法从"在...写道:"...

    // 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"
    }

你不需要做匹配,你可以直接替换。如果替换中使用的模式与任何内容不匹配,则不会发生任何事情。

请尝试以下操作:

// 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"
}

最新更新