Java – fix malformed ellipses in strings
•
Java
I want to fix malformed ellipses (...) in string
"Hello.. World.." "Hello... World..." // this is correct "Hello.... World...." "Hello..... World....."
Should be corrected to:
"Hello... World..."
The following regular expression handles any instance of 3 or more consecutive instances
line.replaceAll("\\.{3,}","...");
However, when two consecutive I don't know how to deal with this situation We can't do such a thing:
line.replaceAll("\\.{2}","...");
For example, for "...", the above code will return "...", because the regular expression will replace the first two (indexes 0 and 1), followed by the next 2 (index 1 and 2), resulting in "..." "..." = "..."
Something like this works:
line.replaceAll("\\.{2}","...").replaceAll("\\.{3,"...");
... but there must be a better way!
Solution
You can replace any of two or more groups.:
[.]{2,}
With
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
二维码