Java regular expressions do not delete points

I am trying to delete "..." in the text and replace it with "."

My code:

System.out.println(TextHandler.class.toString() + " removeExcessiveSpaces E2 " + text);
while (text.contains("\\. \\.")) {
    text = text.replaceAll("\\. \\.",".");
}
System.out.println(TextHandler.class.toString() + " removeExcessiveSpaces E3 " + text);

Text input:

"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["

Expected production:

"from the streets' fit but you kNow it this is just another case of female stopping play,2006. . 8 . 42 .. <script>//<![cdata["

Observed output:

from the streets' fit but you kNow it. . this is just another case of female stopping play,2006. . 8 . 42 . .. .... . . . . . . . . <script>//<![cdata[

(no difference from input)

Why not work?

Solution

String #contains does not expect regular expressions to be just plain strings

So use:

if (text.contains(". .")) {
    text = text.replaceAll("\\. \\.",".");
}

Or just use string #replace:

text = text.replace(". .",".");
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>