A regular expression (or RE) specifies a set of strings that matches it. Thus, a regular expression can be used to check whether an input string is matched by that expression.
Regular expressions can be concatenated to form new regular expressions; if A and B are both regular expressions, then AB is also a 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 primitive expressions like the ones described here.
A brief explanation of the format of regular expressions borrowed from the Python Library Reference follows.
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'. (In the rest of this section, we will write RE's in this special style, usually without quotes, and strings to be matched 'in single quotes'.
Some characters, like "|" or "(", are special. Special characters either stand for classes of ordinary characters, or affect how the regular expressions around them are interpreted.
The special characters are shown by Table 7 on page 56:.
|
Expression |
Description |
|
. |
(Dot.) In the default mode, this matches any character except a newline. If the DOTALL flag has been specified, this matches any character including a newline. |
|
^ |
(Caret.) Matches the start of the string, and in MULTILINE mode also matches immediately after each newline. |
|
$ |
Matches the end of the string and in MULTILINE mode also matches before a newline. 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, as many repetitions as are possible. 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'. |
|
*?,+?,?? |
The *, +, and ? qualifiers are all greedy; they match as much text as possible. Sometimes this behavior is not desired; if the RE <.*> is matched against '<H1>title</H1>', it will match the entire string, and not just '<H1>'. Adding ? after the qualifier makes it perform the match in non-greedy or minimal fashion; as few characters as possible will be matched. Using .*? in the previous expression will match only '<H1>'. |
|
{m,n} |
Causes the resulting RE to match from m to n repetitions of the preceding RE, attempting to match as many repetitions as possible. For example, a{3,5} will match from 3 to 5 a characters. Omitting n specifies an infinite upper bound; you can't omit m. |
|
{m,n}? |
Causes the resulting RE to match from m to n repetitions of the preceding RE, attempting to match as few repetitions as possible. This is the non-greedy version of the previous qualifier. For example, on the 6-character string 'aaaaaa', a{3,5} will match 5 a characters, while a{3,5}? will only match 3 characters. |
|
\ |
Either escapes special characters (permitting you to match characters like *, ?, and so forth), or signals a special sequence; special sequences are discussed below. |
|
[] |
Used to indicate a set of characters. Characters can be listed individually, or a range of characters can be 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, and [a-zA-Z0-9] matches any letter or digit. Character classes such as \w or \S (defined below) are also acceptable inside a range. If you want to include a "]" or a "-" inside a set, precede it with a backslash, or place it as the first character. The pattern []] will match ']', for example. You can match the characters not within a range by complementing the set. This is indicated by including a "^" as the first character of the set; "^" elsewhere will simply match the "^" character. For example, [^5] will match any character except "5". |
|
| |
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. To match a literal "|", use \|, or enclose it inside a character class, as in [|]. |
|
(...) |
Matches whatever regular expression is inside the parentheses, and indicates the start and end of a group; the contents of a group can be retrieved after a match has been performed (for example in a substitution expression), and can be matched later in the string with the \number special sequence, described below. To match the literals "(" or "')", use \( or \), or enclose them inside a character class: [(] [)]. |
|
(?...) |
This is an extension notation (a "?" following a "(" is not meaningful otherwise). The first character after the "?" determines what the meaning and further syntax of the construct is. Extensions usually do not create a new group; (?P<name>>...) is the only exception to this rule. Following are the currently supported extensions. |
|
(?imsx) |
(One or more letters from the set "i", "L", "m", "s", "x".) The group matches the empty string; the letters set the corresponding flags for the entire regular expression: i - Do case-insensitive pattern matching. m - Treat string as multiple lines. That is, change "^" and "$" from matching the start or end of the string to matching the start or end of any line anywhere within the string. s - Treat string as single line. That is, change "." to match any character whatsoever, even a newline, which normally it would not match.
The /s and /m modifiers both override the $* setting. That is, no matter what $* contains, /s without /m will force "^" to match only at the beginning of the string and "$" to match only at the end (or just before a newline at the end) of the string. Together, as /ms, they let the "." match any character whatsoever, while yet allowing "^" and "$" to match, respectively, just after and just before newlines within the string. Extend your pattern's legibility by permitting whitespace and comments. |
|
(?:...) |
A non-grouping version of regular parentheses. Matches whatever regular expression is inside the parentheses, but the substring matched by the group cannot be retrieved after performing a match or referenced later in the pattern. |
|
(?#...) |
A comment; the contents of the parentheses are simply ignored. |
|
(?=...) |
Matches if ... matches next, but doesn't consume any of the string. This is called a look-ahead assertion. For example, Isaac(?=Asimov) will match 'Isaac' only if it's followed by 'Asimov'. |
|
(?!...) |
Matches if ... does not match next. This is a negative look-ahead assertion. For example, Isaac (?!Asimov) will match 'Isaac' only if it's not followed by 'Asimov'. |