簡體   English   中英

在正則表達式匹配中不替換換行符?

[英]Newline not being replaced in regex match?

我有以下代碼嘗試解析以#開頭的行並用空字符串替換每一行,但換行不會被剝離:

- 輸入 -

# Remove this.
# remove this
# remove this line
keep # this line
keep this # line too

- 實際輸出(。代表空白行) -

.   
.
.
keep # this line
keep this # line too

- 預期產量

keep # this line
keep this # line too

- 代碼 -

    String text = "# Remove this.\n# remove this\n# remove this line\n" +
        "keep # this line\nkeep this # line too";
    System.out.println(text);
    Pattern PATTERN = Pattern.compile("^#(.*)$", Pattern.MULTILINE);
    Matcher m = PATTERN.matcher(text);
    if (m.find()) {
        text = m.replaceAll("").replaceAll("[\n]+$", "");
        System.out.println(text);
    }

如何刪除正則表達式匹配中的\\ n?

使用^#.*[\\n]選擇包含換行符的行。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM