devxlogo

Automatically hyperlink URLs in ASP.NET Pages

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.-]+@[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

devxblackblue

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist