31 Aug 2010
I've recently done a proof of concept using RabbitMQ, Spring AMQP and Spring Integration. This...
21 Jul 2010
In over ten years of experience building software, there are patterns and nuances that you...
13 Jul 2010
I set out to find an elegant solution to providing growl notifications for a JRuby...
05 May 2010
I've started playing around with Heroku lately and ran into an issue when I created...
30 Apr 2010
I've been meaning to investigate something that came to mind a while back and just...

Convert Plain Text Links to HREF Using Regular Expressions

Published: 16 Jul 2009

For a project I was working on recently I had snippets of plain text with links embedded but without the actual HTML code. Here’s some code to get the job done:

1 public String getDescriptionForWeb() {
2   if (this.description == null) return null;
3   String r = "(?<!href=)http(s)?://([\\w+?\\.\\w+])+([a-zA-Z0-9\\~\\!\\@\\#\\$\\%\\^\\&amp;\\*\\(\\)_\\-\\=\\+\\\\\\/\\?\\.\\:\\;\\'\\,]*)?";
4   Pattern pattern = Pattern.compile(r, Pattern.DOTALL | Pattern.UNIX_LINES | Pattern.CASE_INSENSITIVE);
5   Matcher matcher = pattern.matcher(this.description);
6   return matcher.replaceAll("<a href=\"$0\">$0</a>");
7 }

blog comments powered by Disqus