Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regular Expression for two or more dots should be separated as dot space

Tags:

java

regex

I have inputs like this.

And I walked 0.69 miles. I had a burger..I took tea...I had a coffee

And my goal is to convert two or more dots to a single dot and then a space so that input become correct according to grammar proper ending. Target output is:

And I walked 0.68 miles. I had a burger. I took tea. I had a coffee

I have made a regular expression for this is as:

[\\.\\.]+

I checked it on Regex Tester it will not work as I wished. As it will also included 0.69 and ending of this line . too which I don't want. If anybody can help me in this I will be thankful to you.

like image 732
Hammad Hassan Avatar asked Jan 04 '23 03:01

Hammad Hassan


1 Answers

You can use:

str = str.replaceAll("\\.{2,}", ". ");

RegEx Demo

\\.{2,} matches 2 or more consecutive dots and ". " replaced them by a dot and space.

like image 93
anubhava Avatar answered Jan 14 '23 04:01

anubhava