When you think ASP, think...
Recent Articles xml
All Articles
ASP.NET Articles
Related Web Technologies
User Tips!
Coding Tips
spgif spgif

Sample Chapters
JavaScript Tutorials
MSDN Communities Hub
Official Docs
Stump the SQL Guru!
XML Info
Author an Article
spgif spgif
ASP ASP.NET ASP FAQs Feedback topnav-right
Print this page.
Published: Monday, December 04, 2000

Common Applications of Regular Expressions

By Richard Lowe

Regular Expressions (for those not yet acquainted with them) provide a way of matching patterns of strings that may be simple or extremely complicated and they do it in a very efficient manner. For an introduction to regular expressions in ASP be sure to read: An Introduction to Regular Expressions in VBScript. (There are also a number of other great beginner-level articles available at the 4Guys Regular Expression Article Index... be sure that you have a fairly good understanding of regular expressions before deciding to tackle this article.)

- continued -

Once you know the syntax of regular expressions and perhaps have even written one or two for practice, you may be inclined to ask "well great, but what are these things really good for in the real world?" This article presents a sample of real-world problems facing ASP programmers every day and some highly effective uses of Regular Expressions in solving those problems!

First, it should be noted that the regular expressions examples in this article were written under version 5.5 of the Microsoft's scripting engines. Regular Expression support has increased steadily in the last few versions of Microsoft's VBScript and JScript and therefore some scripts may not work in previous versions. To download the latest version of the scripting engines, visit http://msdn.microsoft.com/scripting/. To determine what version of the scripting engines you're currently using, be sure to read: Determining the Server-Side Scripting Language and Version!

Common Challenges
Here's a list of some of the common challenges that this article will look at and solve with regular expressions:

  • Validating email and/or password formats on the server side
  • Extracting specific sections from an HTML page (perhaps one retrieved with XMLHttp)
  • Parsing text data files into sections for import into a database
  • Replacing values in text to clean, reformat, or change content

Validating Password Formats
Our first task of validation demonstrates a primary function of regular expressions: abstracting strings of varying levels of complexity. What this means is that regular expressions give you, the programmer, a way to describe a string in general terms, terms which require minimum code, but handle every case your application will encounter. For example: A non-technical person might describe a password's minimum requirements to you like this: "The password's first character must be a letter, it must contain at least 4 characters and no more than 15 characters and no characters other than letters, numbers and the underscore may be used".

Now you, as the developer, have to translate that natural language expression into something the processing ASP page can understand and use to reject invalid password choices. The regular expression that matches the above requirement looks like this:


You could implement it in a reusable way in a function in your application like this:

Function ValidatePassword(strPassword)
  Dim re
  Set re = new RegExp

  re.IgnoreCase = false
  re.global = false
  re.Pattern = "^[a-zA-Z]\w{3,14}$"

  ValidatePassword = re.Test(strPassword)
End Function

Here is our original definition with the relevant parts of the regular expression shown alongside (in parenthesis): "The password's first character must be a letter (^[a-zA-Z] the carrot (^) means the beginning of the string and the square brackets tell the regexp to match one character from any of the group of characters inside), it must contain at least 4 characters and no more than 15 characters ({3,14}) and no characters other than letters, numbers and the underscore may be used (\w)"

Several notes: the {3,14} indicates that at least 3 but no more than 14 of the preceding pattern should be matched, (4 to 15 when you add the first character). It's a picky syntax inside the curly braces, not allowing you to place a space on either side of the comma. If you do, it will silently change the meaning of your regular expression and frustrate you when it doesn't work. Also, I didn't mentioned the $ character at the end of the regular expression - this character anchors our regular expression to the end of the string, and it is necessary to ensure no further characters are placed after a valid password entry. (For a more formal listing of special characters in regular expressions (such as the ^ and $, be sure to check out this article: Microsoft Beefs Up VBScript with Regular Expressions!)

Similar to validating password formats is the common problem of validating email addresses, for which there are already a wealth of documented methods of validating email addresses(including regular expression checks). For a plethora of techniques, be sure to check out the many articles at the Email Address Validation Article Index.

A RegExp to match email addresses might be implemented like this, displaying True or False for a valid email address:

  Dim re
  Set re = new RegExp

  re.pattern = "^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$"

  Response.Write re.Test("chadich@yahoo.com")

There are many regular expressions for validating all sorts of inputs. Check out RegExLib.com for a searchable database of common regular expression patterns!

In Part 2 we'll look at the next Regular Expression topic: extracting specific sections from an HTML page!

  • Read Part 2!

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