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; 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

Share the Post:
Heading photo, Metadata.

What is Metadata?

What is metadata? Well, It’s an odd concept to wrap your head around. Metadata is essentially the secondary layer of data that tracks details about the “regular” data. The regular

XDR solutions

The Benefits of Using XDR Solutions

Cybercriminals constantly adapt their strategies, developing newer, more powerful, and intelligent ways to attack your network. Since security professionals must innovate as well, more conventional endpoint detection solutions have evolved

AI is revolutionizing fraud detection

How AI is Revolutionizing Fraud Detection

Artificial intelligence – commonly known as AI – means a form of technology with multiple uses. As a result, it has become extremely valuable to a number of businesses across

AI innovation

Companies Leading AI Innovation in 2023

Artificial intelligence (AI) has been transforming industries and revolutionizing business operations. AI’s potential to enhance efficiency and productivity has become crucial to many businesses. As we move into 2023, several

data fivetran pricing

Fivetran Pricing Explained

One of the biggest trends of the 21st century is the massive surge in analytics. Analytics is the process of utilizing data to drive future decision-making. With so much of

kubernetes logging

Kubernetes Logging: What You Need to Know

Kubernetes from Google is one of the most popular open-source and free container management solutions made to make managing and deploying applications easier. It has a solid architecture that makes

ransomware cyber attack

Why Is Ransomware Such a Major Threat?

One of the most significant cyber threats faced by modern organizations is a ransomware attack. Ransomware attacks have grown in both sophistication and frequency over the past few years, forcing