Java regex does not remove dots
I am trying to remove "." in the text and replace it with ".".
My code:
System.out.println(TextHandler.class.toString() + " removeExcessiveSpaces E2 " + text);
while (text.contains("\\. \\.")) {
text = text.replaceAll("\\. \\.", ".");
}
System.out.println(TextHandler.class.toString() + " removeExcessiveSpaces E3 " + text);
text input:
"from the streets' fit but you know it. . this is just another case of female stopping play,. in an otherwise total result of a holiday. by m-uhjuly 04, 2006. . 8 . 42 . .. .... . . . . . . . . <script>//<![cdata["
Expected Result:
"from the streets' fit but you know it this is just another case of female stopping play,. in an otherwise total result of a holiday. by m-uhjuly 04, 2006. . 8 . 42 .. <script>//<![cdata["
Observable output:
from the streets' fit but you know it. . this is just another case of female stopping play,. in an otherwise total result of a holiday. by m-uhjuly 04, 2006. . 8 . 42 . .. .... . . . . . . . . <script>//<![cdata[
(no difference from input)
Why isn't it working?
+3
source to share
4 answers
public static void main(String[] args) {
String text = "from the streets' fit but you know it. . this is just another case of female stopping play,. in an otherwise total result of a holiday. by m-uhjuly 04, 2006. . 8 . 42 . .. .... . . . . . . . . <script>//<![cdata[";
while (text.contains(". .")) {
text = text.replaceAll("\\. \\.", ".");
}
System.out.println(text);
}
Output
from the streets' fit but you know it. this is just another case of female stopping play,. in an otherwise total result of a holiday. by m-uhjuly 04, 2006. 8 . 42 ..... <script>//<![cdata[
+1
source to share
contains
doesn't use regex, so contains("\\. \\.")
you should use instead contains(". .")
.
But replace the series . . . . .
with one .
instead of the ineffective
while (text.contains(". .")) {
text = text.replaceAll("\\. \\.", ".");
}
because on each iteration it needs to start from the beginning of the line, you can use
text = text.replaceAll("\\.( \\.)+", ".");
0
source to share