Most probably your lines are not separated only with \n
but with \r\n
. You can try with \r?\n
to optionally add \r
before \n
. Lets also not forget about last b
which doesn't have any line separators after it. To handle it you need to add $
in your regex which means anchor representing end of your data. So your final pattern could look like
in.replaceAll(" d+(\r?\n|$)", "")
In case you don't want to remove these line separators you can use "end of line anchor" $
with MULTILINE
flag (?m)
instead of line separators like
in.replaceAll("(?m) d+$", "")
especially because there are no line separators after last b
.
In Java, when MULTILINE
flag is specified, $
will match the empty string:
- Before a line terminator:
- A carriage-return character followed immediately by a newline character (
"\r\n"
)
- Newline (line feed) character (
'\n'
) without carriage-return ('\r'
) right in front
- Standalone carriage-return character (
'\r'
)
- Next-line character (
'\u0085'
)
- Line-separator character (
'\u2028'
)
- Paragraph-separator character (
'\u2029'
)
- At the end of the string
When UNIX_LINES
flag is specified along with MULTILINE
flag, $
will match the empty string right before a newline ('\n'
) or at the end of the string.
Anyway if it is possible don't use regex with HTML
.