advertisement
Login | Register   
  Include Code  Search Tips
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   TIP BANK
Browse DevX
Partners & Affiliates
advertisement
advertisement
Tip of the Day
Tip formerly from VB2TheMax
Expertise: Intermediate
Language: VB7
October 19, 2002
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.

It's quick, easy and you get access to all the articles on DevX.
This registration/login is to allow you to read articles on devx.com.
Already a member?





Dino Esposito
If you have a hot tip and we publish it, we'll pay you. However, due to accounting overhead we no longer pay $10 for a single tip submission. You must accumulate 10 acceptable tips to receive payment. Be sure to include a clear explanation of what the technique does and why it's useful. If it includes code, limit it to 20 lines if possible. Submit your tip here.
advertisement
advertisement