As a programmer, after completing the design, you must constantly improve the program according to the program's situation and user's feedback, so that you can continuously improve your works. After I finished creating the forum of the Software Business Network, I found that people always like to add various useful URL links or email addresses to their posts. The author did not consider this when designing it, so that these URL links or email addresses can only be displayed in text form rather than hyperlink form. Others who browse posts must copy these URL links to the browser or copy the email address to Outlook to transfer to the corresponding link address or send email to the corresponding email address.
After discovering this problem, the author immediately began to solve it. First of all, I searched the current code on this aspect from the Internet. Unfortunately, I found no articles in this aspect after searching on search engines. Later, when I thought about it, I simply wrote one by myself.
The key to automatically displaying hyperlinks is how to correctly identify hyperlinks. There is no doubt that the most effective way is to use regular expressions. Regular expressions are literal patterns composed of ordinary characters (such as characters a to z) and special characters (called metacharacters). They describe a pattern of string matching. They can be used to check whether a string contains a certain substring, replace the matching substring, or take out a substring that meets a certain condition from a certain string. The .NET basic class library contains a namespace and a series of classes that can fully utilize the power of regular expressions. It can automatically detect URL links or email addresses in text. Here is a detailed explanation of how to use (C#) to achieve our goal step by step:
First, to use regular expressions in (C#), you must include this namespace
using ;
The second step is to use regular expressions to identify the URL hyperlink:
Regex urlregex = new Regex(@"(http:\/\/([\w.]+\/?)\S*)",
|);
The code here uses regular expressions to identify the email address:
Regex emailregex = new Regex(@"([a-zA-Z_0-9.-]+\@[a-zA-Z_0-9.-]+\.\w+)",
|);
Step 3: When the program has recognized the URL hyperlink or email address, it must replace these hyperlinks with the <a href=...> hyperlink </a> so that these text can be displayed as the link form. I'll include them all in the function here:
private void Button1_Click(object sender, e) { string strContent = ; Regex urlregex = new Regex(@"(http:\/\/([\w.]+\/?)\S*)", | ); strContent = (strContent, "〈a href=\"\" target=\"_blank\"〉〈/a〉"); Regex emailregex = new Regex(@"([a-zA-Z_0-9.-]+\@[a-zA-Z_0-9.-]+\.\w+)", | ); strContent = (strContent, "〈a href=mailto:〉〈/a〉"); += "〈br〉"+strContent; }
Through the above steps, you can automatically display the hyperlink and email address on the web page
Additional to other netizens:
private void button1_click(object sender, e){ string strcontent = ; regex urlregex = new regex(@"(http://([w.]+/?)s*)",| ); strcontent = (strcontent,"<a href="" target=" rel="external nofollow" _blank"></a>"); regex emailregex = new regex(@"([a-za-z_0-9.-]+@[a-za-z_0-9.-]+.w+)",| ); strcontent = (strcontent, "<a href=mailto:></a>"); += "<br>"+strcontent;}