Asp. NET automatic URL plus hyperlink code _ Practical Tips

Source: Internet
Author: User

As a programmer, after the completion of the design, according to the situation of the program and the user's reflection constantly improve the program, so as to continuously improve their work. After making the Software Business Network Forum, I found that people always like to add a variety of useful URL links or email addresses in the post. And the author did not take this into account in the design, so that these URL links or email addresses can only be in the form of text and not as a hyperlink to display, Other people browsing the posts must also copy the URL links to the browser or copy the email address to Outlook in order to go to the appropriate link address or send e-mail to the corresponding email address.

When this problem is discovered, the author begins to solve it. The first is from the Internet to find out about this aspect of the current code, unfortunately, search engines repeatedly find no articles in this area. Later, a thought, simply use asp.net to write one.

The key to automatically displaying hyperlinks is how to correctly identify hyperlinks, and the most effective way to do that is to use regular expressions. A regular expression is a literal pattern consisting of ordinary characters (such as characters A through Z) and special characters (called metacharacters). Describes a string matching pattern that can be used to check whether a string contains a seed string, replaces a matching substring, or extracts a substring from a string that matches a condition. NET base Class library contains a namespace and a series of classes that can give full play to the power of the rule expression, and it automatically detects the URL link or email address in the text. Here's how to use ASP.net (C #) Step-by-Step to achieve our goal:

First, if you want to use regular expressions in asp.net (C #), you must include System.Text.RegularExpressions in this namespace.

Using System.Text.RegularExpressions;

The second step is to identify the URL hyperlink with a regular expression:

Copy Code code as follows:

Regex Urlregex = new Regex (@) (http:\/\/([\w.] +\/?) \s*) ",
regexoptions.ignorecase|regexoptions.compiled);

The code here is to identify an email address with a regular expression:

Copy Code code as follows:

Regex emailregex = new Regex ([a-za-z_0-9.-]+\@[a-za-z_0-9.-]+\.\w+),
regexoptions.ignorecase|regexoptions.compiled);

The third step, when the program has identified the URL hyperlink or email address, you must use 〈a href=...〉 hyperlink 〈/a〉 to replace these hyperlinks, so that the text can be displayed as a link form. I'm here to include them all 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,
"〈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; 
}

With the above steps, you can automatically display hyperlinks and email addresses on your Web page.

Other Netizen's supplement:

 private void Button1_Click (object sender, System.EventArgs e) {string strcontent = I  
Nputtextbox.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 Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.