The module defines these functions, and an exception:
-1if the string does not match the pattern (this is different from a zero-length match!).
-1if no position in the string matches the pattern (this is different from a zero-length match anywhere!).
search()methods, described below. The optional argument translate, if present, must be a 256-character string indicating how characters (both of the pattern and of the strings to be matched) are translated before comparing them; the i-th element of the string gives the translation for the character with ASCII code i. This can be used to implement case-insensitive matching; see the
casefolddata item below.
prog = regex.compile(pat) result = prog.match(str)
result = regex.match(pat, str)
but the version using
compile() is more efficient when multiple
regular expressions are used concurrently in a single program. (The
compiled version of the last pattern passed to
regex.search() is cached, so programs that use only a single
regular expression at a time needn't worry about compiling regular
search(). (Already compiled expression objects are not affected.) The argument is an integer which is the OR of several flag bits. The return value is the previous value of the syntax flags. Names for the flags are defined in the standard module
regex_syntax; read the file regex_syntax.py for more information.
compile(), but supports symbolic group names: if a parenthesis-enclosed group begins with a group name in angular brackets, e.g.
'\(<id>[a-z][a-z0-9]*\)', the group can be referenced by its name in arguments to the
group()method of the resulting compiled regular expression object, like this:
p.group('id'). Group names may contain alphanumeric characters and
compile()to map all upper case characters to their lowercase equivalents.
Compiled regular expression objects support these methods:
-1if the string does not match the pattern (this is different from a zero-length match!). The optional second parameter, pos, gives an index in the string where the search is to start; it defaults to
0. This is not completely equivalent to slicing the string; the
''pattern character matches at the real beginning of the string and at positions just after a newline, not necessarily at the index where the search is to start.
-1if no position in the string matches the pattern (this is different from a zero-length match anywhere!). The optional second parameter has the same meaning as for the
search()method found a match. It returns one or more groups of the match. If there is a single index argument, the result is a single string; if there are multiple arguments, the result is a tuple with one item per argument. If the index 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 the corresponding parenthesized group (using the default syntax, groups are parenthesized using
\)). If no such group exists, the corresponding result is
If the regular expression was compiled by
symcomp() instead of
compile(), the index arguments may also be strings
identifying groups by their group name.
Compiled regular expressions support these data attributes:
search()method found a match, this is a tuple of pairs of indexes corresponding to the beginning and end of all parenthesized groups in the pattern. Indices are relative to the string argument passed to
search(). The 0-th tuple gives the beginning and end or the whole pattern. When the last match or search failed, this is
search()method found a match, this is the string argument passed to that method. When the last match or search failed, this is
regex.compile()that created this regular expression object. If the translate argument was omitted in the
regex.compile()call, this is
symcomp(). Same as