Alex Rivera | Logout

Regular expression to find URLs within a string

Asked 2011-01-20T16:49:17.130
13

Possible Duplicate:
C# code to linkify urls in a string

I'm sure this is a stupid question but I can't find a decent answer anywhere. I need a good URL regular expression for C#. It needs to find all URLs in a string so that I can wrap each one in html to make it clickable.

  1. What is the best expression to use for this?

  2. Once I have the expression, what is the best way to replace these URLs with their properly formatted counterparts?

Thanks in advance!

Edit
Report

1 Answer

51

I am using this right now:

text = Regex.Replace(text,
                @"((http|ftp|https):\/\/[\w\-_]+(\.[\w\-_]+)+([\w\-\.,@?^=%&:/~\+#]*[\w\-\@?^=%&/~\+#])?)",
                "<a target='_blank' href='$1'>$1</a>");
answered 2011-01-20T17:30:37.283

Your Answer