how to replace plain url with hyperlinks in html doc

I am trying to replace plain link to hyperlink in an html document. and my logic is

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 =;
                    Matcher proto = WEB_URL_PROTOCOL.matcher(url);
                    String link;
                    if (proto.find()) {
                        lower case protocol link.
                        link = + 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");

This code is successfully find out all links in a html doc .but problem is it also find the hyperlink.So i want to exclude the hyperlinks and want to find only plain links for example it should exclude

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

but plain link should be replaced by a hyperlink

Edit if doc contain text like this - 1.

2. normal address https

so here i want to replace with

<p class="MsoNormal"><a href = "></a></p>

and it should not effect 2 at all .


I would recommand you to use Jsoup here.

Sample code
String text = "<html><head></head><body><a href=''>Don't change this link</a> Change this:</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) {

    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("AFTER:\n\n" + newHtmlCode);

  <a href="">Don't change this link</a> Change this:

  <a href="">Don't change this link</a>
  <span> Change this: <a href=""></a> <a href=""></a></span>

Need Your Help

Radio button value is not null

jquery html forms

I'm making a website with a wizard-like web form, split up in multiple fieldset. Because of this, the required attribute for input doesn't work until you reach the last slide, where the complete fo...