【问题标题】:how to replace plain url with hyperlinks in html doc如何用html doc中的超链接替换纯url
【发布时间】:2014-02-21 06:11:24
【问题描述】:

我正在尝试将普通链接替换为 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 文档中的所有链接。但问题是它也找到了超链接。所以我想排除超链接并只想找到普通链接 例如它应该排除

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

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

编辑 如果文档包含这样的文本 - 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 。

【问题讨论】:

    标签: java html regex hyperlink regex-negation


    【解决方案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>
    

    【讨论】:

    • 是否可以不使用解析器。我正在寻找正则表达式。
    • @Subham 检查我的答案 我使用正则表达式来查找纯链接。 Jsoup 相当快。
    • @Subham 检查我的新答案。
    猜你喜欢
    • 2010-09-07
    • 2020-07-21
    • 1970-01-01
    • 2012-01-14
    • 2013-11-02
    • 2010-12-29
    相关资源
    最近更新 更多