是否可以检测并删除句子中的任何类型的 URL?
例如:
Today,wheather is cold.But I want to out. http://weathers.com..... And I will take a cup of tea...
应该变成
Today,wheather is cold.But I want to out. And I will take a cup of tea...
最佳答案
这取决于您希望匹配过程的全面程度。您可以尝试使用像
这样简单的东西str.replaceAll("http://[^\\s]+", "")
例如
System.out.println("Today,wheather is cold.But I want to out. "
+ "http://weathers.com..... And I will take a cup of tea..."
.replaceAll("http://[^\\s]+", ""));
Today,wheather is cold.But I want to out. And I will take a cup of tea...
如果您想要更强大的东西来匹配有效的 URL,请使用更完整的 URL 正则表达式:
/^(https?:\/\/)?([\da-z\.-]+)\.([a-z\.]{2,6})([\/\w \.-]*)*\/?$/
要获得更彻底的匹配,请参阅 this回答。
关于java - 如何检测和删除 URL 中的一句话?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17407944/