我有以下文件内容,并且我试图在每行的开头匹配一个字符的连续块(特别是'>')的reg-ex并删除该匹配文本块:
-- 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("");
}
答案 0 :(得分:2)
您需要匹配行尾(\n
),而不仅仅是($
),以便完全摆脱这些行。
答案 1 :(得分:2)
如@Peter Alfvin
所述,您需要在替换中加入换行符\n
。
text = text.replaceAll("(?m)^>[^>]*?\n", "");
正则表达式:
(?m) set flags for this block (with ^ and $ matching start and end of line)
^ the beginning of a "line"
> '>'
[^>]*? any character except: '>' (0 or more times)
(matching the least amount possible))
\n '\n' (newline)
(?m)
修饰符(多行)会导致^
和$
匹配每行的开头/结尾。
请参阅working demo