首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > 编程 >

JAVA运用正则表达式把文本中的URL内容转换为链接

2012-09-05 
JAVA使用正则表达式把文本中的URL内容转换为链接/*** URL转换为链接* @author Boyer* @param urlText* @re

JAVA使用正则表达式把文本中的URL内容转换为链接

    /**     * URL转换为链接     * @author Boyer     * @param urlText     * @return String     */    public static String urlToLink(String urlText){        // 匹配的条件选项为结束为空格(半角和全角)、换行符、字符串的结尾或者遇到其他格式的文本        String regexp            = "(((http|ftp|https|file)://)|((?<!((http|ftp|https|file)://))www\\.))"  // 以http...或www开头            + ".*?"                                                                   // 中间为任意内容,惰性匹配            + "(?=(&nbsp;|\\s| |<br />|$|[<>]))";                                     // 结束条件        Pattern pattern = Pattern.compile(regexp, Pattern.CASE_INSENSITIVE);        Matcher matcher = pattern.matcher(urlText);        StringBuffer stringbuffer = new StringBuffer();        while(matcher.find()){            String url = matcher.group().substring(0, 3).equals("www") ? "http://" + matcher.group() : matcher.group();            String tempString = "<a href="" + url + "">" + matcher.group() + "</a>";            // 这里对tempString中的""和"$"进行一次转义,因为下面对它替换的过程中appendReplacement将""和"$"作为特殊字符处理            int tempLength = tempString.length();            StringBuffer buffer = new StringBuffer();            for(int i = 0; i < tempLength; ++i){                char c = tempString.charAt(i);                if(c == '\\' || c == '$'){                    buffer.append("\").append(c);                } else {                    buffer.append(c);                }            }            tempString = buffer.toString();            matcher.appendReplacement(stringbuffer, tempString);        }        matcher.appendTail(stringbuffer);        return stringbuffer.toString();    }    /**     * 文本中含有URL的内容,画面表示为链接     * @author Boyer     * @param note     * @return     */    public static String textToLinks(String note) {        // 转换的思想为把文本中不是链接("/a>"和"<a "之间)的内容逐个进行转换        // 把字符串中的""和"$"加上转义符,避免appendReplacement替换字符串的时候将它们作为特殊字符处理        int noteLength = note.length();        StringBuffer buffer = new StringBuffer();        for(int i = 0; i < noteLength; ++i){            char c = note.charAt(i);            if(c == '\\' || c == '$'){                buffer.append("\").append(c);            } else {                buffer.append(c);            }        }        String linkNote = "/a>" + buffer.toString() + "<a ";        String regexp = "(?<=/a>).*?(?=<a )";        Pattern pattern = Pattern.compile(regexp, Pattern.CASE_INSENSITIVE);        Matcher matcher = pattern.matcher(linkNote);        StringBuffer stringbuffer = new StringBuffer();        while(matcher.find()){            String tempString = urlToLink(matcher.group());            matcher.appendReplacement(stringbuffer, tempString);        }        matcher.appendTail(stringbuffer);        String result = stringbuffer.toString();        // 返回的结果去掉加入的"/a>" 和"<a "        return result.substring(3, result.length() - 3);    }

热点排行