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 = 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);
}
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://www.google.com" target="_BLANK">https://www.google.com</a> normal address https</p>
but plain link https://www.google.com should be replaced by a hyperlink
Edit if doc contain text like this - 1. https://www.yahoo.com
2. https://www.google.com normal address httpsso here i want to replace https://www.yahoo.com with
<p class="MsoNormal"><a href = "https://www.yahoo.com>https://www.yahoo.com</a></p>
and it should not effect 2 at all .