Thus, complex expressions can easily be constructed from simpler primitive expressions like the ones described here.
For details of the theory and implementation of regular expressions, consult the Friedl book referenced above, or almost any textbook about compiler construction.
Whitespace within the pattern is ignored, except when in a character class, or when preceded by an unescaped backslash, or within tokens like is scanned left-to-right, and matches are returned in the order found.
If one or more groups are present in the pattern, return a list of groups; this will be a list of tuples if the pattern has more than one group.
It should be used first in the expression string, or after one or more whitespace characters.
If there are non-whitespace characters before the flag, the results are undefined. Group names must be valid Python identifiers, and each group name must be defined only once within a regular expression. Group references are not supported even if they match strings of some fixed length.
If you’re not using a raw string to express the pattern, 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.
Without arguments, argument is zero, the corresponding return value is the entire matching string; if it is in the inclusive range [1..99], it is the string matching the corresponding parenthesized group.
Regular expressions use the backslash character () to indicate special forms or to allow special characters to be used without invoking their special meaning.
This collides with Python’s usage of the same character for the same purpose in string literals; for example, to match a literal backslash, one might have to write inside a regular Python string literal.
A symbolic group is also a numbered group, just as if the group were not named. Note that patterns which start with positive lookbehind assertions will not match at the beginning of the string being searched; you will most likely want to use the .
Similar to positive lookbehind assertions, the contained pattern must only match strings of some fixed length and shouldn’t contain group references.