
This is the Title of the Book, eMatter Edition
Copyright © 2007 O’Reilly & Associates, Inc. All rights reserved.
588
|
Chapter 10: Regular Expressions
• Verify if the input password string matches some specific rules for entering a
password (i.e., the password is between 6 and 25 characters in length and con-
tains alphanumeric characters):
^(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{6,25}$
• Determine if any malicious characters were input by the user. Note that this reg-
ular expression will not prevent all malicious input, and it also prevents some
valid input, such as last names that contain a single quote.
^([^\)\(\<\>\"\'\%\&\+\;][(-{2})])*$
Discussion
Regular expressions are effective at finding specific information, and they have a
wide range of uses. Many applications use them to locate specific information within
a larger range of text, as well as to filter out bad input. The filtering action is very
useful in tightening the security of an application and preventing an attacker from
attempting to use carefully formed input to gain access to a machine on the Internet
or a local network. By using a regular expression to allow only good input to be
passed to the application, you can reduce the likelihood of many types of attacks,
such as SQL injection or cross-site-scripting.
The regular expressions presented in this recipe provide only a minute cross-section
of what can be accomplished with them. By