\section{Built-in Module \sectcode{re}} \label{module-re} \bimodindex{re} This module provides regular expression matching operations similar to those found in Perl. It's 8-bit clean: both patterns and strings may contain null bytes and characters whose high bit is set. It is always available. Regular expressions use the backslash character (\samp{\e}) 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 \samp{\e\e\e\e} as the pattern string, because the regular expression must be \samp{\e\e}, and each backslash must be expressed as \samp{\e\e} inside a regular Python string literal. The solution is to use Python's raw string notation for regular expression patterns; backslashes are not handled in any special way in a string literal prefixed with 'r'. So \code{r"\e n"} is a two character string containing a backslash and the letter 'n', while \code{"\e n"} is a one-character string containing a newline. Usually patterns will be expressed in Python code using this raw string notation. \subsection{Regular Expression Syntax} 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 \emph{A} and \emph{B} are both regular expressions, then \emph{AB} is also an regular expression. If a string \emph{p} matches A and another string \emph{q} matches B, the string \emph{pq} will match AB. 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 below, or almost any textbook about compiler construction. A brief explanation of the format of regular expressions follows. %For further information and a gentler presentation, consult XXX somewhere. Regular expressions can contain both special and ordinary characters. Most ordinary characters, like \samp{A}, \samp{a}, or \samp{0}, are the simplest regular expressions; they simply match themselves. You can concatenate ordinary characters, so \samp{last} matches the characters 'last'. (In the rest of this section, we'll write RE's in \code{this special font}, usually without quotes, and strings to be matched 'in single quotes'.) Some characters, like \samp{|} or \samp{(}, 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: % define these since they're used twice: \newcommand{\MyLeftMargin}{0.7in} \newcommand{\MyLabelWidth}{0.65in} \begin{list}{}{\leftmargin \MyLeftMargin \labelwidth \MyLabelWidth} \item[\code{.}] (Dot.) In the default mode, this matches any character except a newline. If the \constant{DOTALL} flag has been specified, this matches any character including a newline. % \item[\code{\^}] (Caret.) Matches the start of the string, and in \constant{MULTILINE} mode also immediately after each newline. % \item[\code{\$}] Matches the end of the string, and in \constant{MULTILINE} mode also matches before a newline. \code{foo} matches both 'foo' and 'foobar', while the regular expression \code{foo\$} matches only 'foo'. % \item[\code{*}] Causes the resulting RE to match 0 or more repetitions of the preceding RE, as many repetitions as are possible. \code{ab*} will match 'a', 'ab', or 'a' followed by any number of 'b's. % \item[\code{+}] Causes the resulting RE to match 1 or more repetitions of the preceding RE. \code{ab+} will match 'a' followed by any non-zero number of 'b's; it will not match just 'a'. % \item[\code{?}] Causes the resulting RE to match 0 or 1 repetitions of the preceding RE. \code{ab?} will match either 'a' or 'ab'. \item[\code{*?}, \code{+?}, \code{??}] The \code{*}, \code{+}, and \code{?} qualifiers are all \dfn{greedy}; they match as much text as possible. Sometimes this behaviour isn't desired; if the RE \code{<.*>} is matched against \code{

title

}, it will match the entire string, and not just \code{

}. Adding \code{?} after the qualifier makes it perform the match in \dfn{non-greedy} or \dfn{minimal} fashion; as few characters as possible will be matched. Using \code{.*?} in the previous expression will match only \code{

}. % \item[\code{\{\var{m},\var{n}\}}] Causes the resulting RE to match from \var{m} to \var{n} repetitions of the preceding RE, attempting to match as many repetitions as possible. For example, \code{a\{3,5\}} will match from 3 to 5 'a' characters. % \item[\code{\{\var{m},\var{n}\}?}] Causes the resulting RE to match from \var{m} to \var{n} repetitions of the preceding RE, attempting to match as \emph{few} repetitions as possible. This is the non-greedy version of the previous qualifier. For example, on the 6-character string 'aaaaaa', \code{a\{3,5\}} will match 5 'a' characters, while \code{a\{3,5\}?} will only match 3 characters. % \item[\code{\e}] Either escapes special characters (permitting you to match characters like '*?+\&\$'), or signals a special sequence; special sequences are discussed below. 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. However, if Python would recognize the resulting sequence, the backslash should be repeated twice. This is complicated and hard to understand, so it's highly recommended that you use raw strings for all but the simplest expressions. % \item[\code{[]}] 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, \code{[akm\$]} will match any of the characters \character{a}, \character{k}, \character{m}, or \character{\$}; \code{[a-z]} will match any lowercase letter and \code{[a-zA-Z0-9]} matches any letter or digit. Character classes such as \code{\e w} or \code {\e S} (defined below) are also acceptable inside a range. If you want to include a \samp{]} or a \samp{-} inside a set, precede it with a backslash. Characters \emph{not} within a range can be matched by including a \code{\^} as the first character of the set; \code{\^} elsewhere will simply match the \samp{\^} character. % \item[\code{|}]\code{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 \samp{|}, use \code{\e|}, or enclose it inside a character class, like \code{[|]}. % \item[\code{(...)}] 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, and can be matched later in the string with the \code{\e \var{number}} special sequence, described below. To match the literals '(' or ')', use \code{\e(} or \code{\e)}, or enclose them inside a character class: \code{[(] [)]}. % \item[\code{(?...)}] 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; \code{(?P<\var{name}>...)} is the only exception to this rule. Following are the currently supported extensions. % \item[\code{(?iLmsx)}] (One or more letters from the set \samp{i}, \samp{L}, \samp{m}, \samp{s}, \samp{x}.) The group matches the empty string; the letters set the corresponding flags (\constant{re.I}, \constant{re.L}, \constant{re.M}, \constant{re.S}, \constant{re.X}) for the entire regular expression. This is useful if you wish to include the flags as part of the regular expression, instead of passing a \var{flag} argument to the \function{compile()} function. % \item[\code{(?:...)}] A non-grouping version of regular parentheses. Matches whatever's inside the parentheses, but the substring matched by the group \emph{cannot} be retrieved after performing a match or referenced later in the pattern. % \item[\code{(?P<\var{name}>...)}] Similar to regular parentheses, but the substring matched by the group is accessible via the symbolic group name \var{name}. Group names must be valid Python identifiers. A symbolic group is also a numbered group, just as if the group were not named. So the group named 'id' in the example above can also be referenced as the numbered group 1. For example, if the pattern is \code{(?P[a-zA-Z_]\e w*)}, the group can be referenced by its name in arguments to methods of match objects, such as \code{m.group('id')} or \code{m.end('id')}, and also by name in pattern text (e.g. \code{(?P=id)}) and replacement text (e.g. \code{\e g}). % \item[\code{(?P=\var{name})}] Matches whatever text was matched by the earlier group named \var{name}. % \item[\code{(?\#...)}] A comment; the contents of the parentheses are simply ignored. % \item[\code{(?=...)}] Matches if \code{...} matches next, but doesn't consume any of the string. This is called a lookahead assertion. For example, \code{Isaac (?=Asimov)} will match 'Isaac~' only if it's followed by 'Asimov'. % \item[\code{(?!...)}] Matches if \code{...} doesn't match next. This is a negative lookahead assertion. For example, \code{Isaac (?!Asimov)} will match 'Isaac~' only if it's \emph{not} followed by 'Asimov'. \end{list} The special sequences consist of \samp{\e} 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, \code{\e\$} matches the character \samp{\$}. \begin{list}{}{\leftmargin \MyLeftMargin \labelwidth \MyLabelWidth} % \item[\code{\e \var{number}}] Matches the contents of the group of the same number. Groups are numbered starting from 1. For example, \code{(.+) \e 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 99 groups. If the first digit of \var{number} is 0, or \var{number} is 3 octal digits long, it will not be interpreted as a group match, but as the character with octal value \var{number}. Inside the \code{[} and \code{]} of a character class, all numeric escapes are treated as characters. % \item[\code{\e A}] Matches only at the start of the string. % \item[\code{\e b}] 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. Inside a character range, \code{\e b} represents the backspace character, for compatibility with Python's string literals. % \item[\code{\e B}] Matches the empty string, but only when it is \emph{not} at the beginning or end of a word. % \item[\code{\e d}]Matches any decimal digit; this is equivalent to the set \code{[0-9]}. % \item[\code{\e D}]Matches any non-digit character; this is equivalent to the set \code{[\^0-9]}. % \item[\code{\e s}]Matches any whitespace character; this is equivalent to the set \code{[ \e t\e n\e r\e f\e v]}. % \item[\code{\e S}]Matches any non-whitespace character; this is equivalent to the set \code{[\^\ \e t\e n\e r\e f\e v]}. % \item[\code{\e w}]When the \constant{LOCALE} flag is not specified, matches any alphanumeric character; this is equivalent to the set \code{[a-zA-Z0-9_]}. With \constant{LOCALE}, it will match the set \code{[0-9_]} plus whatever characters are defined as letters for the current locale. % \item[\code{\e W}]When the \constant{LOCALE} flag is not specified, matches any non-alphanumeric character; this is equivalent to the set \code{[\^a-zA-Z0-9_]}. With \constant{LOCALE}, it will match any character not in the set \code{[0-9_]}, and not defined as a letter for the current locale. \item[\code{\e Z}]Matches only at the end of the string. % \item[\code{\e \e}] Matches a literal backslash. \end{list} \subsection{Module Contents} \nodename{Contents of Module re} The module defines the following functions and constants, and an exception: \setindexsubitem{(in module re)} \begin{funcdesc}{compile}{pattern\optional{, flags}} Compile a regular expression pattern into a regular expression object, which can be used for matching using its \function{match()} and \function{search()} methods, described below. The expression's behaviour can be modified by specifying a \var{flags} value. Values can be any of the following variables, combined using bitwise OR (the \code{|} operator). The sequence \begin{verbatim} prog = re.compile(pat) result = prog.match(str) \end{verbatim} is equivalent to \begin{verbatim} result = re.match(pat, str) \end{verbatim} but the version using \function{compile()} is more efficient when the expression will be used several times in a single program. %(The compiled version of the last pattern passed to %\function{regex.match()} or \function{regex.search()} is cached, so %programs that use only a single regular expression at a time needn't %worry about compiling regular expressions.) \end{funcdesc} \begin{datadesc}{I} \dataline{IGNORECASE} Perform case-insensitive matching; expressions like \code{[A-Z]} will match lowercase letters, too. This is not affected by the current locale. \end{datadesc} \begin{datadesc}{L} \dataline{LOCALE} Make \code{\e w}, \code{\e W}, \code{\e b}, \code{\e B}, dependent on the current locale. \end{datadesc} \begin{datadesc}{M} \dataline{MULTILINE} When specified, the pattern character \code{\^} matches at the beginning of the string and at the beginning of each line (immediately following each newline); and the pattern character \code{\$} matches at the end of the string and at the end of each line (immediately preceding each newline). By default, \code{\^} matches only at the beginning of the string, and \code{\$} only at the end of the string and immediately before the newline (if any) at the end of the string. \end{datadesc} \begin{datadesc}{S} \dataline{DOTALL} Make the \code{.} special character match any character at all, including a newline; without this flag, \code{.} will match anything \emph{except} a newline. \end{datadesc} \begin{datadesc}{X} \dataline{VERBOSE} Ignore whitespace within the pattern except when in a character class or preceded by an unescaped backslash, and, when a line contains a \code{\#} neither in a character class or preceded by an unescaped backslash, all characters from the leftmost such \code{\#} through the end of the line are ignored. \end{datadesc} \begin{funcdesc}{escape}{string} Return \var{string} with all non-alphanumerics backslashed; this is useful if you want to match an arbitrary literal string that may have regular expression metacharacters in it. \end{funcdesc} \begin{funcdesc}{match}{pattern, string\optional{, flags}} If zero or more characters at the beginning of \var{string} match the regular expression \var{pattern}, return a corresponding \class{MatchObject} instance. Return \code{None} if the string does not match the pattern; note that this is different from a zero-length match. \end{funcdesc} \begin{funcdesc}{search}{pattern, string\optional{, flags}} Scan through \var{string} looking for a location where the regular expression \var{pattern} produces a match, and return a corresponding \class{MatchObject} instance. Return \code{None} if no position in the string matches the pattern; note that this is different from finding a zero-length match at some point in the string. \end{funcdesc} \begin{funcdesc}{split}{pattern, string, \optional{, maxsplit\code{ = 0}}} Split \var{string} by the occurrences of \var{pattern}. If capturing parentheses are used in pattern, then occurrences of patterns or subpatterns are also returned. If \var{maxsplit} is nonzero, at most \var{maxsplit} splits occur, and the remainder of the string is returned as the final element of the list. (Incompatibility note: in the original Python 1.5 release, \var{maxsplit} was ignored. This has been fixed in later releases.) % \begin{verbatim} >>> re.split('[\W]+', 'Words, words, words.') ['Words', 'words', 'words', ''] >>> re.split('([\W]+)', 'Words, words, words.') ['Words', ', ', 'words', ', ', 'words', '.', ''] >>> re.split('[\W]+', 'Words, words, words.', 1) ['Words', 'words, words.'] \end{verbatim} % This function combines and extends the functionality of the old \function{regsub.split()} and \function{regsub.splitx()}. \end{funcdesc} \begin{funcdesc}{sub}{pattern, repl, string\optional{, count\code{ = 0}}} Return the string obtained by replacing the leftmost non-overlapping occurrences of \var{pattern} in \var{string} by the replacement \var{repl}. If the pattern isn't found, \var{string} is returned unchanged. \var{repl} can be a string or a function; if a function, it is called for every non-overlapping occurance of \var{pattern}. The function takes a single match object argument, and returns the replacement string. For example: % \begin{verbatim} >>> def dashrepl(matchobj): .... if matchobj.group(0) == '-': return ' ' .... else: return '-' >>> re.sub('-{1,2}', dashrepl, 'pro----gram-files') 'pro--gram files' \end{verbatim} % The pattern may be a string or a regex object; if you need to specify regular expression flags, you must use a regex object, or use embedded modifiers in a pattern; e.g. \samp{sub("(?i)b+", "x", "bbbb BBBB")} returns \code{'x x'}. The optional argument \var{count} is the maximum number of pattern occurrences to be replaced; count must be a non-negative integer, and the default value of 0 means to replace all occurrences. Empty matches for the pattern are replaced only when not adjacent to a previous match, so \samp{sub('x*', '-', 'abc')} returns \code{'-a-b-c-'}. If \var{repl} is a string, any backslash escapes in it are processed. That is, \samp{\e n} is converted to a single newline character, \samp{\e r} is converted to a linefeed, and so forth. Unknown escapes such as \samp{\e j} are XXX. Backreferences, such as \samp{\e 6} are replaced with the substring matched by group 6 in the pattern. In addition to character escapes and backreferences as described above, \samp{\e g} will use the substring matched by the group named \samp{name}, as defined by the \samp{(?P...)} syntax. \samp{\e g} uses the corresponding group number; \samp{\e g<2>} is therefore equivalent to \samp{\e 2}, but isn't ambiguous in a replacement such as \samp{\e g<2>0}. \samp{\e 20} would be interpreted as a reference to group 20, not a reference to group 2 followed by the literal character \samp{0}. \end{funcdesc} \begin{funcdesc}{subn}{pattern, repl, string\optional{, count\code{ = 0}}} Perform the same operation as \function{sub()}, but return a tuple \code{(\var{new_string}, \var{number_of_subs_made})}. \end{funcdesc} \begin{excdesc}{error} Exception raised when a string passed to one of the functions here is not a valid regular expression (e.g., unmatched parentheses) or when some other error occurs during compilation or matching. It is never an error if a string contains no match for a pattern. \end{excdesc} \subsection{Regular Expression Objects} Compiled regular expression objects support the following methods and attributes: \begin{methoddesc}[RegexObject]{match}{string\optional{, pos}\optional{, endpos}} If zero or more characters at the beginning of \var{string} match this regular expression, return a corresponding \class{MatchObject} instance. Return \code{None} if the string does not match the pattern; note that this is different from a zero-length match. The optional second parameter \var{pos} gives an index in the string where the search is to start; it defaults to \code{0}. The \samp{\^} pattern character will match at the index where the search is to start. The optional parameter \var{endpos} limits how far the string will be searched; it will be as if the string is \var{endpos} characters long, so only the characters from \var{pos} to \var{endpos} will be searched for a match. \end{methoddesc} \begin{methoddesc}[RegexObject]{search}{string\optional{, pos}\optional{, endpos}} Scan through \var{string} looking for a location where this regular expression produces a match. Return \code{None} if no position in the string matches the pattern; note that this is different from finding a zero-length match at some point in the string. The optional \var{pos} and \var{endpos} parameters have the same meaning as for the \method{match()} method. \end{methoddesc} \begin{methoddesc}[RegexObject]{split}{string, \optional{, maxsplit\code{ = 0}}} Identical to the \function{split()} function, using the compiled pattern. \end{methoddesc} \begin{methoddesc}[RegexObject]{sub}{repl, string\optional{, count\code{ = 0}}} Identical to the \function{sub()} function, using the compiled pattern. \end{methoddesc} \begin{methoddesc}[RegexObject]{subn}{repl, string\optional{, count\code{ = 0}}} Identical to the \function{subn()} function, using the compiled pattern. \end{methoddesc} \begin{memberdesc}[RegexObject]{flags} The flags argument used when the regex object was compiled, or \code{0} if no flags were provided. \end{memberdesc} \begin{memberdesc}[RegexObject]{groupindex} A dictionary mapping any symbolic group names defined by \code{(?P<\var{id}>)} to group numbers. The dictionary is empty if no symbolic groups were used in the pattern. \end{memberdesc} \begin{memberdesc}[RegexObject]{pattern} The pattern string from which the regex object was compiled. \end{memberdesc} \subsection{Match Objects} \class{MatchObject} instances support the following methods and attributes: \begin{methoddesc}[MatchObject]{group}{\optional{group1, group2, ...}} Returns one or more subgroups of the match. If there is a single argument, the result is a single string; if there are multiple arguments, the result is a tuple with one item per argument. Without arguments, \var{group1} defaults to zero (i.e. the whole match is returned). If a \var{groupN} 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 the corresponding parenthesized group. If no such group exists, the corresponding result is \code{None}. If the regular expression uses the \code{(?P<\var{name}>...)} syntax, the \var{groupN} arguments may also be strings identifying groups by their group name. A moderately complicated example: \begin{verbatim} m = re.match(r"(?P\d+)\.(\d*)", '3.14') \end{verbatim} After performing this match, \code{m.group(1)} is \code{'3'}, as is \code{m.group('int')}, and \code{m.group(2)} is \code{'14'}. \end{methoddesc} \begin{methoddesc}[MatchObject]{groups}{} Return a tuple containing all the subgroups of the match, from 1 up to however many groups are in the pattern. Groups that did not participate in the match have values of \code{None}. (Incompatibility note: in the original Python 1.5 release, if the tuple was one element long, a string would be returned instead. In later versions, a singleton tuple is returned in such cases.) \end{methoddesc} \begin{methoddesc}[MatchObject]{start}{\optional{group}} \funcline{end}{\optional{group}} Return the indices of the start and end of the substring matched by \var{group}; \var{group} defaults to zero (meaning the whole matched substring). Return \code{None} if \var{group} exists but did not contribute to the match. For a match object \var{m}, and a group \var{g} that did contribute to the match, the substring matched by group \var{g} (equivalent to \code{\var{m}.group(\var{g})}) is \begin{verbatim} m.string[m.start(g):m.end(g)] \end{verbatim} Note that \code{m.start(\var{group})} will equal \code{m.end(\var{group})} if \var{group} matched a null string. For example, after \code{\var{m} = re.search('b(c?)', 'cba')}, \code{\var{m}.start(0)} is 1, \code{\var{m}.end(0)} is 2, \code{\var{m}.start(1)} and \code{\var{m}.end(1)} are both 2, and \code{\var{m}.start(2)} raises an \exception{IndexError} exception. \end{methoddesc} \begin{methoddesc}[MatchObject]{span}{\optional{group}} For \class{MatchObject} \var{m}, return the 2-tuple \code{(\var{m}.start(\var{group}), \var{m}.end(\var{group}))}. Note that if \var{group} did not contribute to the match, this is \code{(None, None)}. Again, \var{group} defaults to zero. \end{methoddesc} \begin{memberdesc}[MatchObject]{pos} The value of \var{pos} which was passed to the \function{search()} or \function{match()} function. This is the index into the string at which the regex engine started looking for a match. \end{memberdesc} \begin{memberdesc}[MatchObject]{endpos} The value of \var{endpos} which was passed to the \function{search()} or \function{match()} function. This is the index into the string beyond which the regex engine will not go. \end{memberdesc} \begin{memberdesc}[MatchObject]{re} The regular expression object whose \method{match()} or \method{search()} method produced this \class{MatchObject} instance. \end{memberdesc} \begin{memberdesc}[MatchObject]{string} The string passed to \function{match()} or \function{search()}. \end{memberdesc} \begin{seealso} \seetext{Jeffrey Friedl, \emph{Mastering Regular Expressions}, O'Reilly. The Python material in this book dates from before the \module{re} module, but it covers writing good regular expression patterns in great detail.} \end{seealso}