I have an input String say Please go to http://stackoverflow.com
. The url part of the String is detected and an anchor <a href=""></a>
is automatically added by many browser/IDE/applications. So it becomes Please go to <a href='http://stackoverflow.com'>http://stackoverflow.com</a>
.
I need to do the same using Java.
A good refinement to PhiLho's answer would be:
msg.replaceAll("(?:https?|ftps?)://[\w/%.-][/\??\w=?\w?/%.-]?[/\?&\w=?\w?/%.-]*", "$0");
The following code makes these modifications to the "Atwood Approach":
Notes:
This was written in response to our client's requirements and we feel it represents a reasonable compromise between the allowable characters from the RFC and common usage. It is offered here in the hopes that it will be useful to others.
Further expansion could be made which would allow for any Unicode characters to be entered (i.e. not escaped with %XX (two digit hex) and hyperlinked, but that would require accepting all Unicode letters plus limited punctuation and then splitting on the "acceptable" delimiters (eg. .,%,|,#, etc.), URL-encoding each part and then gluing back together. For example, http://en.wikipedia.org/wiki/Björn_Andrésen (which the Stack Overflow generator does not detect) would be "http://en.wikipedia.org/wiki/Bj%C3%B6rn_Andr%C3%A9sen" in the href, but would contain Björn_Andrésen in the linked text on the page.
You could also work with jSoup, see this (quite detailed) example:
http://jsoup.org/cookbook/extracting-data/example-list-links
Primitive:
This needs refinement, to match proper URLs, and particularly GET parameters (?foo=bar&x=25)
There is a very good javascript framework that renders the links directly in the browser: https://github.com/gregjacobs/Autolinker.js
It supports: html, email, (us only) phone number, twitter and hashtags.
It also renders links without: http://
You could do something like this (adjust the regex to suit your needs):