It would be probably fully solvable if the contained URL always contained protocol (such as HTTP).
Because this is not the case, any "word", which contains . character can potentially be URL (for example mysite.com) and moreover you cannot be sure with teh actual protocol (you may assume).
If you assume that user will be always online, you may make a method that will take all potential URLs, checks if URL exists and if it does, then produce HTML link.
I have wroted this code snippet:
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.regex.*;
public class JavaURLHighlighter
{
Pattern potentialURLAtTheBeginning = Pattern.compile("^[^\\s]+\\.[^\\s]+\\s");
Pattern potentialURLintheMiddle = Pattern.compile("\\s[^\\s]+\\.[^\\s]+\\s");
Pattern potentialURLAtTheEnd = Pattern.compile("\\s[^\\s]+\\.[^\\s]+$");
private String urlString;
ArrayList<String> matchesList=new ArrayList<String>();
public String getUrlString() {
return urlString;
}
public void setUrlString(String urlString) {
this.urlString = urlString;
}
public void getConvertedMatches()
{
String match;
String originalMatch;
Matcher matcher;
matcher = potentialURLAtTheBeginning.matcher(urlString);
matchesList.clear();
while (matcher.find())
{
match = matcher.group().trim();
if (!match.startsWith("http://") && !match.startsWith("https://")) match = "http://"+match;
if (match.endsWith(".")) match=match.substring(0, match.length()-1);
if (urlExists(match)) matchesList.add(match);
}
matcher = potentialURLintheMiddle.matcher(urlString);
while (matcher.find())
{
match = matcher.group().trim();
if (!match.startsWith("http://") && !match.startsWith("https://")) match = "http://"+match;
if (match.endsWith(".")) match=match.substring(0, match.length()-1);
if (urlExists(match))matchesList.add(match);
}
matcher = potentialURLAtTheEnd.matcher(urlString);
while (matcher.find())
{
match = matcher.group().trim();
if (!match.startsWith("http://") && !match.startsWith("https://")) match = "http://"+match;
if (match.endsWith(".")) match=match.substring(0, match.length()-1);
if (urlExists(match)) matchesList.add(match);
}
for (int i=0; i< matchesList.size();i++) System.out.println(matchesList.get(i));
}
public static boolean urlExists(String urlAddress)
{
try
{
HttpURLConnection.setFollowRedirects(false);
HttpURLConnection connection = (HttpURLConnection) new URL(urlAddress).openConnection();
connection.setRequestMethod("HEAD");
return (connection.getResponseCode() == HttpURLConnection.HTTP_OK);
}
catch (Exception e) {return false; }
}
public static void main(String[] args)
{
JavaURLHighlighter hg = new JavaURLHighlighter();
hg.setUrlString("This is some text, visit www.mysite.com. Thanks & bye.");
hg.getConvertedMatches();
hg.setUrlString("This is some text, visit www.nonexistingmysite.com. Thanks & bye.");
hg.getConvertedMatches();
}
}
It's not actual solution to your problem and I wrote it quicky, so it might not be completly correct, but it should guide you a bit. Here I just print the matches. Have a look here Java equivalent to PHP's preg_replace_callback for regexp replacing function with which you could embrace all modified matches with a hrefs. With provided information you should be able to write what you want - but possibly with not 100% reliable detection.