这是一个简单的问题,但我不明白.我想检测字符串中的url并用缩短的字符串替换它们.
我从stackoverflow中找到了这个表达式,但结果却是 http
Pattern p = Pattern.compile("\\b(https?|ftp|file)://[-a-zA-Z0-9+&@#/%?=~_|!:,.;]*[-a-zA-Z0-9+&@#/%=~_|]",Pattern.CASE_INSENSITIVE);
Matcher m = p.matcher(str);
boolean result = m.find();
while (result) {
for (int i = 1; i <= m.groupCount(); i++) {
String url=m.group(i);
str = str.replace(url, shorten(url));
}
result = m.find();
}
return html;
Run Code Online (Sandbox Code Playgroud)
有什么好主意吗?