tags:

views:

1004

answers:

2

This should be easy, but I'm not sure how to best go about it. I have a WinForms app that lets the user type in long descriptions. Occaisionally, they would type in URLs, and the RichTextBox would recognize them and make them clickable when displayed.

I'm moving the app to the web, and I'm not sure how to make those same URLs clickable. Is there some semi-automatic way to convert "http://www.google.com" or "www.google.com" to clickable links? Do I have to resort to RegEx matching?

+11  A: 

It's actually a very difficult problem. You can get close, but not perfect, with regular expressions. There's a very nice breakdown of potential regex patterns to consider here: http://www.regexguru.com/2008/11/detecting-urls-in-a-block-of-text/

The last one he lists seems like it's probably good enough for most purposes:

\b(?:(?:https?|ftp|file)://|www\.|ftp\.)
  (?:\([-A-Z0-9+&@#/%=~_|$?!:,.]*\)|[-A-Z0-9+&@#/%=~_|$?!:,.])*
  (?:\([-A-Z0-9+&@#/%=~_|$?!:,.]*\)|[A-Z0-9+&@#/%=~_|$])
MrKurt
+2  A: 

Beware. If the clickable URLs your users are generating are visible to other users, you'll be introducing new issues with cross-site scripting attacks and the like. Also, you may need to sanitize URLs. For example, there might be an ampersand in the URL but you'll need to escape it.

So, there's actually two steps. 1. Find the url. 2. Make the url clickable.

Step 2 is probably more difficult.

Also, be careful of things like parentheses and such. Some users happily use their URLs in a sentence which they then end in a period. E.g. I like http://www.pie.com.It is good. One of the best ways to solve this problem is just to have the URL generated as the user types. If something goes wrong, they'll be able to tell before submitting.

Brian
Fortunately this is just for in-house reference data, so attacks aren't much of an issue. The URLs are already in the text, so addressing them as they are entered isn't an option (although it would have been a good alternative).
gfrizzle