我有一个输入String说Please go to http://stackoverflow.com。<a href=""></a>许多浏览器/ IDE /应用程序都会检测到字符串的url部分,并自动添加锚。就这样Please go to <a href='http://stackoverflow.com'>http://stackoverflow.com</a>。我需要使用Java进行相同的操作。
3 回答

Cats萌萌
TA贡献1805条经验 获得超9个赞
尽管不是Java专用的,但Jeff Atwood最近发布了一篇文章,介绍了在尝试查找和匹配任意文本中的URL时可能遇到的陷阱:
它提供了一个很好的正则表达式,可与需要正确(或多或少)处理parens的代码片段一起使用。
正则表达式:
\(?\bhttp://[-A-Za-z0-9+&@#/%?=~_()|!:,.;]*[-A-Za-z0-9+&@#/%=~_()|]
括号清除:
if (s.StartsWith("(") && s.EndsWith(")"))
{
return s.Substring(1, s.Length - 2);
}
添加回答
举报
0/150
提交
取消