Java正则表达式不会删除点
我正在尝试删除文本中的“.”,并将其替换为“.” 我的代码:Java正则表达式不会删除点,java,regex,Java,Regex,我正在尝试删除文本中的“.”,并将其替换为“.” 我的代码: System.out.println(TextHandler.class.toString() + " removeExcessiveSpaces E2 " + text); while (text.contains("\\. \\.")) { text = text.replaceAll("\\. \\.", "."); } System.out.println(TextHandler.class.toString() +
System.out.println(TextHandler.class.toString() + " removeExcessiveSpaces E2 " + text);
while (text.contains("\\. \\.")) {
text = text.replaceAll("\\. \\.", ".");
}
System.out.println(TextHandler.class.toString() + " removeExcessiveSpaces E3 " + 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["
“从街上的健康状况来看,但你知道的……这只是女性停止玩耍的另一个例子,。在其他方面,这完全是一个假期的结果。截至2006年7月4日,m-UH……8.42////
(与输入无差异)
它为什么不工作?String#contains
不期望正则表达式只是普通字符串
因此,请使用:
if (text.contains(". .")) {
text = text.replaceAll("\\. \\.", ".");
}
或者简单地使用String#replace
:
text = text.replace(". .", ".");
试试这个:
text = text.replace(". .", ".");
我希望它能对您有所帮助!publicstaticvoidmain(String[]args){
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);
}
String text=“from the streets'fit,但你知道的..这只是女性停止玩耍的另一个例子,.在一个假期的其他总结果中.截至2006年7月4日..8.42………/包含
不使用正则表达式,所以你应该使用包含(“\\.\\”)
但是用一个而不是低效的
替换一系列的..
while (text.contains(". .")) {
text = text.replaceAll("\\. \\.", ".");
}
因为每次迭代都需要从字符串的开头开始,所以可以使用
text = text.replaceAll("\\.( \\.)+", ".");
我想你指的是text.replace()
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("\\. \\.", ".");
}
text = text.replaceAll("\\.( \\.)+", ".");