A regular expression (or RE) specifies a set of strings that matches
it; the functions in this module let you check if a particular string
matches a given regular expression (or if a given regular expression
matches a particular string, which comes down to the same thing).
Regular expressions can be concatenated to form new regular
expressions; if A and B are both regular expressions,
then AB is also an regular expression. If a string p
matches A and another string q matches B, the string pq
will match AB. Thus, complex expressions can easily be constructed
from simpler ones like the primitives described here. For details of
the theory and implementation of regular expressions, consult almost
any textbook about compiler construction.
A brief explanation of the format of regular expressions follows.
Regular expressions can contain both special and ordinary characters.
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
characters 'last'. (In the rest of this section, we'll write RE's in
this special font, usually without quotes, and strings to be
matched 'in single quotes'.)
Special characters either stand for classes of ordinary characters, or
affect how the regular expressions around them are interpreted.
The special characters are:
- (Dot.) Matches any character except a newline.
- (Caret.) Matches the start of the string.
- Matches the end of the string.
foo matches both 'foo' and 'foobar', while the regular
expression 'foo$' matches only 'foo'.
- Causes the resulting RE to
match 0 or more repetitions of the preceding RE. ab* will
match 'a', 'ab', or 'a' followed by any number of 'b's.
- Causes the
resulting RE to match 1 or more repetitions of the preceding RE.
ab+ will match 'a' followed by any non-zero number of 'b's; it
will not match just 'a'.
- Causes the resulting RE to
match 0 or 1 repetitions of the preceding RE. ab? will
match either 'a' or 'ab'.
- Either escapes special characters (permitting you to match
characters like '*?+&$'), or signals a special sequence; special
sequences are discussed below. Remember that Python also uses the
backslash as an escape sequence in string literals; if the escape
sequence isn't recognized by Python's parser, the backslash and
subsequent character are included in the resulting string. However,
if Python would recognize the resulting sequence, the backslash should
be repeated twice.
- Used to indicate a set of characters. Characters can
be listed individually, or a range is indicated by giving two
characters and separating them by a '-'. Special characters are
not active inside sets. For example, [akm$]
will match any of the characters 'a', 'k', 'm', or '$'; [a-z] will
match any lowercase letter.
If you want to include a ] inside a
set, it must be the first character of the set; to include a -,
place it as the first or last character.
Characters not within a range can be matched by including a
^ as the first character of the set; ^ elsewhere will
simply match the '^' character.
The special sequences consist of '\' and a character
from the list below. If the ordinary character is not on the list,
then the resulting RE will match the second character. For example,
\$ matches the character '$'. Ones where the backslash
should be doubled in string literals are indicated.
- A\|B, where A and B can be arbitrary REs,
creates a regular expression that will match either A or B. This can
be used inside groups (see below) as well.
- \( \)
- Indicates the start and end of a group; the
contents of a group can be matched later in the string with the
\[1-9] special sequence, described next.
- \\1, ... \\7, \8, \9
Matches the contents of the group of the same
number. For example, \(.+\) \\1 matches 'the the' or
'55 55', but not 'the end' (note the space after the group). This
special sequence can only be used to match one of the first 9 groups;
groups with higher numbers can be matched using the \v
sequence. (\8 and \9 don't need a double backslash
because they are not octal digits.)
- Matches the empty string, but only at the
beginning or end of a word. A word is defined as a sequence of
alphanumeric characters, so the end of a word is indicated by
whitespace or a non-alphanumeric character.
- Matches the empty string, but when it is not at the
beginning or end of a word.
- Must be followed by a two digit decimal number, and
matches the contents of the group of the same number. The group
number must be between 1 and 99, inclusive.
- Matches any alphanumeric character; this is
equivalent to the set [a-zA-Z0-9].
- Matches any non-alphanumeric character; this is
equivalent to the set [â-zA-Z0-9].
- Matches the empty string, but only at the beginning of a
word. A word is defined as a sequence of alphanumeric characters, so
the end of a word is indicated by whitespace or a non-alphanumeric
- Matches the empty string, but only at the end of a
- Matches a literal backslash.
- Like ^, this only matches at the start of the
- Like $, this only matches at the end of