java - Newline not being replaced in regex match? -
i have following code attempting parse lines starting # , replacing each line empty string, newline not getting stripped:
-- input --
# remove this. # remove # remove line keep # line keep # line
-- actual output (the . represents blank line) --
. . . keep # line keep # line
-- expected output
keep # line keep # line
-- code --
string text = "# remove this.\n# remove this\n# remove line\n" + "keep # line\nkeep # 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); }
how can remove \n's in regex match?
use ^#.*[\n]
select lines including newline character.
Comments
Post a Comment