Automatically add hyperlinks to URLs in asp.net

Source: Internet
Author: User
Tags expression include regular expression
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 I finished making the Software Business Network http://www.bizsofts.com Forum, I found that people always like to add a variety of useful URL links or email address in the post. I didn't 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 and not as hyperlinks, 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.

After discovering the problem, I proceeded 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. Then I thought, I simply write one myself with asp.net.

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. Let me say a few words on how to use ASP.net (C #) Step-by-Step to achieve our goal:

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

Using System.Text.RegularExpressions;

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

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

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

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 the <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. Welcome to download the source code for this example, and to the Http://www.bizsofts.com forum to see the actual effect.

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.