Fix invalid ellipses in a string

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!

+6
source share
4 answers

You can replace any group of two or more . :

 [.]{2,} 

with ...

+5
source

Why not keep it simple?

 \.\.+ 

If you really don't want him to mess with groups of 3, this is:

 \.{4,}|(?<!\.)\.{2}(?!\.) 

What this does is search for groups that are more than 3, then it searches for groups of 2. The special thing about "..." is that there are two groups ".." in "...". So, in (?!\.) You are looking for a third "." . after the first 2. If this is the third "." exists then discard this result. This is called a negative look. To undo the second "..", you must execute a negative lookbehind. So, (?<!\.) Is looking for this "." before the second ".." and this result is discarded if found.

A negative lookbehind cannot be executed by javascript, so I used the one that uses the Java compiler.

Link: https://www.myregextester.com/?r=d41b2f7e

+3
source

You can do this by negating, finding everything that is not one or more words, followed by the “correct” ellipse, and correct the ellipse using a regular expression, for example:

 line.replaceAll("[^\\w* ]+([.]{1,})", "...") 

this has the advantage that it does not replace another punctuation than irregular ellipses.

+1
source

You need something like below (in unescaped form):

 (?<!\.)\.{2}(?!\.)|\.{4,} 

Internet examples

In principle, in the point-to-point case, negative lookbehind and lookahead are used to prevent their three-point sequence from matching.

+1
source

All Articles