When you think ASP, think...
Recent Articles
All Articles
ASP.NET Articles
ASPFAQs.com
Message Board
Related Web Technologies
User Tips!
Coding Tips

Sections:
Sample Chapters
Commonly Asked Message Board Questions
JavaScript Tutorials
MSDN Communities Hub
Official Docs
Security
Stump the SQL Guru!
XML Info
Information:
Feedback
Author an Article
ASP ASP.NET ASP FAQs Message Board Feedback

The 4 Guys Present: ASPFAQs.com

Jump to a FAQ
Enter FAQ #:
..or see our 10 Most Viewed FAQs.

4GuysFromRolla.com : ASP FAQS : Regular Expressions


Question:

How can I use regular expressions to dynamically place delimiters around certain strings?


[Print this FAQ]

Answer: Regular expressions can be used to replace chunks of text in a string; due to the powerful pattern matching in regular expressions, such search and replace functionality with regular expressions can be much more powerful than using VBScript's Replace function. (For information on Replace be sure to read the FAQ: "How can I replace each occurrence of a particular pattern in a string with another pattern?")

The following excerpt is taken from Richard Lowe's article Common Applications of Regular Expressions:

ASP often is called to dynamically apply formatting to the text it retrieves from the numerous data sources it has available to it. One handy feature of VBScript's regular expressions allow it to modify the complex patterns it matches. A prime example of this is the highlighting of certain words, such as keywords in a search by adding HTML tags.

To illustrate this, here is an example of highlighting all references to ".NET" in a string. This string could easily come from any source, such as a database or another web site.

<%
  Set regEx = New RegExp
  regEx.Global = true
  regEx.IgnoreCase = True

  ' Pattern finds any word (or URL) with
  ' .NET on the end of it
  regEx.Pattern = "(\b[a-zA-Z\._]+?\.NET\b)"

  ' Input a string to test our replace functionality:
  strText = ""
  strText = strText & "Microsoft has launched a new Web site," & _
        " www.ASP.NET. " & vbCrLf & "This Web site (as you " & _
        "can probably guess) contains information " & vbCrLf
  strText = strText & "on the next ""version"" of ASP: ASP.NET. The site " & _
        "contains links to " & vbCrLf & "download the latest " & _
        "ASP.NET bits, contains a FAQ section, links " & vbCrLf
  strText = strText & "to ASP community sites, and other great information. " & _
        "To learn more " & vbCrLf & "about ASP.NET, check out " & _
        "www.ASP.NET and the 4Guys ASP.NET " & vbCrLf
  strText = strText & "Article Index! " & vbCrLf

  ' Call Replace method of the regular expression:
  ' the $1 means place the matching text here
  Response.Write regEx.Replace(strText, _
        "<b style='color: #000099; font-size: 18pt'>$1")
%>

View the live demo!

In this example, there are several important things to note. The whole regular expression is enclosed in parenthesis - (). This tells it to capture any pattern it matches for use later. This captured match is then referenced in the replacement text by $1. Up to nine such captures can be done per match, referenced by $1 to $9. The Replace method of the regular expression object is different from the Replace global function in VBScript. It takes only two parameters, the string to search in and the replacement text.

Also in the example, a bold tag, with other style attributes was added to surround matching text for emphasis on the ".NET" matches. A search and replace like this could easily highlight search terms from a search of your site, or place automatic links to other pages on keywords that appear in any text.

-- Taken from Common Applications of Regular Expressions


FAQ posted by Scott Mitchell at 12/3/2000 8:44:26 PM to the Regular Expressions category. This FAQ has been viewed 51,999 times.

Do you have a FAQ you'd like to suggest? Suggestions? Comments? If so, send it in! Also, if you'd like to be a FAQ Admin (creating/editing FAQs), let me know! If you are looking for other FAQs, be sure to check out the 4Guys FAQ and Commonly Asked Messageboard Questions!

Most Viewed FAQs:

1.) How can I format numbers and date/times using ASP.NET? For example, I want to format a number as a currency. (761643 views)
2.) I am using Access and getting a 80004005 error (or a [Microsoft][ODBC Microsoft Access Driver] The Microsoft Jet database engine cannot open the file '(unknown)' error) when trying to open a connection! How can I fix this problem? (207777 views)
3.) How can I convert a Recordset into an array? Also, how can I convert an array into a Recordset? (202549 views)
4.) How can I quickly sort a VBScript array? (196039 views)
5.) How can I find out if a record already exists in a database? If it doesn't, I want to add it. (156019 views)
6.) How do I display data on a web page using arrays instead of Do...While...MoveNext...???... (152331 views)
7.) When I get a list of all files in a directory via the FileSystemObject, they aren't ordered in any reasonable way. How can I sort the files by name? Or by size? Or by date created? Or... (140381 views)
8.) For session variables to work, must the Web visitor have cookies enabled? (110162 views)
9.) Can I send emails without using CDONTS? (107083 views)
10.) How can I take the result of a SELECT...MULTIPLE or a group of same-named checkboxes and turn it into a query? That is, if the user selects 3 answers, how can I construct a query that looks for all 3? (106308 views)
Last computed at 9/17/2007 3:22:00 AM


ASP.NET [1.x] [2.0] | ASPMessageboard.com | ASPFAQs.com | Advertise | Feedback | Author an Article