从String中删除所有换行符但在点(。)后保留一个换行符

时间:2014-07-22 08:59:55

标签: java string newline

我有这个输入字符串(包含换行符):

That is
a test.
seems to work pretty good? working. Another test again.

我希望达到这种状态:

That is a test.
seems to work pretty good? working. Another test again.

有什么想法吗?

我正在尝试这个:

replaceAll("[^\\.]\\n", "");

1 个答案:

答案 0 :(得分:0)

您可以使用lookbehind(请参阅documentation)。这将查看是否存在非点字符而未实际替换该字符(从而删除最后一个字的一部分)。此外,您应该用空格替换换行符,以便不加入单词。

text.replaceAll("(?<=[^\\.])\\n", " ")

或者,您可以捕获组中的非点字符并将其重新插入替换中:

text.replaceAll("([^\\.])\\n", "$1 ")

两者都会给你

That is a test.
seems to work pretty good? working. Another test again.