A regular expression for two or more points must be separated as a point

I have such inputs.

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 points to one point, and then a space, so that the input becomes correct according to the end of the grammar. Final result:

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

I made a regex for this:

[\\.\\.]+

I tested it on Regex Tester , it will not work as I wanted. Since it will also include the 0.69completion of this line .too, which I do not want. If anyone can help me with this, I will be grateful to you.

+4
source share
5 answers

You can use:

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

RegEx Demo

\\.{2,} ". " .

+4

[\.]+[\.]+

2 .

+1

:

String str = "And I walked 0.69 miles. I had a burger..I took tea...I had a coffee";
String result = str.replaceAll("\\.{2,}", ". ");

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

. , 1+ \.

\.\.+ or Java string escaped \\.\\.+

[\. \.] \. . .

+1

String.replaceAll() :

myString.replaceAll("\\.[\\.]+", ". ");

regex \.[\.]+. Java \, \\.[\\.]+...

0

Source: https://habr.com/ru/post/1675205/


All Articles