我有以下文件内容,我正在尝试匹配下面解释的 reg,并将匹配的开头(“On....wrote”)替换为字符串缓冲区的结尾,并用空白“”:
-- 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...wrote:”部分,我似乎无法从“On ... write:”替换文件末尾。 .:
// 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"
}