blob: 92de4c06a41b0287ce7f8f39bc39868407709217 [file] [log] [blame]
Fred Drake295da241998-08-10 19:42:37 +00001\section{\module{re} ---
2 New Perl-style regular expression search and match operations.}
Fred Drake66da9d61998-08-07 18:57:18 +00003\declaremodule{standard}{re}
4\moduleauthor{Andrew M. Kuchling}{akuchling@acm.org}
5\sectionauthor{Andrew M. Kuchling}{akuchling@acm.org}
Guido van Rossum1acceb01997-08-14 23:12:18 +00006
Fred Drakeb91e9341998-07-23 17:59:49 +00007
Fred Drake66da9d61998-08-07 18:57:18 +00008\modulesynopsis{New Perl-style regular expression search and match
9operations.}
Fred Drakeb91e9341998-07-23 17:59:49 +000010
Guido van Rossum1acceb01997-08-14 23:12:18 +000011
Guido van Rossum1acceb01997-08-14 23:12:18 +000012This module provides regular expression matching operations similar to
Andrew M. Kuchling25332811998-04-09 14:56:04 +000013those found in Perl. It's 8-bit clean: the strings being processed
14may contain both null bytes and characters whose high bit is set. Regular
15expression patterns may not contain null bytes, but they may contain
16characters with the high bit set. The \module{re} module is always
Guido van Rossum0b334101997-12-08 17:33:40 +000017available.
Guido van Rossum1acceb01997-08-14 23:12:18 +000018
Andrew M. Kuchling25332811998-04-09 14:56:04 +000019Regular expressions use the backslash character (\character{\e}) to
Guido van Rossum1acceb01997-08-14 23:12:18 +000020indicate special forms or to allow special characters to be used
21without invoking their special meaning. This collides with Python's
22usage of the same character for the same purpose in string literals;
23for example, to match a literal backslash, one might have to write
Andrew M. Kuchling25332811998-04-09 14:56:04 +000024\code{'\e\e\e\e'} as the pattern string, because the regular expression
Fred Drake20e01961998-02-19 15:09:35 +000025must be \samp{\e\e}, and each backslash must be expressed as
26\samp{\e\e} inside a regular Python string literal.
Guido van Rossum1acceb01997-08-14 23:12:18 +000027
28The solution is to use Python's raw string notation for regular
29expression patterns; backslashes are not handled in any special way in
Andrew M. Kuchling25332811998-04-09 14:56:04 +000030a string literal prefixed with \character{r}. So \code{r"\e n"} is a
31two-character string containing \character{\e} and \character{n},
32while \code{"\e n"} is a one-character string containing a newline.
33Usually patterns will be expressed in Python code using this raw
34string notation.
Guido van Rossum1acceb01997-08-14 23:12:18 +000035
Fred Draked16d4981998-09-10 20:21:00 +000036\subsection{Regular Expression Syntax \label{re-syntax}}
Guido van Rossum1acceb01997-08-14 23:12:18 +000037
38A regular expression (or RE) specifies a set of strings that matches
39it; the functions in this module let you check if a particular string
40matches a given regular expression (or if a given regular expression
41matches a particular string, which comes down to the same thing).
42
43Regular expressions can be concatenated to form new regular
44expressions; if \emph{A} and \emph{B} are both regular expressions,
45then \emph{AB} is also an regular expression. If a string \emph{p}
46matches A and another string \emph{q} matches B, the string \emph{pq}
47will match AB. Thus, complex expressions can easily be constructed
48from simpler primitive expressions like the ones described here. For
49details of the theory and implementation of regular expressions,
50consult the Friedl book referenced below, or almost any textbook about
51compiler construction.
52
Andrew M. Kuchlingc1cea201998-10-28 15:44:14 +000053A brief explanation of the format of regular expressions follows. For
54further information and a gentler presentation, consult the Regular
55Expression HOWTO, accessible from \url{http://www.python.org/doc/howto/}.
Guido van Rossum1acceb01997-08-14 23:12:18 +000056
57Regular expressions can contain both special and ordinary characters.
Andrew M. Kuchling25332811998-04-09 14:56:04 +000058Most ordinary characters, like \character{A}, \character{a}, or \character{0},
Guido van Rossum1acceb01997-08-14 23:12:18 +000059are the simplest regular expressions; they simply match themselves.
Andrew M. Kuchling25332811998-04-09 14:56:04 +000060You can concatenate ordinary characters, so \regexp{last} matches the
61string \code{'last'}. (In the rest of this section, we'll write RE's in
62\regexp{this special style}, usually without quotes, and strings to be
63matched \code{'in single quotes'}.)
Guido van Rossum1acceb01997-08-14 23:12:18 +000064
Andrew M. Kuchling25332811998-04-09 14:56:04 +000065Some characters, like \character{|} or \character{(}, are special. Special
Guido van Rossum1acceb01997-08-14 23:12:18 +000066characters either stand for classes of ordinary characters, or affect
67how the regular expressions around them are interpreted.
68
69The special characters are:
Fred Drake2705e801998-02-16 21:21:13 +000070% define these since they're used twice:
71\newcommand{\MyLeftMargin}{0.7in}
72\newcommand{\MyLabelWidth}{0.65in}
Fred Draked16d4981998-09-10 20:21:00 +000073
Fred Drake2705e801998-02-16 21:21:13 +000074\begin{list}{}{\leftmargin \MyLeftMargin \labelwidth \MyLabelWidth}
Fred Draked16d4981998-09-10 20:21:00 +000075
Andrew M. Kuchling25332811998-04-09 14:56:04 +000076\item[\character{.}] (Dot.) In the default mode, this matches any
Fred Drake20e01961998-02-19 15:09:35 +000077character except a newline. If the \constant{DOTALL} flag has been
Guido van Rossum1acceb01997-08-14 23:12:18 +000078specified, this matches any character including a newline.
Fred Draked16d4981998-09-10 20:21:00 +000079
Andrew M. Kuchling25332811998-04-09 14:56:04 +000080\item[\character{\^}] (Caret.) Matches the start of the string, and in
81\constant{MULTILINE} mode also matches immediately after each newline.
Fred Draked16d4981998-09-10 20:21:00 +000082
Andrew M. Kuchling25332811998-04-09 14:56:04 +000083\item[\character{\$}] Matches the end of the string, and in
Fred Drake20e01961998-02-19 15:09:35 +000084\constant{MULTILINE} mode also matches before a newline.
Andrew M. Kuchling25332811998-04-09 14:56:04 +000085\regexp{foo} matches both 'foo' and 'foobar', while the regular
86expression \regexp{foo\$} matches only 'foo'.
Fred Draked16d4981998-09-10 20:21:00 +000087
Andrew M. Kuchling25332811998-04-09 14:56:04 +000088\item[\character{*}] Causes the resulting RE to
Guido van Rossum1acceb01997-08-14 23:12:18 +000089match 0 or more repetitions of the preceding RE, as many repetitions
Andrew M. Kuchling25332811998-04-09 14:56:04 +000090as are possible. \regexp{ab*} will
Guido van Rossum1acceb01997-08-14 23:12:18 +000091match 'a', 'ab', or 'a' followed by any number of 'b's.
Fred Draked16d4981998-09-10 20:21:00 +000092
Andrew M. Kuchling25332811998-04-09 14:56:04 +000093\item[\character{+}] Causes the
Guido van Rossum1acceb01997-08-14 23:12:18 +000094resulting RE to match 1 or more repetitions of the preceding RE.
Andrew M. Kuchling25332811998-04-09 14:56:04 +000095\regexp{ab+} will match 'a' followed by any non-zero number of 'b's; it
Guido van Rossum1acceb01997-08-14 23:12:18 +000096will not match just 'a'.
Fred Draked16d4981998-09-10 20:21:00 +000097
Andrew M. Kuchling25332811998-04-09 14:56:04 +000098\item[\character{?}] Causes the resulting RE to
99match 0 or 1 repetitions of the preceding RE. \regexp{ab?} will
Guido van Rossum1acceb01997-08-14 23:12:18 +0000100match either 'a' or 'ab'.
Andrew M. Kuchling25332811998-04-09 14:56:04 +0000101\item[\code{*?}, \code{+?}, \code{??}] The \character{*}, \character{+}, and
102\character{?} qualifiers are all \dfn{greedy}; they match as much text as
Guido van Rossum1acceb01997-08-14 23:12:18 +0000103possible. Sometimes this behaviour isn't desired; if the RE
Andrew M. Kuchling25332811998-04-09 14:56:04 +0000104\regexp{<.*>} is matched against \code{'<H1>title</H1>'}, it will match the
105entire string, and not just \code{'<H1>'}.
106Adding \character{?} after the qualifier makes it perform the match in
107\dfn{non-greedy} or \dfn{minimal} fashion; as \emph{few} characters as
108possible will be matched. Using \regexp{.*?} in the previous
109expression will match only \code{'<H1>'}.
Fred Draked16d4981998-09-10 20:21:00 +0000110
Guido van Rossum0148bbf1997-12-22 22:41:40 +0000111\item[\code{\{\var{m},\var{n}\}}] Causes the resulting RE to match from
112\var{m} to \var{n} repetitions of the preceding RE, attempting to
Andrew M. Kuchlingc1cea201998-10-28 15:44:14 +0000113match as many repetitions as possible. For example, \regexp{a\{3,5\}}
114will match from 3 to 5 \character{a} characters. Omitting \var{n}
115specifies an infinite upper bound; you can't omit \var{m}.
Fred Draked16d4981998-09-10 20:21:00 +0000116
Guido van Rossum0148bbf1997-12-22 22:41:40 +0000117\item[\code{\{\var{m},\var{n}\}?}] Causes the resulting RE to
118match from \var{m} to \var{n} repetitions of the preceding RE,
119attempting to match as \emph{few} repetitions as possible. This is
120the non-greedy version of the previous qualifier. For example, on the
Fred Draked16d4981998-09-10 20:21:00 +00001216-character string \code{'aaaaaa'}, \regexp{a\{3,5\}} will match 5
122\character{a} characters, while \regexp{a\{3,5\}?} will only match 3
123characters.
124
125\item[\character{\e}] Either escapes special characters (permitting
126you to match characters like \character{*}, \character{?}, and so
127forth), or signals a special sequence; special sequences are discussed
128below.
Guido van Rossum1acceb01997-08-14 23:12:18 +0000129
130If you're not using a raw string to
131express the pattern, remember that Python also uses the
132backslash as an escape sequence in string literals; if the escape
133sequence isn't recognized by Python's parser, the backslash and
134subsequent character are included in the resulting string. However,
135if Python would recognize the resulting sequence, the backslash should
Fred Drake023f87f1998-01-12 19:16:24 +0000136be repeated twice. This is complicated and hard to understand, so
137it's highly recommended that you use raw strings for all but the
138simplest expressions.
Fred Draked16d4981998-09-10 20:21:00 +0000139
Guido van Rossum1acceb01997-08-14 23:12:18 +0000140\item[\code{[]}] Used to indicate a set of characters. Characters can
Guido van Rossum48d04371997-12-11 20:19:08 +0000141be listed individually, or a range of characters can be indicated by
Andrew M. Kuchling25332811998-04-09 14:56:04 +0000142giving two characters and separating them by a \character{-}. Special
143characters are not active inside sets. For example, \regexp{[akm\$]}
Fred Drake76547c51998-04-03 05:59:05 +0000144will match any of the characters \character{a}, \character{k},
Andrew M. Kuchling25332811998-04-09 14:56:04 +0000145\character{m}, or \character{\$}; \regexp{[a-z]}
146will match any lowercase letter, and \code{[a-zA-Z0-9]} matches any
Guido van Rossum48d04371997-12-11 20:19:08 +0000147letter or digit. Character classes such as \code{\e w} or \code {\e
148S} (defined below) are also acceptable inside a range. If you want to
Andrew M. Kuchling25332811998-04-09 14:56:04 +0000149include a \character{]} or a \character{-} inside a set, precede it with a
150backslash, or place it as the first character. The
151pattern \regexp{[]]} will match \code{']'}, for example.
Guido van Rossum1acceb01997-08-14 23:12:18 +0000152
Andrew M. Kuchling25332811998-04-09 14:56:04 +0000153You can match the characters not within a range by \dfn{complementing}
154the set. This is indicated by including a
155\character{\^} as the first character of the set; \character{\^} elsewhere will
156simply match the \character{\^} character. For example, \regexp{[\^5]}
157will match any character except \character{5}.
158
Andrew M. Kuchling25332811998-04-09 14:56:04 +0000159\item[\character{|}]\code{A|B}, where A and B can be arbitrary REs,
Guido van Rossum1acceb01997-08-14 23:12:18 +0000160creates a regular expression that will match either A or B. This can
Andrew M. Kuchling25332811998-04-09 14:56:04 +0000161be used inside groups (see below) as well. To match a literal \character{|},
162use \regexp{\e|}, or enclose it inside a character class, as in \regexp{[|]}.
Fred Draked16d4981998-09-10 20:21:00 +0000163
Guido van Rossum48d04371997-12-11 20:19:08 +0000164\item[\code{(...)}] Matches whatever regular expression is inside the
165parentheses, and indicates the start and end of a group; the contents
166of a group can be retrieved after a match has been performed, and can
Andrew M. Kuchling25332811998-04-09 14:56:04 +0000167be matched later in the string with the \regexp{\e \var{number}} special
Fred Draked16d4981998-09-10 20:21:00 +0000168sequence, described below. To match the literals \character{(} or
169\character{')}, use \regexp{\e(} or \regexp{\e)}, or enclose them
170inside a character class: \regexp{[(] [)]}.
171
172\item[\code{(?...)}] This is an extension notation (a \character{?}
173following a \character{(} is not meaningful otherwise). The first
174character after the \character{?}
Guido van Rossum0b334101997-12-08 17:33:40 +0000175determines what the meaning and further syntax of the construct is.
Guido van Rossume9625e81998-04-02 01:32:24 +0000176Extensions usually do not create a new group;
Andrew M. Kuchling25332811998-04-09 14:56:04 +0000177\regexp{(?P<\var{name}>...)} is the only exception to this rule.
Guido van Rossum0b334101997-12-08 17:33:40 +0000178Following are the currently supported extensions.
Fred Draked16d4981998-09-10 20:21:00 +0000179
Andrew M. Kuchling25332811998-04-09 14:56:04 +0000180\item[\code{(?iLmsx)}] (One or more letters from the set \character{i},
181\character{L}, \character{m}, \character{s}, \character{x}.) The group matches
Fred Drake023f87f1998-01-12 19:16:24 +0000182the empty string; the letters set the corresponding flags
Fred Drake20e01961998-02-19 15:09:35 +0000183(\constant{re.I}, \constant{re.L}, \constant{re.M}, \constant{re.S},
184\constant{re.X}) for the entire regular expression. This is useful if
Guido van Rossume9625e81998-04-02 01:32:24 +0000185you wish to include the flags as part of the regular expression, instead
Fred Drake20e01961998-02-19 15:09:35 +0000186of passing a \var{flag} argument to the \function{compile()} function.
Fred Draked16d4981998-09-10 20:21:00 +0000187
Guido van Rossum1acceb01997-08-14 23:12:18 +0000188\item[\code{(?:...)}] A non-grouping version of regular parentheses.
Andrew M. Kuchling25332811998-04-09 14:56:04 +0000189Matches whatever regular expression is inside the parentheses, but the
190substring matched by the
Guido van Rossum1acceb01997-08-14 23:12:18 +0000191group \emph{cannot} be retrieved after performing a match or
192referenced later in the pattern.
Fred Draked16d4981998-09-10 20:21:00 +0000193
Guido van Rossum1acceb01997-08-14 23:12:18 +0000194\item[\code{(?P<\var{name}>...)}] Similar to regular parentheses, but
Guido van Rossume9625e81998-04-02 01:32:24 +0000195the substring matched by the group is accessible via the symbolic group
Guido van Rossum1acceb01997-08-14 23:12:18 +0000196name \var{name}. Group names must be valid Python identifiers. A
197symbolic group is also a numbered group, just as if the group were not
198named. So the group named 'id' in the example above can also be
199referenced as the numbered group 1.
200
Guido van Rossum48d04371997-12-11 20:19:08 +0000201For example, if the pattern is
Andrew M. Kuchling25332811998-04-09 14:56:04 +0000202\regexp{(?P<id>[a-zA-Z_]\e w*)}, the group can be referenced by its
Guido van Rossum1acceb01997-08-14 23:12:18 +0000203name in arguments to methods of match objects, such as \code{m.group('id')}
Fred Drake023f87f1998-01-12 19:16:24 +0000204or \code{m.end('id')}, and also by name in pattern text
Andrew M. Kuchling25332811998-04-09 14:56:04 +0000205(e.g. \regexp{(?P=id)}) and replacement text (e.g. \code{\e g<id>}).
Fred Draked16d4981998-09-10 20:21:00 +0000206
Fred Drake023f87f1998-01-12 19:16:24 +0000207\item[\code{(?P=\var{name})}] Matches whatever text was matched by the
208earlier group named \var{name}.
Fred Draked16d4981998-09-10 20:21:00 +0000209
Fred Drake023f87f1998-01-12 19:16:24 +0000210\item[\code{(?\#...)}] A comment; the contents of the parentheses are
211simply ignored.
Fred Draked16d4981998-09-10 20:21:00 +0000212
Andrew M. Kuchling25332811998-04-09 14:56:04 +0000213\item[\code{(?=...)}] Matches if \regexp{...} matches next, but doesn't
Fred Drake023f87f1998-01-12 19:16:24 +0000214consume any of the string. This is called a lookahead assertion. For
Andrew M. Kuchling25332811998-04-09 14:56:04 +0000215example, \regexp{Isaac (?=Asimov)} will match \code{'Isaac~'} only if it's
216followed by \code{'Asimov'}.
Fred Draked16d4981998-09-10 20:21:00 +0000217
Andrew M. Kuchling25332811998-04-09 14:56:04 +0000218\item[\code{(?!...)}] Matches if \regexp{...} doesn't match next. This
Fred Drake023f87f1998-01-12 19:16:24 +0000219is a negative lookahead assertion. For example,
Andrew M. Kuchling25332811998-04-09 14:56:04 +0000220\regexp{Isaac (?!Asimov)} will match \code{'Isaac~'} only if it's \emph{not}
221followed by \code{'Asimov'}.
Guido van Rossum0b334101997-12-08 17:33:40 +0000222
Fred Drake2705e801998-02-16 21:21:13 +0000223\end{list}
Guido van Rossum1acceb01997-08-14 23:12:18 +0000224
Andrew M. Kuchling25332811998-04-09 14:56:04 +0000225The special sequences consist of \character{\e} and a character from the
Guido van Rossum1acceb01997-08-14 23:12:18 +0000226list below. If the ordinary character is not on the list, then the
227resulting RE will match the second character. For example,
Andrew M. Kuchling25332811998-04-09 14:56:04 +0000228\regexp{\e\$} matches the character \character{\$}.
Guido van Rossum1acceb01997-08-14 23:12:18 +0000229
Fred Drake2705e801998-02-16 21:21:13 +0000230\begin{list}{}{\leftmargin \MyLeftMargin \labelwidth \MyLabelWidth}
Guido van Rossum1acceb01997-08-14 23:12:18 +0000231
232%
233\item[\code{\e \var{number}}] Matches the contents of the group of the
Guido van Rossum0b334101997-12-08 17:33:40 +0000234same number. Groups are numbered starting from 1. For example,
Andrew M. Kuchling25332811998-04-09 14:56:04 +0000235\regexp{(.+) \e 1} matches \code{'the the'} or \code{'55 55'}, but not
236\code{'the end'} (note
Guido van Rossum0b334101997-12-08 17:33:40 +0000237the space after the group). This special sequence can only be used to
238match one of the first 99 groups. If the first digit of \var{number}
239is 0, or \var{number} is 3 octal digits long, it will not be interpreted
240as a group match, but as the character with octal value \var{number}.
Andrew M. Kuchling25332811998-04-09 14:56:04 +0000241Inside the \character{[} and \character{]} of a character class, all numeric
Guido van Rossume9625e81998-04-02 01:32:24 +0000242escapes are treated as characters.
Guido van Rossum1acceb01997-08-14 23:12:18 +0000243%
244\item[\code{\e A}] Matches only at the start of the string.
245%
246\item[\code{\e b}] Matches the empty string, but only at the
247beginning or end of a word. A word is defined as a sequence of
248alphanumeric characters, so the end of a word is indicated by
Guido van Rossum48d04371997-12-11 20:19:08 +0000249whitespace or a non-alphanumeric character. Inside a character range,
Andrew M. Kuchling25332811998-04-09 14:56:04 +0000250\regexp{\e b} represents the backspace character, for compatibility with
Guido van Rossum48d04371997-12-11 20:19:08 +0000251Python's string literals.
Guido van Rossum1acceb01997-08-14 23:12:18 +0000252%
Guido van Rossum0b334101997-12-08 17:33:40 +0000253\item[\code{\e B}] Matches the empty string, but only when it is
254\emph{not} at the beginning or end of a word.
Guido van Rossum1acceb01997-08-14 23:12:18 +0000255%
256\item[\code{\e d}]Matches any decimal digit; this is
Andrew M. Kuchling25332811998-04-09 14:56:04 +0000257equivalent to the set \regexp{[0-9]}.
Guido van Rossum1acceb01997-08-14 23:12:18 +0000258%
259\item[\code{\e D}]Matches any non-digit character; this is
Andrew M. Kuchling25332811998-04-09 14:56:04 +0000260equivalent to the set \regexp{[\^0-9]}.
Guido van Rossum1acceb01997-08-14 23:12:18 +0000261%
262\item[\code{\e s}]Matches any whitespace character; this is
Andrew M. Kuchling25332811998-04-09 14:56:04 +0000263equivalent to the set \regexp{[ \e t\e n\e r\e f\e v]}.
Guido van Rossum1acceb01997-08-14 23:12:18 +0000264%
265\item[\code{\e S}]Matches any non-whitespace character; this is
Andrew M. Kuchling25332811998-04-09 14:56:04 +0000266equivalent to the set \regexp{[\^\ \e t\e n\e r\e f\e v]}.
Guido van Rossum1acceb01997-08-14 23:12:18 +0000267%
Fred Drake20e01961998-02-19 15:09:35 +0000268\item[\code{\e w}]When the \constant{LOCALE} flag is not specified,
Fred Drake023f87f1998-01-12 19:16:24 +0000269matches any alphanumeric character; this is equivalent to the set
Andrew M. Kuchling25332811998-04-09 14:56:04 +0000270\regexp{[a-zA-Z0-9_]}. With \constant{LOCALE}, it will match the set
271\regexp{[0-9_]} plus whatever characters are defined as letters for the
Fred Drake023f87f1998-01-12 19:16:24 +0000272current locale.
Guido van Rossum1acceb01997-08-14 23:12:18 +0000273%
Fred Drake20e01961998-02-19 15:09:35 +0000274\item[\code{\e W}]When the \constant{LOCALE} flag is not specified,
Fred Drake023f87f1998-01-12 19:16:24 +0000275matches any non-alphanumeric character; this is equivalent to the set
Andrew M. Kuchling25332811998-04-09 14:56:04 +0000276\regexp{[\^a-zA-Z0-9_]}. With \constant{LOCALE}, it will match any
277character not in the set \regexp{[0-9_]}, and not defined as a letter
Guido van Rossum0b334101997-12-08 17:33:40 +0000278for the current locale.
Guido van Rossum1acceb01997-08-14 23:12:18 +0000279
280\item[\code{\e Z}]Matches only at the end of the string.
281%
282
283\item[\code{\e \e}] Matches a literal backslash.
284
Fred Drake2705e801998-02-16 21:21:13 +0000285\end{list}
Guido van Rossum1acceb01997-08-14 23:12:18 +0000286
Fred Drake42de1851998-04-20 16:28:44 +0000287
Guido van Rossum1acceb01997-08-14 23:12:18 +0000288\subsection{Module Contents}
Fred Drake78f8e981997-12-29 21:39:39 +0000289\nodename{Contents of Module re}
Guido van Rossum1acceb01997-08-14 23:12:18 +0000290
291The module defines the following functions and constants, and an exception:
292
Guido van Rossum1acceb01997-08-14 23:12:18 +0000293
Fred Drake013ad981998-03-08 07:38:27 +0000294\begin{funcdesc}{compile}{pattern\optional{, flags}}
Guido van Rossum1acceb01997-08-14 23:12:18 +0000295 Compile a regular expression pattern into a regular expression
Fred Drake20e01961998-02-19 15:09:35 +0000296 object, which can be used for matching using its \function{match()} and
297 \function{search()} methods, described below.
Guido van Rossum1acceb01997-08-14 23:12:18 +0000298
Guido van Rossum0b334101997-12-08 17:33:40 +0000299 The expression's behaviour can be modified by specifying a
300 \var{flags} value. Values can be any of the following variables,
301 combined using bitwise OR (the \code{|} operator).
302
Fred Drake76547c51998-04-03 05:59:05 +0000303The sequence
304
305\begin{verbatim}
306prog = re.compile(pat)
307result = prog.match(str)
308\end{verbatim}
309
310is equivalent to
311
312\begin{verbatim}
313result = re.match(pat, str)
314\end{verbatim}
315
316but the version using \function{compile()} is more efficient when the
317expression will be used several times in a single program.
318%(The compiled version of the last pattern passed to
319%\function{regex.match()} or \function{regex.search()} is cached, so
320%programs that use only a single regular expression at a time needn't
321%worry about compiling regular expressions.)
322\end{funcdesc}
323
Fred Drake013ad981998-03-08 07:38:27 +0000324\begin{datadesc}{I}
325\dataline{IGNORECASE}
Andrew M. Kuchling25332811998-04-09 14:56:04 +0000326Perform case-insensitive matching; expressions like \regexp{[A-Z]} will match
Guido van Rossum48d04371997-12-11 20:19:08 +0000327lowercase letters, too. This is not affected by the current locale.
Fred Drake013ad981998-03-08 07:38:27 +0000328\end{datadesc}
Guido van Rossum0b334101997-12-08 17:33:40 +0000329
Fred Drake013ad981998-03-08 07:38:27 +0000330\begin{datadesc}{L}
331\dataline{LOCALE}
Andrew M. Kuchling25332811998-04-09 14:56:04 +0000332Make \regexp{\e w}, \regexp{\e W}, \regexp{\e b},
333\regexp{\e B}, dependent on the current locale.
Fred Drake013ad981998-03-08 07:38:27 +0000334\end{datadesc}
Guido van Rossuma42c1781997-12-09 20:41:47 +0000335
Fred Drake013ad981998-03-08 07:38:27 +0000336\begin{datadesc}{M}
337\dataline{MULTILINE}
Andrew M. Kuchling25332811998-04-09 14:56:04 +0000338When specified, the pattern character \character{\^} matches at the
Fred Drake023f87f1998-01-12 19:16:24 +0000339beginning of the string and at the beginning of each line
340(immediately following each newline); and the pattern character
Andrew M. Kuchling25332811998-04-09 14:56:04 +0000341\character{\$} matches at the end of the string and at the end of each line
Guido van Rossum48d04371997-12-11 20:19:08 +0000342(immediately preceding each newline).
Andrew M. Kuchling25332811998-04-09 14:56:04 +0000343By default, \character{\^} matches only at the beginning of the string, and
344\character{\$} only at the end of the string and immediately before the
Guido van Rossum0b334101997-12-08 17:33:40 +0000345newline (if any) at the end of the string.
Fred Drake013ad981998-03-08 07:38:27 +0000346\end{datadesc}
Guido van Rossum0b334101997-12-08 17:33:40 +0000347
Fred Drake013ad981998-03-08 07:38:27 +0000348\begin{datadesc}{S}
349\dataline{DOTALL}
Andrew M. Kuchling25332811998-04-09 14:56:04 +0000350Make the \character{.} special character match any character at all, including a
351newline; without this flag, \character{.} will match anything \emph{except}
Fred Drake78f8e981997-12-29 21:39:39 +0000352a newline.
Fred Drake013ad981998-03-08 07:38:27 +0000353\end{datadesc}
Guido van Rossum48d04371997-12-11 20:19:08 +0000354
Fred Drake013ad981998-03-08 07:38:27 +0000355\begin{datadesc}{X}
356\dataline{VERBOSE}
Andrew M. Kuchling25332811998-04-09 14:56:04 +0000357This flag allows you to write regular expressions that look nicer.
358Whitespace within the pattern is ignored,
Guido van Rossum48d04371997-12-11 20:19:08 +0000359except when in a character class or preceded by an unescaped
Andrew M. Kuchling25332811998-04-09 14:56:04 +0000360backslash, and, when a line contains a \character{\#} neither in a character
Guido van Rossum48d04371997-12-11 20:19:08 +0000361class or preceded by an unescaped backslash, all characters from the
Andrew M. Kuchling25332811998-04-09 14:56:04 +0000362leftmost such \character{\#} through the end of the line are ignored.
363% XXX should add an example here
Fred Drake013ad981998-03-08 07:38:27 +0000364\end{datadesc}
Guido van Rossum0b334101997-12-08 17:33:40 +0000365
Guido van Rossum0b334101997-12-08 17:33:40 +0000366
Guido van Rossum7d447aa1998-10-13 16:03:52 +0000367\begin{funcdesc}{search}{pattern, string\optional{, flags}}
368 Scan through \var{string} looking for a location where the regular
369 expression \var{pattern} produces a match, and return a
370 corresponding \class{MatchObject} instance.
371 Return \code{None} if no
372 position in the string matches the pattern; note that this is
373 different from finding a zero-length match at some point in the string.
Guido van Rossum1acceb01997-08-14 23:12:18 +0000374\end{funcdesc}
375
Fred Drake013ad981998-03-08 07:38:27 +0000376\begin{funcdesc}{match}{pattern, string\optional{, flags}}
Guido van Rossum1acceb01997-08-14 23:12:18 +0000377 If zero or more characters at the beginning of \var{string} match
378 the regular expression \var{pattern}, return a corresponding
Fred Drake20e01961998-02-19 15:09:35 +0000379 \class{MatchObject} instance. Return \code{None} if the string does not
Guido van Rossum1acceb01997-08-14 23:12:18 +0000380 match the pattern; note that this is different from a zero-length
381 match.
382\end{funcdesc}
383
Fred Drake013ad981998-03-08 07:38:27 +0000384\begin{funcdesc}{split}{pattern, string, \optional{, maxsplit\code{ = 0}}}
Guido van Rossum1acceb01997-08-14 23:12:18 +0000385 Split \var{string} by the occurrences of \var{pattern}. If
Andrew M. Kuchlingd22e2501998-08-14 14:49:20 +0000386 capturing parentheses are used in \var{pattern}, then the text of all
387 groups in the pattern are also returned as part of the resulting list.
Guido van Rossum97546391998-01-12 18:58:53 +0000388 If \var{maxsplit} is nonzero, at most \var{maxsplit} splits
389 occur, and the remainder of the string is returned as the final
390 element of the list. (Incompatibility note: in the original Python
391 1.5 release, \var{maxsplit} was ignored. This has been fixed in
392 later releases.)
Guido van Rossum1acceb01997-08-14 23:12:18 +0000393%
Fred Drake19479911998-02-13 06:58:54 +0000394\begin{verbatim}
Andrew M. Kuchlingd22e2501998-08-14 14:49:20 +0000395>>> re.split('\W+', 'Words, words, words.')
Guido van Rossum1acceb01997-08-14 23:12:18 +0000396['Words', 'words', 'words', '']
Andrew M. Kuchlingd22e2501998-08-14 14:49:20 +0000397>>> re.split('(\W+)', 'Words, words, words.')
Guido van Rossum1acceb01997-08-14 23:12:18 +0000398['Words', ', ', 'words', ', ', 'words', '.', '']
Andrew M. Kuchlingd22e2501998-08-14 14:49:20 +0000399>>> re.split('\W+', 'Words, words, words.', 1)
Guido van Rossum97546391998-01-12 18:58:53 +0000400['Words', 'words, words.']
Fred Drake19479911998-02-13 06:58:54 +0000401\end{verbatim}
Guido van Rossum1acceb01997-08-14 23:12:18 +0000402%
403 This function combines and extends the functionality of
Fred Drake20e01961998-02-19 15:09:35 +0000404 the old \function{regsub.split()} and \function{regsub.splitx()}.
Guido van Rossum1acceb01997-08-14 23:12:18 +0000405\end{funcdesc}
406
Guido van Rossum6c373f71998-06-29 22:48:01 +0000407\begin{funcdesc}{findall}{pattern, string}
Fred Drake10a79851998-09-18 17:11:49 +0000408\versionadded{1.5.2}
Guido van Rossum6c373f71998-06-29 22:48:01 +0000409Return a list of all non-overlapping matches of \var{pattern} in
410\var{string}. If one or more groups are present in the pattern,
411return a list of groups; this will be a list of tuples if the pattern
412has more than one group. Empty matches are included in the result.
413\end{funcdesc}
414
Fred Drake013ad981998-03-08 07:38:27 +0000415\begin{funcdesc}{sub}{pattern, repl, string\optional{, count\code{ = 0}}}
Guido van Rossum1acceb01997-08-14 23:12:18 +0000416Return the string obtained by replacing the leftmost non-overlapping
417occurrences of \var{pattern} in \var{string} by the replacement
Barry Warsaw4552f3d1997-11-20 00:15:13 +0000418\var{repl}. If the pattern isn't found, \var{string} is returned
419unchanged. \var{repl} can be a string or a function; if a function,
420it is called for every non-overlapping occurance of \var{pattern}.
Guido van Rossum0b334101997-12-08 17:33:40 +0000421The function takes a single match object argument, and returns the
422replacement string. For example:
Barry Warsaw4552f3d1997-11-20 00:15:13 +0000423%
Fred Drake19479911998-02-13 06:58:54 +0000424\begin{verbatim}
Barry Warsaw4552f3d1997-11-20 00:15:13 +0000425>>> def dashrepl(matchobj):
Guido van Rossume9625e81998-04-02 01:32:24 +0000426.... if matchobj.group(0) == '-': return ' '
427.... else: return '-'
Barry Warsaw4552f3d1997-11-20 00:15:13 +0000428>>> re.sub('-{1,2}', dashrepl, 'pro----gram-files')
429'pro--gram files'
Fred Drake19479911998-02-13 06:58:54 +0000430\end{verbatim}
Barry Warsaw4552f3d1997-11-20 00:15:13 +0000431%
Guido van Rossum0b334101997-12-08 17:33:40 +0000432The pattern may be a string or a
Guido van Rossum48d04371997-12-11 20:19:08 +0000433regex object; if you need to specify
434regular expression flags, you must use a regex object, or use
435embedded modifiers in a pattern; e.g.
Fred Drake013ad981998-03-08 07:38:27 +0000436\samp{sub("(?i)b+", "x", "bbbb BBBB")} returns \code{'x x'}.
Fred Drake023f87f1998-01-12 19:16:24 +0000437
Guido van Rossum1acceb01997-08-14 23:12:18 +0000438The optional argument \var{count} is the maximum number of pattern
Andrew M. Kuchling25332811998-04-09 14:56:04 +0000439occurrences to be replaced; \var{count} must be a non-negative integer, and
Guido van Rossum1acceb01997-08-14 23:12:18 +0000440the default value of 0 means to replace all occurrences.
441
442Empty matches for the pattern are replaced only when not adjacent to a
Fred Drake013ad981998-03-08 07:38:27 +0000443previous match, so \samp{sub('x*', '-', 'abc')} returns \code{'-a-b-c-'}.
Guido van Rossume9625e81998-04-02 01:32:24 +0000444
445If \var{repl} is a string, any backslash escapes in it are processed.
446That is, \samp{\e n} is converted to a single newline character,
447\samp{\e r} is converted to a linefeed, and so forth. Unknown escapes
Andrew M. Kuchling25332811998-04-09 14:56:04 +0000448such as \samp{\e j} are left alone. Backreferences, such as \samp{\e 6}, are
Guido van Rossume9625e81998-04-02 01:32:24 +0000449replaced with the substring matched by group 6 in the pattern.
450
451In addition to character escapes and backreferences as described
452above, \samp{\e g<name>} will use the substring matched by the group
Andrew M. Kuchling25332811998-04-09 14:56:04 +0000453named \samp{name}, as defined by the \regexp{(?P<name>...)} syntax.
Guido van Rossume9625e81998-04-02 01:32:24 +0000454\samp{\e g<number>} uses the corresponding group number; \samp{\e
455g<2>} is therefore equivalent to \samp{\e 2}, but isn't ambiguous in a
456replacement such as \samp{\e g<2>0}. \samp{\e 20} would be
457interpreted as a reference to group 20, not a reference to group 2
Andrew M. Kuchling25332811998-04-09 14:56:04 +0000458followed by the literal character \character{0}.
Guido van Rossum1acceb01997-08-14 23:12:18 +0000459\end{funcdesc}
460
Fred Drake013ad981998-03-08 07:38:27 +0000461\begin{funcdesc}{subn}{pattern, repl, string\optional{, count\code{ = 0}}}
Fred Drake20e01961998-02-19 15:09:35 +0000462Perform the same operation as \function{sub()}, but return a tuple
Fred Drake023f87f1998-01-12 19:16:24 +0000463\code{(\var{new_string}, \var{number_of_subs_made})}.
Guido van Rossum1acceb01997-08-14 23:12:18 +0000464\end{funcdesc}
465
Guido van Rossum7d447aa1998-10-13 16:03:52 +0000466\begin{funcdesc}{escape}{string}
467 Return \var{string} with all non-alphanumerics backslashed; this is
468 useful if you want to match an arbitrary literal string that may have
469 regular expression metacharacters in it.
470\end{funcdesc}
471
Guido van Rossum1acceb01997-08-14 23:12:18 +0000472\begin{excdesc}{error}
473 Exception raised when a string passed to one of the functions here
474 is not a valid regular expression (e.g., unmatched parentheses) or
Fred Drake013ad981998-03-08 07:38:27 +0000475 when some other error occurs during compilation or matching. It is
476 never an error if a string contains no match for a pattern.
Guido van Rossum1acceb01997-08-14 23:12:18 +0000477\end{excdesc}
478
Fred Drake42de1851998-04-20 16:28:44 +0000479
Fred Draked16d4981998-09-10 20:21:00 +0000480\subsection{Regular Expression Objects \label{re-objects}}
Fred Drake42de1851998-04-20 16:28:44 +0000481
Guido van Rossum1acceb01997-08-14 23:12:18 +0000482Compiled regular expression objects support the following methods and
483attributes:
484
Guido van Rossum7d447aa1998-10-13 16:03:52 +0000485\begin{methoddesc}[RegexObject]{search}{string\optional{, pos}\optional{,
486 endpos}}
487 Scan through \var{string} looking for a location where this regular
488 expression produces a match, and return a
489 corresponding \class{MatchObject} instance. Return \code{None} if no
490 position in the string matches the pattern; note that this is
491 different from finding a zero-length match at some point in the string.
492
493 The optional \var{pos} and \var{endpos} parameters have the same
494 meaning as for the \method{match()} method.
495\end{methoddesc}
496
Fred Drake76547c51998-04-03 05:59:05 +0000497\begin{methoddesc}[RegexObject]{match}{string\optional{, pos}\optional{,
498 endpos}}
Guido van Rossumeb53ae41997-10-05 18:54:07 +0000499 If zero or more characters at the beginning of \var{string} match
500 this regular expression, return a corresponding
Fred Drake20e01961998-02-19 15:09:35 +0000501 \class{MatchObject} instance. Return \code{None} if the string does not
Guido van Rossumeb53ae41997-10-05 18:54:07 +0000502 match the pattern; note that this is different from a zero-length
503 match.
Guido van Rossum1acceb01997-08-14 23:12:18 +0000504
505 The optional second parameter \var{pos} gives an index in the string
Andrew M. Kuchling65b78631998-06-22 15:02:42 +0000506 where the search is to start; it defaults to \code{0}. This is not
507 completely equivalent to slicing the string; the \code{'\^'} pattern
508 character matches at the real beginning of the string and at positions
509 just after a newline, but not necessarily at the index where the search
510 is to start.
Guido van Rossum0b334101997-12-08 17:33:40 +0000511
512 The optional parameter \var{endpos} limits how far the string will
513 be searched; it will be as if the string is \var{endpos} characters
514 long, so only the characters from \var{pos} to \var{endpos} will be
515 searched for a match.
Fred Drake76547c51998-04-03 05:59:05 +0000516\end{methoddesc}
Guido van Rossum1acceb01997-08-14 23:12:18 +0000517
Fred Drake76547c51998-04-03 05:59:05 +0000518\begin{methoddesc}[RegexObject]{split}{string, \optional{,
519 maxsplit\code{ = 0}}}
Fred Drake20e01961998-02-19 15:09:35 +0000520Identical to the \function{split()} function, using the compiled pattern.
Fred Drake76547c51998-04-03 05:59:05 +0000521\end{methoddesc}
Guido van Rossum1acceb01997-08-14 23:12:18 +0000522
Guido van Rossum6c373f71998-06-29 22:48:01 +0000523\begin{methoddesc}[RegexObject]{findall}{string}
524Identical to the \function{findall()} function, using the compiled pattern.
525\end{methoddesc}
526
Fred Drake76547c51998-04-03 05:59:05 +0000527\begin{methoddesc}[RegexObject]{sub}{repl, string\optional{, count\code{ = 0}}}
Fred Drake20e01961998-02-19 15:09:35 +0000528Identical to the \function{sub()} function, using the compiled pattern.
Fred Drake76547c51998-04-03 05:59:05 +0000529\end{methoddesc}
Guido van Rossum1acceb01997-08-14 23:12:18 +0000530
Fred Drake76547c51998-04-03 05:59:05 +0000531\begin{methoddesc}[RegexObject]{subn}{repl, string\optional{,
532 count\code{ = 0}}}
Fred Drake20e01961998-02-19 15:09:35 +0000533Identical to the \function{subn()} function, using the compiled pattern.
Fred Drake76547c51998-04-03 05:59:05 +0000534\end{methoddesc}
Guido van Rossum1acceb01997-08-14 23:12:18 +0000535
Guido van Rossum1acceb01997-08-14 23:12:18 +0000536
Fred Drake76547c51998-04-03 05:59:05 +0000537\begin{memberdesc}[RegexObject]{flags}
Fred Drake013ad981998-03-08 07:38:27 +0000538The flags argument used when the regex object was compiled, or
539\code{0} if no flags were provided.
Fred Drake76547c51998-04-03 05:59:05 +0000540\end{memberdesc}
Guido van Rossum1acceb01997-08-14 23:12:18 +0000541
Fred Drake76547c51998-04-03 05:59:05 +0000542\begin{memberdesc}[RegexObject]{groupindex}
Fred Drake013ad981998-03-08 07:38:27 +0000543A dictionary mapping any symbolic group names defined by
Andrew M. Kuchling25332811998-04-09 14:56:04 +0000544\regexp{(?P<\var{id}>)} to group numbers. The dictionary is empty if no
Guido van Rossum1acceb01997-08-14 23:12:18 +0000545symbolic groups were used in the pattern.
Fred Drake76547c51998-04-03 05:59:05 +0000546\end{memberdesc}
Guido van Rossum1acceb01997-08-14 23:12:18 +0000547
Fred Drake76547c51998-04-03 05:59:05 +0000548\begin{memberdesc}[RegexObject]{pattern}
Guido van Rossum1acceb01997-08-14 23:12:18 +0000549The pattern string from which the regex object was compiled.
Fred Drake76547c51998-04-03 05:59:05 +0000550\end{memberdesc}
Guido van Rossum1acceb01997-08-14 23:12:18 +0000551
Fred Drake42de1851998-04-20 16:28:44 +0000552
Fred Draked16d4981998-09-10 20:21:00 +0000553\subsection{Match Objects \label{match-objects}}
Fred Drake023f87f1998-01-12 19:16:24 +0000554
Fred Drake20e01961998-02-19 15:09:35 +0000555\class{MatchObject} instances support the following methods and attributes:
Guido van Rossum1acceb01997-08-14 23:12:18 +0000556
Fred Drake76547c51998-04-03 05:59:05 +0000557\begin{methoddesc}[MatchObject]{group}{\optional{group1, group2, ...}}
Guido van Rossum46503921998-01-19 23:14:17 +0000558Returns one or more subgroups of the match. If there is a single
559argument, the result is a single string; if there are
Guido van Rossum48d04371997-12-11 20:19:08 +0000560multiple arguments, the result is a tuple with one item per argument.
Guido van Rossum46503921998-01-19 23:14:17 +0000561Without arguments, \var{group1} defaults to zero (i.e. the whole match
562is returned).
563If a \var{groupN} argument is zero, the corresponding return value is the
Guido van Rossum48d04371997-12-11 20:19:08 +0000564entire matching string; if it is in the inclusive range [1..99], it is
Guido van Rossum791468f1998-04-03 20:07:37 +0000565the string matching the the corresponding parenthesized group. If a
566group number is negative or larger than the number of groups defined
567in the pattern, an \exception{IndexError} exception is raised.
568If a group is contained in a part of the pattern that did not match,
569the corresponding result is \code{None}. If a group is contained in a
570part of the pattern that matched multiple times, the last match is
571returned.
Guido van Rossum1acceb01997-08-14 23:12:18 +0000572
Andrew M. Kuchling25332811998-04-09 14:56:04 +0000573If the regular expression uses the \regexp{(?P<\var{name}>...)} syntax,
Guido van Rossum46503921998-01-19 23:14:17 +0000574the \var{groupN} arguments may also be strings identifying groups by
Guido van Rossum791468f1998-04-03 20:07:37 +0000575their group name. If a string argument is not used as a group name in
576the pattern, an \exception{IndexError} exception is raised.
Guido van Rossume4eb2231997-12-17 00:23:39 +0000577
578A moderately complicated example:
Fred Drake023f87f1998-01-12 19:16:24 +0000579
580\begin{verbatim}
Guido van Rossume4eb2231997-12-17 00:23:39 +0000581m = re.match(r"(?P<int>\d+)\.(\d*)", '3.14')
Fred Drake023f87f1998-01-12 19:16:24 +0000582\end{verbatim}
583
584After performing this match, \code{m.group(1)} is \code{'3'}, as is
Guido van Rossum46503921998-01-19 23:14:17 +0000585\code{m.group('int')}, and \code{m.group(2)} is \code{'14'}.
Fred Drake76547c51998-04-03 05:59:05 +0000586\end{methoddesc}
Guido van Rossum1acceb01997-08-14 23:12:18 +0000587
Guido van Rossum6c373f71998-06-29 22:48:01 +0000588\begin{methoddesc}[MatchObject]{groups}{\optional{default}}
Guido van Rossum48d04371997-12-11 20:19:08 +0000589Return a tuple containing all the subgroups of the match, from 1 up to
Guido van Rossum6c373f71998-06-29 22:48:01 +0000590however many groups are in the pattern. The \var{default} argument is
591used for groups that did not participate in the match; it defaults to
592\code{None}. (Incompatibility note: in the original Python 1.5
593release, if the tuple was one element long, a string would be returned
594instead. In later versions (from 1.5.1 on), a singleton tuple is
595returned in such cases.)
596\end{methoddesc}
597
598\begin{methoddesc}[MatchObject]{groupdict}{\optional{default}}
599Return a dictionary containing all the \emph{named} subgroups of the
600match, keyed by the subgroup name. The \var{default} argument is
601used for groups that did not participate in the match; it defaults to
602\code{None}.
Fred Drake76547c51998-04-03 05:59:05 +0000603\end{methoddesc}
Guido van Rossum48d04371997-12-11 20:19:08 +0000604
Fred Drake76547c51998-04-03 05:59:05 +0000605\begin{methoddesc}[MatchObject]{start}{\optional{group}}
Fred Drake013ad981998-03-08 07:38:27 +0000606\funcline{end}{\optional{group}}
Guido van Rossume4eb2231997-12-17 00:23:39 +0000607Return the indices of the start and end of the substring
Guido van Rossum46503921998-01-19 23:14:17 +0000608matched by \var{group}; \var{group} defaults to zero (meaning the whole
609matched substring).
610Return \code{None} if \var{group} exists but
Guido van Rossume4eb2231997-12-17 00:23:39 +0000611did not contribute to the match. For a match object
Fred Drake023f87f1998-01-12 19:16:24 +0000612\var{m}, and a group \var{g} that did contribute to the match, the
613substring matched by group \var{g} (equivalent to
614\code{\var{m}.group(\var{g})}) is
615
616\begin{verbatim}
617m.string[m.start(g):m.end(g)]
618\end{verbatim}
619
Guido van Rossume4eb2231997-12-17 00:23:39 +0000620Note that
621\code{m.start(\var{group})} will equal \code{m.end(\var{group})} if
Fred Drake023f87f1998-01-12 19:16:24 +0000622\var{group} matched a null string. For example, after \code{\var{m} =
623re.search('b(c?)', 'cba')}, \code{\var{m}.start(0)} is 1,
624\code{\var{m}.end(0)} is 2, \code{\var{m}.start(1)} and
625\code{\var{m}.end(1)} are both 2, and \code{\var{m}.start(2)} raises
Fred Drake20e01961998-02-19 15:09:35 +0000626an \exception{IndexError} exception.
Fred Drake76547c51998-04-03 05:59:05 +0000627\end{methoddesc}
Guido van Rossume4eb2231997-12-17 00:23:39 +0000628
Fred Drake76547c51998-04-03 05:59:05 +0000629\begin{methoddesc}[MatchObject]{span}{\optional{group}}
Fred Drake20e01961998-02-19 15:09:35 +0000630For \class{MatchObject} \var{m}, return the 2-tuple
Fred Drake023f87f1998-01-12 19:16:24 +0000631\code{(\var{m}.start(\var{group}), \var{m}.end(\var{group}))}.
Guido van Rossume4eb2231997-12-17 00:23:39 +0000632Note that if \var{group} did not contribute to the match, this is
Guido van Rossum46503921998-01-19 23:14:17 +0000633\code{(None, None)}. Again, \var{group} defaults to zero.
Fred Drake76547c51998-04-03 05:59:05 +0000634\end{methoddesc}
Guido van Rossume4eb2231997-12-17 00:23:39 +0000635
Fred Drake76547c51998-04-03 05:59:05 +0000636\begin{memberdesc}[MatchObject]{pos}
Guido van Rossum0b334101997-12-08 17:33:40 +0000637The value of \var{pos} which was passed to the
Fred Drake20e01961998-02-19 15:09:35 +0000638\function{search()} or \function{match()} function. This is the index into
Fred Drake023f87f1998-01-12 19:16:24 +0000639the string at which the regex engine started looking for a match.
Fred Drake76547c51998-04-03 05:59:05 +0000640\end{memberdesc}
Guido van Rossum0b334101997-12-08 17:33:40 +0000641
Fred Drake76547c51998-04-03 05:59:05 +0000642\begin{memberdesc}[MatchObject]{endpos}
Guido van Rossum0b334101997-12-08 17:33:40 +0000643The value of \var{endpos} which was passed to the
Fred Drake20e01961998-02-19 15:09:35 +0000644\function{search()} or \function{match()} function. This is the index into
Fred Drake023f87f1998-01-12 19:16:24 +0000645the string beyond which the regex engine will not go.
Fred Drake76547c51998-04-03 05:59:05 +0000646\end{memberdesc}
Guido van Rossum1acceb01997-08-14 23:12:18 +0000647
Fred Drake76547c51998-04-03 05:59:05 +0000648\begin{memberdesc}[MatchObject]{re}
Fred Drake20e01961998-02-19 15:09:35 +0000649The regular expression object whose \method{match()} or
650\method{search()} method produced this \class{MatchObject} instance.
Fred Drake76547c51998-04-03 05:59:05 +0000651\end{memberdesc}
Guido van Rossum1acceb01997-08-14 23:12:18 +0000652
Fred Drake76547c51998-04-03 05:59:05 +0000653\begin{memberdesc}[MatchObject]{string}
Fred Drake20e01961998-02-19 15:09:35 +0000654The string passed to \function{match()} or \function{search()}.
Fred Drake76547c51998-04-03 05:59:05 +0000655\end{memberdesc}
Guido van Rossum1acceb01997-08-14 23:12:18 +0000656
Guido van Rossum1acceb01997-08-14 23:12:18 +0000657\begin{seealso}
Fred Drakef9951811997-12-29 16:37:04 +0000658\seetext{Jeffrey Friedl, \emph{Mastering Regular Expressions},
Fred Drake023f87f1998-01-12 19:16:24 +0000659O'Reilly. The Python material in this book dates from before the
Fred Drake20e01961998-02-19 15:09:35 +0000660\module{re} module, but it covers writing good regular expression
Fred Drake023f87f1998-01-12 19:16:24 +0000661patterns in great detail.}
Guido van Rossum1acceb01997-08-14 23:12:18 +0000662\end{seealso}
Andrew M. Kuchling25332811998-04-09 14:56:04 +0000663