
This is the Title of the Book, eMatter Edition
Copyright © 2007 O’Reilly & Associates, Inc. All rights reserved.
586
|
Chapter 10: Regular Expressions
See Also
See the “.NET Framework Regular Expressions” and “ArrayList Class” topics in the
MSDN documentation.
10.12 Using Common Patterns
Problem
You need a quick list from which to choose regular expression patterns that match
standard items. These standard items could be a social security number, a zip code, a
word containing only characters, an alphanumeric word, an email address, a URL,
dates, or one of many other possible items used throughout business applications.
These patterns can be useful in making sure that a user has input the correct data
and that it is well formed. These patterns can also be used as an extra security mea-
sure to keep hackers from attempting to break your code by entering strange or mal-
formed input (e.g., SQL injection or cross-site-scripting attacks). Note that these
regular expressions are not a silver bullet that will stop all attacks on your system;
rather, they are an added layer of defense.
Solution
• Match only alphanumeric characters along with the characters -, +, ., and any
whitespace:
^([\w\.+-]|\s)*$
Be careful using the - character within a character class—a regular
expression enclosed within
[ and ]. That character is also used to
specify a range of characters, as in
a-z for a through z inclusive. If you
want ...