private String changeUrl(String content){
String regex = "(http:|https:)//[^[A-Za-z0-9\\._\\?%&+\\-=/#]]*";
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(content);
StringBuffer result = new StringBuffer();
while (matcher.find()) {
String urlStr=matcher.group();
StringBuffer replace = new StringBuffer();
replace.append("<a href=\""+urlStr+"\"");
replace.append(" onClick=\""+"window.open('"+urlStr+"')\""+">"+urlStr+"</a>");
matcher.appendReplacement(result, replace.toString());
}
matcher.appendTail(result);
System.out.println(result);
return result.toString();
}
java处理文本中的http/https链接
最新推荐文章于 2022-08-08 00:39:00 发布