次のファイルの内容があり、各行の先頭にある連続する文字ブロック (具体的には「>」) の正規表現を照合し、一致するテキストのブロックを削除しようとしています。
-- file.txt (Before regx match and replace) --
keep this
> remove this
>
> remove this too
-- EOF --
-- file.txt (After regex mach and replace) --
keep this
-- EOF --
これを複数行に一致させようとしています(つまり、「>」で始まる行を削除します)。これは正しいですか、それとも最善のアプローチですか? 以下は動作していないようです。
// String text = <file contents from above...the Before contents>
Pattern PATTERN =
Pattern.compile("^>(.*)$", Pattern.MULTILINE);
Matcher m = PATTERN.matcher(text);
if (m.find()) {
// Matches each line starting with > and deletes (replaces with "") the line
text = m.replaceAll("");
}