Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# code to linkify urls in a string

Does anyone have any good c# code (and regular expressions) that will parse a string and "linkify" any urls that may be in the string?

like image 896
Vance Smith Avatar asked Apr 16 '09 21:04

Vance Smith


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.

Is C language easy?

Compared to other languages—like Java, PHP, or C#—C is a relatively simple language to learn for anyone just starting to learn computer programming because of its limited number of keywords.

What is C language?

C is an imperative procedural language supporting structured programming, lexical variable scope, and recursion, with a static type system. It was designed to be compiled to provide low-level access to memory and language constructs that map efficiently to machine instructions, all with minimal runtime support.


2 Answers

It's a pretty simple task you can acheive it with Regex and a ready-to-go regular expression from:

  • http://regexlib.com/

Something like:

var html = Regex.Replace(html, @"^(http|https|ftp)\://[a-zA-Z0-9\-\.]+" +                          "\.[a-zA-Z]{2,3}(:[a-zA-Z0-9]*)?/?" +                          "([a-zA-Z0-9\-\._\?\,\'/\\\+&amp;%\$#\=~])*$",                          "<a href=\"$1\">$1</a>"); 

You may also be interested not only in creating links but in shortening URLs. Here is a good article on this subject:

  • Resolve and shorten URLs in C#

See also:

  • Regular Expression Workbench at MSDN
  • Converting a URL into a Link in C# Using Regular Expressions
  • Regex to find URL within text and make them as link
  • Regex.Replace Method at MSDN
  • The Problem With URLs by Jeff Atwood
  • Parsing URLs with Regular Expressions and the Regex Object
  • Format URLs in string to HTML Links in C#
  • Automatically hyperlink URL and Email in ASP.NET Pages with C#
like image 71
Konstantin Tarkus Avatar answered Sep 21 '22 08:09

Konstantin Tarkus


well, after a lot of research on this, and several attempts to fix times when

  1. people enter in http://www.sitename.com and www.sitename.com in the same post
  2. fixes to parenthisis like (http://www.sitename.com) and http://msdn.microsoft.com/en-us/library/aa752574(vs.85).aspx
  3. long urls like: http://www.amazon.com/gp/product/b000ads62g/ref=s9_simz_gw_s3_p74_t1?pf_rd_m=atvpdkikx0der&pf_rd_s=center-2&pf_rd_r=04eezfszazqzs8xfm9yd&pf_rd_t=101&pf_rd_p=470938631&pf_rd_i=507846

we are now using this HtmlHelper extension... thought I would share and get any comments:

    private static Regex regExHttpLinks = new Regex(@"(?<=\()\b(https?://|www\.)[-A-Za-z0-9+&@#/%?=~_()|!:,.;]*[-A-Za-z0-9+&@#/%=~_()|](?=\))|(?<=(?<wrap>[=~|_#]))\b(https?://|www\.)[-A-Za-z0-9+&@#/%?=~_()|!:,.;]*[-A-Za-z0-9+&@#/%=~_()|](?=\k<wrap>)|\b(https?://|www\.)[-A-Za-z0-9+&@#/%?=~_()|!:,.;]*[-A-Za-z0-9+&@#/%=~_()|]", RegexOptions.Compiled | RegexOptions.IgnoreCase);      public static string Format(this HtmlHelper htmlHelper, string html)     {         if (string.IsNullOrEmpty(html))         {             return html;         }          html = htmlHelper.Encode(html);         html = html.Replace(Environment.NewLine, "<br />");          // replace periods on numeric values that appear to be valid domain names         var periodReplacement = "[[[replace:period]]]";         html = Regex.Replace(html, @"(?<=\d)\.(?=\d)", periodReplacement);          // create links for matches         var linkMatches = regExHttpLinks.Matches(html);         for (int i = 0; i < linkMatches.Count; i++)         {             var temp = linkMatches[i].ToString();              if (!temp.Contains("://"))             {                 temp = "http://" + temp;             }              html = html.Replace(linkMatches[i].ToString(), String.Format("<a href=\"{0}\" title=\"{0}\">{1}</a>", temp.Replace(".", periodReplacement).ToLower(), linkMatches[i].ToString().Replace(".", periodReplacement)));         }          // Clear out period replacement         html = html.Replace(periodReplacement, ".");          return html;     } 
like image 35
josefresno Avatar answered Sep 19 '22 08:09

josefresno