我找到了使用模式匹配的解决方案。这是我的代码-
String str = "(?i)\b((?:https?://|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}/)(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:'".,<>?«»“”‘’]))";Pattern patt = Pattern.compile(str);Matcher matcher = patt.matcher(plain);plain = matcher.replaceAll("<a href="$1">$1</a>");这是输入和输出-
输入文字是可变的
plain:
some text and then the URL http://www.google.com and then some other text.
输出:
some text and then the URL <a href="http://www.google.com">http://www.google.com</a> and then some other text.



