Archive for January, 2010
An elegant regular expression for finding URLs
so you can turn them into hyperlinks automatically…
A while ago I needed to write some code which would automatically recognise a URL in plain text, and turn it into a hyperlink. Being a lazy sort, I turned to Google, and found this article on DevX. The regular expression it gave there was not perfect, but worked reasonably well:
\w*[\://]*\w+\.\w+\.\w+[/\w+]*[.\w+]*
At the time, I was sufficiently rushed off my feet that I forgave its flaws and implemented it. Over time, however, it’s been bugging me, and as the service it’s implemented on gets more traffic, so the need to improve it has become greater. And so it came to pass that this evening I bit the bullet and tried to write a better one. After a couple of hours of testing various permutations, here it is (after the jump):