java - 如何检测和删除 URL 中的一句话?

标签 java regex string parsing url

是否可以检测并删除句子中的任何类型的 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/

相关文章:

javascript - 用于 JavaScript 中非特定版本控制的正则表达式

javascript - 使用 javascript 正则表达式删除多个反斜杠,同时保留\n 特殊字符

JavaScript 语法错误 : invalid regular expression

java - 使用字符串数组将字符串转换为不同的字符

Java String 为空,虽然打印出来并且之前不为空

SceneBuilder 中的 JavaFX 和 TableView

java - 使库使用 jpa 注释而不依赖于任何实现

javascript - 如何将 PostgreSQL 数组转换为 JS

java - 返回值: boolean vs int flag vs exception

java - Android Studio : Duplicate files copied in APK META-INF/LICENSE when compile(exclude did not work)