如何匹配'>'的连续字符块在换行?

时间:2013-11-04 01:26:02

标签: java regex

我有以下文件内容,并且我试图在每行的开头匹配一个字符的连续块(​​特别是'>')的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("");  

    }

2 个答案:

答案 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