![]() |
|
|
Published: Monday, December 04, 2000 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.)
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
Validating Password Formats 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:
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 (
Several notes: the 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:
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!
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
![]() |
![]() |
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||