【发布时间】:2011-08-08 11:39:14
【问题描述】:
这是一个简单的问题,但我就是不明白。 我想检测字符串中的 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;
有没有更好的办法?
【问题讨论】: