【发布时间】:2015-07-20 12:04:57
【问题描述】:
我正在尝试使用 String.replace 将 url 替换为正则表达式,代码如下
public class Test {
public static void main(String[] args) {
String test = "https://google.com";
//String regex = "\\b(https?|ftp|file)://[-a-zA-Z0-9+&@#/%?=~_|!:,.;]*[-a-zA-Z0-9+&@#/%=~_|]";
String regex = "(http?|ftp|file)://[-a-zA-Z0-9+&@#/%?=~_|!:,.;]*[-a-zA-Z0-9+&@#/%=~_|]"; // does not match <http://google.com>
String newText = test.replace(regex, "");
System.out.println(newText);
}
}
我在 SO 中研究了几个关于它的问题,但它并没有取代模式。有人可以告诉我如何实现吗?
【问题讨论】:
-
您的正则表达式匹配 google.com ,确保将
http?更改为https? -
我没有看到任何替代品。你只是想删除google.com ?
-
@Pedro:非常抱歉。我忙于工作,所以这就是原因。非常感谢你的帮助。反正下次不会重复了
-
NP,已接受道歉,我将删除我的评论。