Java 如何从字符串中删除此url?
我有以下字符串:Java 如何从字符串中删除此url?,java,regex,Java,Regex,我有以下字符串: String description= errex for Screen Share https://ednovo.webex.com/ednovo/j.php?ED=224466857&UID=1465621292&RT=MiM0 You can find the meeting notes here https://docs.yahoo.com/a/filter.org/document/d/1Luf_6Q73_Lm30t3x6wHS_4Ztkn7HfXD
String description= errex for Screen Share
https://ednovo.webex.com/ednovo/j.php?ED=224466857&UID=1465621292&RT=MiM0
You can find the meeting notes here https://docs.yahoo.com/a/filter.org/document/d/1Luf_6Q73_Lm30t3x6wHS_4Ztkn7HfXDg4sZZWz-CuVw/edit?usp=sharing
我想删除url链接并以以下内容结束:
String description=errex for Screen Share You can find the meeting notes here
我尝试了以下代码,但未检测到URL:
private String removeUrl(String commentstr)
{
String commentstr1=commentstr;
String urlPattern = "((https?|ftp|gopher|telnet|file|Unsure|http):((//)|(\\\\))+[\\w\\d:#@%/;$()~_?\\+-=\\\\\\.&]*)";
Pattern p = Pattern.compile(urlPattern,Pattern.CASE_INSENSITIVE);
Matcher m = p.matcher(commentstr1);
int i=0;
while (m.find()) {
commentstr1=commentstr1.replaceAll(m.group(i),"").trim();
i++;
}
System.out.println("After url filter" +commentstr1);
return commentstr1;
}
这里出了什么问题?这将删除URL:
description = description.replaceAll("https?://\\S+\\s?", "");
顺便说一句,结尾的小
\\s?
确保在URL从两个空格之间删除后不会得到两个空格。如果您确定URL不能有空格,您可以轻松拆分字符串。然后,您可以使用正则表达式解析所有生成的数组,或者简单地捕获(并忽略)包含特殊字符的字符串。如果你觉得合适的话,我可以安排一节简单的课。看看这个问题
String description= "errex for Screen Share https://ednovo.webex.com/ednovo/j.php?ED=224466857&UID=1465621292&RT=MiM0 " +
"You can find the meeting notes here https://docs.yahoo.com/a/filter.org/document/d/1Luf_6Q73_Lm30t3x6wHS_4Ztkn7HfXDg4sZZWz-CuVw/edit?usp=sharing";
System.out.println(description.replaceAll("\\S+://\\S+", ""));