Automatically hyperlink URLs in ASP.NET Pages

Suppose that your ASP.NET pages display contents read from database fields. Suppose also that some of those fields may contain URLs or email addresses. A typical example is when you display user information such as the email address or the personal Web site. By using regular expressions, you can automatically detect hyperlink-sensitive text and automatically wrap them into element pointing to the same URL. Let’s see how to proceed. The idea is preprocessing any displayable text using ad hoc regular expressions to identify portions of text that are email addresses or Web site URLs.

You create a RegEx object and initialize it with the regular expression. Next, you call IsMatch on the input string to see whether at least one match is found. Next, you call the method Replace. Replace takes the input as its first argument. It also takes a second argument being a delegate function with the following prototype:

Function MatchEvaluator(ByVal m As Match) As String

When invoked, your callback function receives a Match object in which the Value property is set to a substring excerpted from the text. It goes without saying that substring is a portion of the original text that matches the pattern. The match evaluator creates a new string as appropriate and returns that to the caller. The Replace method ensured that the returned string replaces the match string for each occurrence.

The following code shows a console application that implements the tip. Notice that UriBuilder normalizes a Web site expression by adding protocol and port information. Finally, the regular expressions used here work in most cases; however, I can’t guarantee they work always. Forewarned, forearmed.

Imports SystemImports System.DiagnosticsImports System.TextImports System.Text.RegularExpressionsImports Microsoft.VisualBasicPublic Class RegExApp    Public Shared Sub Main()        Dim r As RegExApp = New RegExApp()    End Sub    Public Sub New()        ' Detecting EMAIL addresses. All occurrences will be processed.        Dim emailString As String = "My email address is [email protected] " _            & "Don't spam me."        Console.WriteLine(ActivateEmailAddress(emailString))        Console.WriteLine(vbCrLf & vbCrLf)        ' Detecting Web sites. All occurrences will be processed.        Dim siteString As String = "My Web site is www.vb2themax.com; Visit us."        Console.WriteLine(ActivateWebSiteUrl(siteString))    End Sub    Public Function ActivateEmailAddress(emailString As String) As String        Dim buf As String = emailString        Dim patternEmail As String = "[a-zA-Z_0-9.-][email protected][a-zA-Z_0-9.-]+.w+"        Dim re As RegEx = New Regex(patternEmail)        If re.IsMatch(buf) Then            buf = re.Replace(buf, AddressOf MailToMatchEvaluator)        End If        Return buf     End Function    Public Function ActivateWebSiteUrl(siteString As String) As String        Dim buf As String = siteString        Dim patternSite As String = "w*[://]*w+.w+.w+[/w+]*[.w+]*"        Dim re As RegEx = New Regex(patternSite)        If re.IsMatch(buf) Then            buf = re.Replace(buf, AddressOf WebSiteMatchEvaluator)        End If        Return buf    End Function    Private Function MailToMatchEvaluator(ByVal m As Match) As String        Dim sb As StringBuilder = New StringBuilder("")        sb.Append(m.Value)        sb.Append("")        Return sb.ToString()    End Function    Private Function WebSiteMatchEvaluator(ByVal m As Match) As String        Dim ub As UriBuilder = New UriBuilder(m.Value)        Dim sb As StringBuilder = New StringBuilder("")        sb.Append(m.Value)        sb.Append("")        Return sb.ToString()    End FunctionEnd Class


Source: Applied XML Programming for Microsoft .NET, Dino Esposito, Microsoft Press 2002

Share the Post:
Share on facebook
Share on twitter
Share on linkedin

Overview

The Latest

microsoft careers

Top Careers at Microsoft

Microsoft has gained its position as one of the top companies in the world, and Microsoft careers are flourishing. This multinational company is efficiently developing popular software and computers with other consumer electronics. It is a dream come true for so many people to acquire a high paid, high-prestige job

your company's audio

4 Areas of Your Company Where Your Audio Really Matters

Your company probably relies on audio more than you realize. Whether you’re creating a spoken text message to a colleague or giving a speech, you want your audio to shine. Otherwise, you could cause avoidable friction points and potentially hurt your brand reputation. For example, let’s say you create a

chrome os developer mode

How to Turn on Chrome OS Developer Mode

Google’s Chrome OS is a popular operating system that is widely used on Chromebooks and other devices. While it is designed to be simple and user-friendly, there are times when users may want to access additional features and functionality. One way to do this is by turning on Chrome OS