12

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!

Community
  • 1
  • 1
CatDadCode
  • 58,507
  • 61
  • 212
  • 318
  • 3
    Possible duplicate? [C# Code to Linkify URLs in a String](http://stackoverflow.com/questions/758135/c-code-to-linkify-urls-in-a-string) – eldarerathis Jan 20 '11 at 16:51

2 Answers2

49

I am using this right now:

text = Regex.Replace(text,
                @"((http|ftp|https):\/\/[\w\-_]+(\.[\w\-_]+)+([\w\-\.,@?^=%&:/~\+#]*[\w\-\@?^=%&/~\+#])?)",
                "<a target='_blank' href='$1'>$1</a>");
CatDadCode
  • 58,507
  • 61
  • 212
  • 318
  • Did you use this code exactly? Visual Studio thinks that all of those backslashes are "Unrecognized escape sequence"s. – brainmurphy1 May 23 '13 at 13:10
  • 2
    @brainmurphy1 Did you include the `@` in front of the opening quote for the string? That tells the parser/compiler that it is a string literal and to ignore escape sequences. – CatDadCode May 28 '13 at 21:32
  • 1
    Yes, thank you; I figured that out. It works as it should. – brainmurphy1 May 29 '13 at 13:36
  • be wary of tabnabbing - best to also add "rel noopener noreferrer" to any links with a target of '_blank' - see https://www.jitbit.com/alexblog/256-targetblank---the-most-underestimated-vulnerability-ever/ – Conan Oct 25 '19 at 08:50
11

Use this code

protected string MakeLink(string txt)
{
     Regex regx = new Regex("http://([\\w+?\\.\\w+])+([a-zA-Z0-9\\~\\!\\@\\#\\$\\%\\^\\&amp;\\*\\(\\)_\\-\\=\\+\\\\\\/\\?\\.\\:\\;\\'\\,]*)?", RegexOptions.IgnoreCase);        
     MatchCollection mactches = regx.Matches(txt);        
     foreach (Match match in mactches)
     {
         txt = txt.Replace(match.Value, "<a href='" + match.Value + "'>" + match.Value + "</a>");
     }    
     return txt;
}
abatishchev
  • 98,240
  • 88
  • 296
  • 433
Sunil Agarwal
  • 4,097
  • 5
  • 44
  • 80
  • 1
    Use the `{}` button, not the `"` button. :) – Richard Marskell - Drackir Jan 20 '11 at 16:57
  • Use `RegexOptions.Compiled` and `String.Format` – abatishchev Jan 20 '11 at 16:57
  • 2
    Also, have a look at [@ quoted string expressions](http://msdn.microsoft.com/en-us/library/362314fe(v=VS.100).aspx#CodeSnippetContainerCode6) It makes your regex a lot more readable (not to mention being able to copy/paste and whatnot). ie. `"@http://([\w+?\.\w+])+([a-zA-Z0-9\~\!\@\#\$\%\^\&\*\(\)_\-\=\+\\\/\?\.\:\;\'\,]*)?"` – Richard Marskell - Drackir Jan 20 '11 at 17:03
  • 1
    Slight tweak to great suggestion from @RichardMarskell-Drackir. The @ sign is prepended ahead of the opening double quote for the string. So it's `@"http://([\......."` rather than `"@http://([\......."` – Ian Yates Jul 05 '17 at 06:04
  • 2
    This will fail for HTTPS sites. – DavidG Mar 27 '18 at 16:14