Reputation: 1222
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.
Upvotes: 4
Views: 3257
Reputation: 13222
You can use String.replaceAll()
to accomplish this:
myString.replaceAll("\\.[\\.]+", ". ");
Your regex
is slightly wrong also it should be \.[\.]+
. In Java
you will have to escape the \
so it will be \\.[\\.]+
...
Upvotes: 0
Reputation: 8833
You need to pull the first \. out to match that, then 1+ \. so like
\.\.+ or Java string escaped \\.\\.+
[\.\.] Will match \ or . or \ or . witch is a redundant check on a single character.
Upvotes: 1
Reputation: 59988
You can use this :
String str = "And I walked 0.69 miles. I had a burger..I took tea...I had a coffee";
String result = str.replaceAll("\\.{2,}", ". ");
Output
And I walked 0.68 miles. I had a burger. I took tea. I had a coffee
Upvotes: 2
Reputation: 2639
This code should work
[\.]+[\.]+
This will match 2 or more continuous periods. You can then replace each match with a period and space
Upvotes: 1
Reputation: 785196
You can use:
str = str.replaceAll("\\.{2,}", ". ");
\\.{2,}
matches 2 or more consecutive dots and ". "
replaced them by a dot and space.
Upvotes: 6