I want to fix invalid ellipses ( ... ) in String .
"Hello.. World.." "Hello... World..." // this is correct "Hello.... World...." "Hello..... World....."
should be fixed:
"Hello... World..."
The following regular expression processes any instance of 3 or more consecutive ones . ':
line.replaceAll("\\.{3,}", "...");
However, I do not know how to handle the case when there are exactly 2 consecutive ones . . We cannot do something like this:
line.replaceAll("\\.{2}", "...");
For example, for "..." the above code will return "......" because the regular expression will replace the first 2 . (index 0 and 1), then the following 2 . (indices 1 and 2), which leads to "..." + "..." = "......" .
Something like this works:
line.replaceAll("\\.{2}", "...").replaceAll("\\.{3,}", "...");
... but there must be a better way!
source share