ASP. NET code to automatically hyperlink URL

  • 2020-12-26 05:38:25
  • OfStack

As a programmer, after the completion of the design, according to the situation of the program and the user's response to constantly improve the program, so as to constantly improve their own work. After I created the forum of the software commerce website, I found that people always like to add various useful URL links or Email addresses to their posts. While the author was designed without considering the 1 point, makes these URL links or Email address in the form of words and not only in the form of a hyperlink, according to other people also have to take these URL browse posts links to copy into a browser or copy the Email address into Outlook can go to the corresponding links or send E-mail to the corresponding Email address.

After discovering this problem, the author sets out to solve it immediately. The first is to look up the current code on this from the Internet. Unfortunately, repeated searches on the search engines have not found any articles on this subject. Later, I decided to use ES11en. NET to write one.

The key to automatically displaying hyperlinks is to correctly identify hyperlinks, and the most efficient way to do this is, without a doubt, to use regular expressions. Regular expressions are from ordinary characters (such as the character to the a z), and special characters (known as the yuan character) text mode, describes a kind of string matching model, which can be used to check whether a string contains a seed series, matching substring for replacement or from out if it meets the requirements of a substring in a string, etc. The NET base class library contains a namespace and a series of classes that take advantage of the power of regular expressions to automatically detect URL links or Email addresses in text. Here is how to realize our goal with ES20en. NET(C#)1 step by step:

First, to use regular expressions in ASP.NET (C#), you must include the namespace System.Text.RegularExpressions

using System.Text.RegularExpressions;

Step 2 is to identify URL hyperlinks using regular expressions:


Regex urlregex = new Regex(@"(http:\/\/([\w.]+\/?)\S*)",
RegexOptions.IgnoreCase|RegexOptions.Compiled);

The code here is to identify the Email address using a regular expression:


Regex emailregex = new Regex(@"([a-zA-Z_0-9.-]+\@[a-zA-Z_0-9.-]+\.\w+)",
RegexOptions.IgnoreCase|RegexOptions.Compiled);

Step 3. When the program has identified the URL hyperlink or Email address, it must use < a href=... > hyperlinks < /a > replaces these hyperlinks so that the text appears as links. I include them all here in the 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,
" The < 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, " The < a href=mailto: > < /a > "); 
lbContent.Text += " The < br > "+strContent; 
}

With these steps, you can automatically display hyperlinks and the Email address on your web page

Comments from others:


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=" rel="external nofollow" _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;}

Related articles: