0

我有以下文件内容,我正在尝试在每行的开头为字符的连续块(​​特别是'>')匹配一个正则表达式,并删除该匹配文本块:

-- 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("");  

    }
4

2 回答 2

2

您需要匹配行尾 ( \n) 而不仅仅是匹配 ( $) 才能完全摆脱这些行。

于 2013-11-04T01:57:28.017 回答
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

于 2013-11-04T05:50:04.197 回答