7.1

Table Of Contents
Introduction to Regular Expressions
Syntax 191
Character selection 191
Alternation 192
Grouping 192
Quantification 192
Examples 193
Only numbers 193
Dutch zip code 193
Canadian zip codes 193
This chapter is an introduction to regular expressions, explaining basic regular expression syntax. Regular expressions for
user input fields use the perl regular expression notation. Note that the user input regular expressions must match all of the
input.
Additional information can be found at the following web sites:
l http://regexlib.com
l http://www.regular-expressions.info
l http://en.wikipedia.org/wiki/Regular_expression
Syntax
The following sections describe the basic regular expression syntax.
Character selection
Regular expressions can contain both special and ordinary characters. Most ordinary characters, like "A", "a", or "0", are the
simplest regular expressions; they simply match themselves. You can concatenate ordinary characters, so last matches the
string 'last'.
l [] Matches a single character that is contained within the brackets. For example, [abc] matches "a", "b", or "c". [a-z]
matches any lowercase letter. These can be mixed: [abcq-z] matches a, b, c, q, r, s, t, u, v, w, x, y, z, and so does [a-
cq-z]. The '-' character should be literal only if it is the last or the first character within the brackets: [abc-] or [-abc]. To
match an '[' or ']' character, the easiest way is to make sure the closing bracket is first in the enclosing square brack-
ets: [][ab] matches ']', '[', 'a' or 'b'.
l [^ ] Matches a single character that is not contained within the brackets. For example, [^abc] matches any character
other than "a", "b", or "c". [^a-z] matches any single character that is not a lowercase letter. As above, these can be
mixed.
l ( ) Defines a "subexpression".
l . Matches any single character. Within [ ] this character has its normal (literal) meaning. For example, "a.cd" matches
"abcd", "a..d" matches "abcd" but [a.cd] matches "a" or "." or "c" or "d".
l \d Any digit 0-9
Introduction to Regular Expressions
©2010 Objectif Lune Inc - 191 -