如何用html doc中的超链接替换普通网址

时间:2014-01-29 12:26:40

标签: java html regex hyperlink regex-negation

我正在尝试将纯链接替换为html文档中的超链接。 我的逻辑是

private static final Pattern WEB_URL_PROTOCOL = Pattern.compile("(?i)http|https://");

StringBuffer sb = new StringBuffer();
        if (text != null) {
            // Escape any inadvertent HTML in the text message
            text = EmailHtmlUtil.escapeCharacterToDisplay(text);
            // Find any embedded URL's and linkify
              Matcher m = Patterns.WEB_URL.matcher(text);


            while (m.find()) {
                int start = m.start();

                if (start == 0 || text.charAt(start - 1) != '@') {
                    String url = m.group();
                    Matcher proto = WEB_URL_PROTOCOL.matcher(url);
                    String link;
                    if (proto.find()) {
                        lower case protocol link.
                        link = proto.group().toLowerCase() + url.substring(proto.end());
                    } else {

                        link = "http://" + url;
                    }
                    String href = String.format("<a href=\"%s\">%s</a>", link, url);
                    m.appendReplacement(sb, href);
                }
                else {
                    m.appendReplacement(sb, "$0");
                }
            }
            m.appendTail(sb);
        }

此代码成功找到html doc中的所有链接。但问题是它还找到了超链接。所以我想要排除超链接并想要只查找纯链接 例如,它应该排除

<p class="MsoNormal"><a href="awbs://www.google.com" target="_BLANK">https://www.google.com</a> normal address https</p> 

但普通链接https://www.google.com应替换为超链接

编辑 如果doc包含这样的文本 - 1. https://www.yahoo.com

2. https://www.google.com普通地址https

所以我想用

替换https://www.yahoo.com
<p class="MsoNormal"><a href = "https://www.yahoo.com>https://www.yahoo.com</a></p>

它根本不应该影响2。

1 个答案:

答案 0 :(得分:1)

我建议你在这里使用Jsoup

示例代码

String text = "<html><head></head><body><a href='http://google.com'>Don't change this link</a> Change this: http://yahoo.com foo.com</body></html>";
Document d = Jsoup.parse(text);
String newHtmlCode = "";
String oldHtmlCode = d.outerHtml();
List<TextNode> textNodes = d.body().textNodes();

Matcher m = Patterns.WEB_URL.matcher("");
for (TextNode textNode : textNodes) {
    m.reset(textNode.text());

    String fragment = "";
    while (m.find()) {
        fragment = m.replaceAll("<a href=\"\\*\\*\\*$1\">$1</a>");
        textNode.replaceWith(new Element(Tag.valueOf("span"),"").html(fragment));
    }

    newHtmlCode = d.outerHtml().replaceAll("\"\\Q***\\E(?!https?://)", "\"http://").replaceAll("\"\\Q***\\E(https?://)", "\"$1");
}

System.out.println("BEFORE:\n\n" + oldHtmlCode);
System.out.println("----------------------------");
System.out.println("AFTER:\n\n" + newHtmlCode);

输出

BEFORE:

<html>
 <head></head>
 <body>
  <a href="http://google.com">Don't change this link</a> Change this: http://yahoo.com foo.com
 </body>
</html>
----------------------------
AFTER:

<html>
 <head></head>
 <body>
  <a href="http://google.com">Don't change this link</a>
  <span> Change this: <a href="http://yahoo.com">http://yahoo.com</a> <a href="http://foo.com">foo.com</a></span>
 </body>
</html>