天天看点

获取前台内容中含有的链接,并转成超链接

//zj 加链接

String contents = "这是测试获取链接数据http://write.blog.csdn.net/postedit?ref=toolbar&ticket=ST-703940-wrpdwzcrwnn1EhoKCQ3v-passport.csdn.net";

String regex = "(http:|https:)//[^[A-Za-z0-9\\._\\?%&+\\-=/#]]*";

        Pattern pattern = Pattern.compile(regex);

        Matcher matcher = pattern.matcher(contents);

        StringBuffer result = new StringBuffer();

        while (matcher.find()) {

            String urlStr=matcher.group();

            StringBuffer replace = new StringBuffer();

            replace.append("<a href=\"").append(urlStr);

            replace.append("\" target=\"_blank\" style=\"color:#0000ff\">"+urlStr+"</a>");

            matcher.appendReplacement(result, replace.toString());

        }

        matcher.appendTail(result);

        //System.out.println(resultContents);

        String resultContents = result.toString();