Automatically Hyperlink URLs and E-Mail Addresses in ASP.NET Pages with C#

After I designed a web site, I found that many users post URL and e-mail information, but these URLs and e-mails just display as text. If you want to visit these URLs or e-mail addressess, you have to copy them and then paste them to your browser or e-mail client. I searched articles about automatically hyperlinking URLs, but only found VB source code. So, I decided do it myself.Using the Code

The first step is how to detect URLs and e-mail addresses. Of course, the best way is with regular expressions. By using regular expressions, you can automatically detect hyperlink-sensitive text and automatically wrap it into elements pointing to the same URL. Here’s how to proceed. The idea is to preprocess any displayable text using ad hoc regular expressions to identify portions of text that are e-mail addresses or Web site URLs. You create a RegEx object and initialize it with the regular expression. Next, you call IsMatch on the input string to see whether at least one match is found. Next, you call the Replace method. Replace takes the input as its first argument.

First, when you use regular expressions, you must include System.Text.RegularExpressions:

using System.Text.RegularExpressions;
The second step is to detect a URL:

Regex urlregex = new Regex(@”(http:\/\/([\w.]+\/?)\S*)”,

Here is how to detect an e-mail address:

Regex emailregex = new Regex(@”([a-zA-Z_0-9.-]+\@[a-zA-Z_0-9.-]+\.\w+)”,

When you detect a URL or e-mail address, you need to replace it with <a href=…></a>. I put all these into a function.

private void Button1_Click(object sender, System.EventArgs e)
string strContent = InputTextBox.Text;
Regex urlregex = new Regex(@”(http:\/\/([\w.]+\/?)\S*)”,
RegexOptions.IgnoreCase| RegexOptions.Compiled);
strContent = urlregex.Replace(strContent,
“<a href=\”\” target=\”_blank\”></a>”);
Regex emailregex = new Regex(@”([a-zA-Z_0-9.-]+\@[a-zA-Z_0-9.-]+\.\w+)”,
RegexOptions.IgnoreCase| RegexOptions.Compiled);
strContent = emailregex.Replace(strContent, “<a href=mailto:></a>”);
lbContent.Text += “<br>”+strContent;

Here you go. Build this control library, add references to your project, and enjoy validating.

Author: Roland Luo
Roland Luo is a computer developer & designer working with computers and the Internet since 2001. Started out doing Classic ASP, and moved on from there. My skillset now encompasses ASP, ASP.NET / C#, C++, VC++, MFC, SQL Server.


  1. Actually, the code above is incorrect, because it doesn't grab the original URL and Email to include in the hyperlink (it just creates blank hyperlink). Below is the corrected version of this code: public string AutoParseURLAndEmail(string psText) { string lsStrContent = psText; Regex loURLRegex = new Regex(@"(http:\/\/([\w.]+\/?)\S*)", RegexOptions.IgnoreCase | RegexOptions.Compiled); lsStrContent = loURLRegex.Replace(lsStrContent, "http://$2"); Regex loEmailRegex = new Regex(@"([a-zA-Z_0-9.-]+\@[a-zA-Z_0-9.-]+\.\w+)", RegexOptions.IgnoreCase | RegexOptions.Compiled); lsStrContent = loEmailRegex.Replace(lsStrContent, ""); return lsStrContent; }

  2. Looks like the blog system filtered out the HTML tags from the code. The correct version is identical to the original except there is a "$1" where the URL is suppose to go in the Replace function. Pete .-= Pete Soheil´s last undefined ..If you register your site for free at =-.