java处理文本中的http/https链接

来源:互联网 发布:emacs for windows 编辑:程序博客网 时间:2024/05/18 00:20
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();    }

原创粉丝点击