ASP. NET automatically adds hyperlinks to URLs

Source: Internet
Author: User
As Program After the design, you must constantly improve the program based on the Program situation and user feedback, so as to continuously improve your work. After making a forum for software-based business network, I found that people always like to add various useful URL links or email addresses to their posts. I did not take this into account when I designed it, so that these URL links or email addresses can only be displayed in the form of text rather than hyperlinks, other users who browse the post must copy these URL links to the browser or copy the email address to outlook to transfer the link address or send an email to the corresponding email address.

After finding this problem, I will start to solve it. First, search for the current situation on the InternetCodeUnfortunately, the search engine does not find this problem after repeated searches.Article. Later I thought about writing one by myself using ASP. NET.

The key to automatically display hyperlinks is how to correctly identify hyperlinks. Undoubtedly, the most effective method is to use regular expressions. A regular expression is a text pattern consisting of common characters (such as characters A to Z) and special characters (such as metacharacters). It describes a string matching pattern, it can be used to check whether a string contains a seed string, replace matched substrings, or retrieve substrings that meet certain conditions from a string .. Net base class library contains a namespace and a series of classes that can fully utilize the power of Rule expressions. With this class, you can automatically detect URL links or email addresses in text. The following describes how to use ASP. NET (C #) to achieve our goal step by step:

First, to use a regular expression in ASP. NET (C #), you must include the system. Text. regularexpressions namespace.

Using system. Text. regularexpressions;

Step 2: use regular expressions to identify URL hyperlinks:

RegEx urlregex = new RegEx (@ "(http: \/([\ W.] + \/?) \ S *)",
Regexoptions. ignorecase | regexoptions. Compiled );

The code here is to use a regular expression to identify the email address:

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 recognized the URL hyperlink or email address, you must use <a href =...> hyperlinks </a> are replaced to display the text as links. Here I will include all of them in the function:

Private void button#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;
}

After completing the preceding steps, you can automatically display hyperlinks and email addresses on the webpage.

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.