我正在尝试删除“。” 在文本中,并将其替换为“。”。
我的代码:
System.out.println(TextHandler.class.toString() + " removeExcessiveSpaces E2 " + text);
while (text.contains("\\. \\.")) {
text = text.replaceAll("\\. \\.", ".");
}
System.out.println(TextHandler.class.toString() + " removeExcessiveSpaces E3 " + text);
Run Code Online (Sandbox Code Playgroud)
文字输入:
"from the streets' fit but you know it. . this is just another case of female stopping play,. in an otherwise total result of a holiday. by m-uhjuly 04, 2006. . 8 . 42 . .. .... . . . . . . . . <script>//<![cdata["
Run Code Online (Sandbox Code Playgroud)
预期产量:
"from the streets' fit but you know it this is just another case of female stopping play,. in an otherwise total result of a holiday. by m-uhjuly 04, 2006. . 8 . 42 .. <script>//<![cdata["
Run Code Online (Sandbox Code Playgroud)
观察到的输出:
from the streets' fit but you know it. . this is just another case of female stopping play,. in an otherwise total result of a holiday. by m-uhjuly 04, 2006. . 8 . 42 . .. .... . . . . . . . . <script>//<![cdata[
Run Code Online (Sandbox Code Playgroud)
(与输入没有区别)
为什么不起作用?
String#contains 不希望正则表达式只是纯字符串。
因此使用:
if (text.contains(". .")) {
text = text.replaceAll("\\. \\.", ".");
}
Run Code Online (Sandbox Code Playgroud)
或简单地使用String#replace:
text = text.replace(". .", ".");
Run Code Online (Sandbox Code Playgroud)