Atlanta Custom Software Development 

   Search        Code/Page

User Login



Forgot the Password?
» Web Development
» Maintenance
» Data Integration/BI
» Information Management
» Regular Expr Tester
» Free Tools

Automatically hyperlink URLs in ASP.NET Pages

Total Hit ( 6307)

Rate this article:     Poor     Excellent 

 Submit Your Question/Comment about this article



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:

Click here to copy the following block
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.

Click here to copy the following block
Imports System
Imports System.Diagnostics
Imports System.Text
Imports System.Text.RegularExpressions
Imports Microsoft.VisualBasic

Public 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 " _
      & "Don't spam me."

    Console.WriteLine(vbCrLf & vbCrLf)

    ' Detecting Web sites. All occurrences will be processed.
    Dim siteString As String = "My Web site is; Visit us."
  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("<a href='mailto:")
    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("<a href='")
    Return sb.ToString()
  End Function
End Class

Submitted By : Nayan Patel  (Member Since : 5/26/2004 12:23:06 PM)

Job Description : He is the moderator of this site and currently working as an independent consultant. He works with, SQL Server and other MS technologies. He is, MCDBA and MCSE. In his free time he likes to watch funny movies and doing oil painting.
View all (893) submissions by this author  (Birth Date : 7/14/1981 )

Home   |  Comment   |  Contact Us   |  Privacy Policy   |  Terms & Conditions   |  BlogsZappySys

© 2008 BinaryWorld LLC. All rights reserved.