blob: 142fd59bd0258f9402b1b5b87e70b21913d398fb [file] [log] [blame]
Guido van Rossum1acceb01997-08-14 23:12:18 +00001\section{Built-in Module \sectcode{re}}
2\label{module-re}
3
4\bimodindex{re}
5
6% XXX Remove before 1.5final release.
7{\large\bf The \code{re} module is still in the process of being
8developed, and more features will be added in future 1.5 alphas and
9betas. This documentation is also preliminary and incomplete. If you
10find a bug or documentation error, or just find something unclear,
11please send a message to
12\code{string-sig@python.org}, and we'll fix it.}
13
14This module provides regular expression matching operations similar to
Guido van Rossum0b334101997-12-08 17:33:40 +000015those found in Perl. It's 8-bit clean: both patterns and strings may
16contain null bytes and characters whose high bit is set. It is always
17available.
Guido van Rossum1acceb01997-08-14 23:12:18 +000018
19Regular expressions use the backslash character (\code{\e}) to
20indicate 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
Guido van Rossum0b334101997-12-08 17:33:40 +000024\code{\e\e\e\e} as the pattern string, because the regular expression
25must be \code{\e\e}, and each backslash must be expressed as
26\code{\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
30a string literal prefixed with 'r'. So \code{r"\e n"} is a two
31character string containing a backslash and the letter 'n', while
32\code{"\e n"} is a one-character string containing a newline. Usually
33patterns will be expressed in Python code using this raw string notation.
34
35% XXX Can the following section be dropped, or should it be boiled down?
36
37%\strong{Please note:} There is a little-known fact about Python string
38%literals which means that you don't usually have to worry about
39%doubling backslashes, even though they are used to escape special
40%characters in string literals as well as in regular expressions. This
41%is because Python doesn't remove backslashes from string literals if
42%they are followed by an unrecognized escape character.
43%\emph{However}, if you want to include a literal \dfn{backslash} in a
44%regular expression represented as a string literal, you have to
45%\emph{quadruple} it or enclose it in a singleton character class.
46%E.g.\ to extract \LaTeX\ \code{\e section\{{\rm
47%\ldots}\}} headers from a document, you can use this pattern:
48%\code{'[\e ] section\{\e (.*\e )\}'}. \emph{Another exception:}
49%the escape sequence \code{\e b} is significant in string literals
50%(where it means the ASCII bell character) as well as in Emacs regular
51%expressions (where it stands for a word boundary), so in order to
52%search for a word boundary, you should use the pattern \code{'\e \e b'}.
53%Similarly, a backslash followed by a digit 0-7 should be doubled to
54%avoid interpretation as an octal escape.
55
56\subsection{Regular Expressions}
57
58A regular expression (or RE) specifies a set of strings that matches
59it; the functions in this module let you check if a particular string
60matches a given regular expression (or if a given regular expression
61matches a particular string, which comes down to the same thing).
62
63Regular expressions can be concatenated to form new regular
64expressions; if \emph{A} and \emph{B} are both regular expressions,
65then \emph{AB} is also an regular expression. If a string \emph{p}
66matches A and another string \emph{q} matches B, the string \emph{pq}
67will match AB. Thus, complex expressions can easily be constructed
68from simpler primitive expressions like the ones described here. For
69details of the theory and implementation of regular expressions,
70consult the Friedl book referenced below, or almost any textbook about
71compiler construction.
72
Guido van Rossum0b334101997-12-08 17:33:40 +000073A brief explanation of the format of regular expressions follows.
74%For further information and a gentler presentation, consult XXX somewhere.
Guido van Rossum1acceb01997-08-14 23:12:18 +000075
76Regular expressions can contain both special and ordinary characters.
77Most ordinary characters, like '\code{A}', '\code{a}', or '\code{0}',
78are the simplest regular expressions; they simply match themselves.
79You can concatenate ordinary characters, so '\code{last}' matches the
80characters 'last'. (In the rest of this section, we'll write RE's in
81\code{this special font}, usually without quotes, and strings to be
82matched 'in single quotes'.)
83
84Some characters, like \code{|} or \code{(}, are special. Special
85characters either stand for classes of ordinary characters, or affect
86how the regular expressions around them are interpreted.
87
88The special characters are:
89\begin{itemize}
90\item[\code{.}] (Dot.) In the default mode, this matches any
91character except a newline. If the \code{DOTALL} flag has been
92specified, this matches any character including a newline.
93\item[\code{\^}] (Caret.) Matches the start of the string, and in
94\code{MULTILINE} mode also immediately after each newline.
95\item[\code{\$}] Matches the end of the string.
96\code{foo} matches both 'foo' and 'foobar', while the regular
97expression '\code{foo\$}' matches only 'foo'.
98%
99\item[\code{*}] Causes the resulting RE to
100match 0 or more repetitions of the preceding RE, as many repetitions
101as are possible. \code{ab*} will
102match 'a', 'ab', or 'a' followed by any number of 'b's.
103%
104\item[\code{+}] Causes the
105resulting RE to match 1 or more repetitions of the preceding RE.
106\code{ab+} will match 'a' followed by any non-zero number of 'b's; it
107will not match just 'a'.
108%
109\item[\code{?}] Causes the resulting RE to
110match 0 or 1 repetitions of the preceding RE. \code{ab?} will
111match either 'a' or 'ab'.
112\item[\code{*?}, \code{+?}, \code{??}] The \code{*}, \code{+}, and
113\code{?} qualifiers are all \dfn{greedy}; they match as much text as
114possible. Sometimes this behaviour isn't desired; if the RE
115\code{<.*>} is matched against \code{<H1>title</H1>}, it will match the
116entire string, and not just \code{<H1>}.
117Adding \code{?} after the qualifier makes it perform the match in
118\dfn{non-greedy} or \dfn{minimal} fashion; as few characters as
119possible will be matched. Using \code{.*?} in the previous
Guido van Rossum0b334101997-12-08 17:33:40 +0000120expression will match only \code{<H1>}.
Guido van Rossum1acceb01997-08-14 23:12:18 +0000121%
122\item[\code{\e}] Either escapes special characters (permitting you to match
123characters like '*?+\&\$'), or signals a special sequence; special
124sequences are discussed below.
125
126If you're not using a raw string to
127express the pattern, remember that Python also uses the
128backslash as an escape sequence in string literals; if the escape
129sequence isn't recognized by Python's parser, the backslash and
130subsequent character are included in the resulting string. However,
131if Python would recognize the resulting sequence, the backslash should
132be repeated twice. This is complicated and hard to understand, so
133it's highly recommended that you use raw strings.
134%
135\item[\code{[]}] Used to indicate a set of characters. Characters can
136be listed individually, or a range is indicated by giving two
137characters and separating them by a '-'. Special characters are not
138active inside sets. For example, \code{[akm\$]} will match any of the
139characters 'a', 'k', 'm', or '\$'; \code{[a-z]} will match any
140lowercase letter and \code{[a-zA-Z0-9]} matches any letter or digit.
Guido van Rossum0b334101997-12-08 17:33:40 +0000141Character classes of the form \code{\e \var{X}} defined below are also acceptable.
Guido van Rossum1acceb01997-08-14 23:12:18 +0000142If you want to include a \code{]} or a \code{-} inside a
143set, precede it with a backslash.
144
145Characters \emph{not} within a range can be matched by including a
146\code{\^} as the first character of the set; \code{\^} elsewhere will
147simply match the '\code{\^}' character.
148%
149\item[\code{|}]\code{A|B}, where A and B can be arbitrary REs,
150creates a regular expression that will match either A or B. This can
151be used inside groups (see below) as well. To match a literal '|',
152use \code{\e|}, or enclose it inside a character class, like \code{[|]}.
153%
Guido van Rossum0b334101997-12-08 17:33:40 +0000154\item[\code{(...)}] Matches whatever regular expression is inside the parentheses, and indicates the start and end of a group; the
Guido van Rossum1acceb01997-08-14 23:12:18 +0000155contents of a group can be retrieved after a match has been performed,
156and can be matched later in the string with the
157\code{\e \var{number}} special sequence, described below. To match the
158literals '(' or ')',
159use \code{\e(} or \code{\e)}, or enclose them inside a character
160class: \code{[(] [)]}.
161%
Guido van Rossum0b334101997-12-08 17:33:40 +0000162\item[\code{(?...)}] This is an extension notation (a '?' following a
163'(' is not meaningful otherwise). The first character after the '?'
164determines what the meaning and further syntax of the construct is.
165Following are the currently supported extensions.
166%
167\item[\code{(?ilmsx)}] (One or more letters from the set 'i', 'l', 'm', 's',
168'x'.) The group matches the empty string; the letters set the
169corresponding flags (re.I, re.L, re.M, re.S, re.X) for the entire regular
170expression. This is useful if you wish include the flags as part of the regular
171expression, instead of passing a \var{flag} argument to the \code{compile} function.
172%
Guido van Rossum1acceb01997-08-14 23:12:18 +0000173\item[\code{(?:...)}] A non-grouping version of regular parentheses.
174Matches whatever's inside the parentheses, but the text matched by the
175group \emph{cannot} be retrieved after performing a match or
176referenced later in the pattern.
177%
178\item[\code{(?P<\var{name}>...)}] Similar to regular parentheses, but
179the text matched by the group is accessible via the symbolic group
180name \var{name}. Group names must be valid Python identifiers. A
181symbolic group is also a numbered group, just as if the group were not
182named. So the group named 'id' in the example above can also be
183referenced as the numbered group 1.
184
185For example, if the pattern string is
186\code{r'(?P<id>[a-zA-Z_]\e w*)'}, the group can be referenced by its
187name in arguments to methods of match objects, such as \code{m.group('id')}
188or \code{m.end('id')}, and also by name in pattern text (e.g. \code{(?P=id)}) and
189replacement text (e.g. \code{\e g<id>}).
190%
191\item[\code{(?\#...)}] A comment; the contents of the parentheses are simply ignored.
192%
Guido van Rossum0b334101997-12-08 17:33:40 +0000193\item[\code{(?=...)}] Matches if \code{...} matches next, but doesn't consume any of the string. This is called a lookahead assertion. For example,
194\code{Isaac (?=Asimov)} will match 'Isaac~' only if it's followed by 'Asimov'.
Guido van Rossum1acceb01997-08-14 23:12:18 +0000195%
Guido van Rossum0b334101997-12-08 17:33:40 +0000196\item[\code{(?!...)}] Matches if \code{...} doesn't match next. This is a negative lookahead assertion. For example,
197For example,
198\code{Isaac (?!Asimov)} will match 'Isaac~' only if it's \emph{not} followed by 'Asimov'.
199
Guido van Rossum1acceb01997-08-14 23:12:18 +0000200\end{itemize}
201
202The special sequences consist of '\code{\e}' and a character from the
203list below. If the ordinary character is not on the list, then the
204resulting RE will match the second character. For example,
205\code{\e\$} matches the character '\$'. Ones where the backslash
206should be doubled are indicated.
207
208\begin{itemize}
209
210%
211\item[\code{\e \var{number}}] Matches the contents of the group of the
Guido van Rossum0b334101997-12-08 17:33:40 +0000212same number. Groups are numbered starting from 1. For example,
213\code{(.+) \e 1} matches 'the the' or '55 55', but not 'the end' (note
214the space after the group). This special sequence can only be used to
215match one of the first 99 groups. If the first digit of \var{number}
216is 0, or \var{number} is 3 octal digits long, it will not be interpreted
217as a group match, but as the character with octal value \var{number}.
Guido van Rossum1acceb01997-08-14 23:12:18 +0000218%
219\item[\code{\e A}] Matches only at the start of the string.
220%
221\item[\code{\e b}] Matches the empty string, but only at the
222beginning or end of a word. A word is defined as a sequence of
223alphanumeric characters, so the end of a word is indicated by
Guido van Rossum0b334101997-12-08 17:33:40 +0000224whitespace or a non-alphanumeric character.
Guido van Rossum1acceb01997-08-14 23:12:18 +0000225%
Guido van Rossum0b334101997-12-08 17:33:40 +0000226\item[\code{\e B}] Matches the empty string, but only when it is
227\emph{not} at the beginning or end of a word.
Guido van Rossum1acceb01997-08-14 23:12:18 +0000228%
229\item[\code{\e d}]Matches any decimal digit; this is
230equivalent to the set \code{[0-9]}.
231%
232\item[\code{\e D}]Matches any non-digit character; this is
Guido van Rossumd7dc2eb1997-10-22 03:03:44 +0000233equivalent to the set \code{[{\^}0-9]}.
Guido van Rossum1acceb01997-08-14 23:12:18 +0000234%
235\item[\code{\e s}]Matches any whitespace character; this is
236equivalent to the set \code{[ \e t\e n\e r\e f\e v]}.
237%
238\item[\code{\e S}]Matches any non-whitespace character; this is
Guido van Rossumd7dc2eb1997-10-22 03:03:44 +0000239equivalent to the set \code{[{\^} \e t\e n\e r\e f\e v]}.
Guido van Rossum1acceb01997-08-14 23:12:18 +0000240%
Guido van Rossum0b334101997-12-08 17:33:40 +0000241\item[\code{\e w}]When the LOCALE flag is not specified, matches any alphanumeric character; this is
242equivalent to the set \code{[a-zA-Z0-9_]}. With LOCALE, it will match
243the set \code{[0-9_]} plus whatever characters are defined as letters
244for the current locale.
Guido van Rossum1acceb01997-08-14 23:12:18 +0000245%
Guido van Rossum0b334101997-12-08 17:33:40 +0000246\item[\code{\e W}]When the LOCALE flag is not specified, matches any
247non-alphanumeric character; this is equivalent to the set
248\code{[{\^}a-zA-Z0-9_]}. With LOCALE, it will match any character
249not in the set \code{[0-9_]}, and not defined as a letter
250for the current locale.
Guido van Rossum1acceb01997-08-14 23:12:18 +0000251
252\item[\code{\e Z}]Matches only at the end of the string.
253%
254
255\item[\code{\e \e}] Matches a literal backslash.
256
257\end{itemize}
258
259\subsection{Module Contents}
260
261The module defines the following functions and constants, and an exception:
262
263\renewcommand{\indexsubitem}{(in module re)}
264
265\begin{funcdesc}{compile}{pattern\optional{\, flags}}
266 Compile a regular expression pattern into a regular expression
267 object, which can be used for matching using its \code{match} and
268 \code{search} methods, described below.
269
Guido van Rossum0b334101997-12-08 17:33:40 +0000270 The expression's behaviour can be modified by specifying a
271 \var{flags} value. Values can be any of the following variables,
272 combined using bitwise OR (the \code{|} operator).
273
274\begin{tableii}{|l|l|}{code}{Flag}{Meaning}
275
276\lineii{I or IGNORECASE}{Perform case-insensitive matching;
277 expressions like [A-Z] will match lowercase letters, too.}
278
279\lineii{L or LOCALE}{Make \code{\e w}, \code{\e W}, \code{\e b},
280\code{\e B}, dependent on the current locale.
281}
282
283\lineii{M or MULTILINE}{When specified, the pattern character \code{\^}
284 matches at the beginning of the string and at the beginning of each
285line (immediately following each newline); and the pattern character
286\code{\$} matches at the end of the string and at the end of each line
287(immediately preceding each newline).
288By default, \code{\^} matches only at the beginning of the string, and
289\code{\$} only at the end of the string and immediately before the
290newline (if any) at the end of the string.
291}
292
293\lineii{S or DOTALL}{Make the \code{.} special character match a newline; without this flag, \code{.} will match anything \emph{except} a newline.}
294
295\lineii{X or VERBOSE}{When specified, whitespace within the pattern
296string is ignored except when in a character class or preceded by an
297unescaped backslash, and, when a line contains a \code{\#} not in a
298character class or preceded by an unescaped backslash, all characters
299from the leftmost such \code{\#} through the end of the line are
300ignored.
301}
302
303\end{tableii}
304
Guido van Rossum1acceb01997-08-14 23:12:18 +0000305 The sequence
306%
307\bcode\begin{verbatim}
308prog = re.compile(pat)
309result = prog.match(str)
310\end{verbatim}\ecode
311%
312is equivalent to
313%
314\bcode\begin{verbatim}
315result = re.match(pat, str)
316\end{verbatim}\ecode
317%
318but the version using \code{compile()} is more efficient when multiple
319regular expressions are used concurrently in a single program.
320%(The compiled version of the last pattern passed to \code{regex.match()} or
321%\code{regex.search()} is cached, so programs that use only a single
322%regular expression at a time needn't worry about compiling regular
323%expressions.)
324\end{funcdesc}
325
326\begin{funcdesc}{escape}{string}
327Return \var{string} with all non-alphanumerics backslashed; this is
328useful if you want to match some variable string which may have
329regular expression metacharacters in it.
330\end{funcdesc}
331
332\begin{funcdesc}{match}{pattern\, string\optional{\, flags}}
333 If zero or more characters at the beginning of \var{string} match
334 the regular expression \var{pattern}, return a corresponding
335 \code{Match} object. Return \code{None} if the string does not
336 match the pattern; note that this is different from a zero-length
337 match.
338\end{funcdesc}
339
340\begin{funcdesc}{search}{pattern\, string\optional{\, flags}}
341 Scan through \var{string} looking for a location where the regular
342 expression \var{pattern} produces a match. Return \code{None} if no
343 position in the string matches the pattern; note that this is
344 different from finding a zero-length match at some point in the string.
345\end{funcdesc}
346
347\begin{funcdesc}{split}{pattern\, string\, \optional{, maxsplit=0}}
348 Split \var{string} by the occurrences of \var{pattern}. If
349 capturing parentheses are used in pattern, then occurrences of
350 patterns or subpatterns are also returned.
351%
352\bcode\begin{verbatim}
353>>> re.split('[\W]+', 'Words, words, words.')
354['Words', 'words', 'words', '']
355>>> re.split('([\W]+)', 'Words, words, words.')
356['Words', ', ', 'words', ', ', 'words', '.', '']
357\end{verbatim}\ecode
358%
359 This function combines and extends the functionality of
Guido van Rossum0b334101997-12-08 17:33:40 +0000360 the old \code{regex.split()} and \code{regex.splitx()}.
Guido van Rossum1acceb01997-08-14 23:12:18 +0000361\end{funcdesc}
362
363\begin{funcdesc}{sub}{pattern\, repl\, string\optional{, count=0}}
364Return the string obtained by replacing the leftmost non-overlapping
365occurrences of \var{pattern} in \var{string} by the replacement
Barry Warsaw4552f3d1997-11-20 00:15:13 +0000366\var{repl}. If the pattern isn't found, \var{string} is returned
367unchanged. \var{repl} can be a string or a function; if a function,
368it is called for every non-overlapping occurance of \var{pattern}.
Guido van Rossum0b334101997-12-08 17:33:40 +0000369The function takes a single match object argument, and returns the
370replacement string. For example:
Barry Warsaw4552f3d1997-11-20 00:15:13 +0000371%
372\bcode\begin{verbatim}
373>>> def dashrepl(matchobj):
374... if matchobj.group(0) == '-': return ' '
375... else: return '-'
376>>> re.sub('-{1,2}', dashrepl, 'pro----gram-files')
377'pro--gram files'
378\end{verbatim}\ecode
379%
Guido van Rossum0b334101997-12-08 17:33:40 +0000380The pattern may be a string or a
381regexp object; if you need to specify
382regular expression flags, you must use a regexp object, or use
383embedded modifiers in a pattern string; e.g.
Guido van Rossum1acceb01997-08-14 23:12:18 +0000384%
385\bcode\begin{verbatim}
386sub("(?i)b+", "x", "bbbb BBBB") returns 'x x'.
387\end{verbatim}\ecode
388%
389The optional argument \var{count} is the maximum number of pattern
390occurrences to be replaced; count must be a non-negative integer, and
391the default value of 0 means to replace all occurrences.
392
393Empty matches for the pattern are replaced only when not adjacent to a
394previous match, so \code{sub('x*', '-', 'abc')} returns '-a-b-c-'.
395\end{funcdesc}
396
397\begin{funcdesc}{subn}{pattern\, repl\, string\optional{, count=0}}
398Perform the same operation as \code{sub()}, but return a tuple
399\code{(new_string, number_of_subs_made)}.
400\end{funcdesc}
401
402\begin{excdesc}{error}
403 Exception raised when a string passed to one of the functions here
404 is not a valid regular expression (e.g., unmatched parentheses) or
405 when some other error occurs during compilation or matching. (It is
406 never an error if a string contains no match for a pattern.)
407\end{excdesc}
408
409\subsection{Regular Expression Objects}
410Compiled regular expression objects support the following methods and
411attributes:
412
Guido van Rossumeb53ae41997-10-05 18:54:07 +0000413\renewcommand{\indexsubitem}{(re method)}
Guido van Rossum0b334101997-12-08 17:33:40 +0000414\begin{funcdesc}{match}{string\optional{\, pos}\optional{\, endpos}}
Guido van Rossumeb53ae41997-10-05 18:54:07 +0000415 If zero or more characters at the beginning of \var{string} match
416 this regular expression, return a corresponding
417 \code{Match} object. Return \code{None} if the string does not
418 match the pattern; note that this is different from a zero-length
419 match.
Guido van Rossum1acceb01997-08-14 23:12:18 +0000420
421 The optional second parameter \var{pos} gives an index in the string
422 where the search is to start; it defaults to \code{0}. This is not
423 completely equivalent to slicing the string; the \code{'\^'} pattern
424 character matches at the real begin of the string and at positions
425 just after a newline, not necessarily at the index where the search
426 is to start.
Guido van Rossum0b334101997-12-08 17:33:40 +0000427
428 The optional parameter \var{endpos} limits how far the string will
429 be searched; it will be as if the string is \var{endpos} characters
430 long, so only the characters from \var{pos} to \var{endpos} will be
431 searched for a match.
Guido van Rossum1acceb01997-08-14 23:12:18 +0000432\end{funcdesc}
433
Guido van Rossum0b334101997-12-08 17:33:40 +0000434\begin{funcdesc}{search}{string\optional{\, pos}\optional{\, endpos}}
Guido van Rossumeb53ae41997-10-05 18:54:07 +0000435 Scan through \var{string} looking for a location where this regular
436 expression produces a match. Return \code{None} if no
437 position in the string matches the pattern; note that this is
438 different from finding a zero-length match at some point in the string.
Guido van Rossum1acceb01997-08-14 23:12:18 +0000439
Guido van Rossum0b334101997-12-08 17:33:40 +0000440 The optional \var{pos} and \var{endpos} parameters have the same meaning as for the
Guido van Rossum1acceb01997-08-14 23:12:18 +0000441 \code{match} method.
442\end{funcdesc}
443
444\begin{funcdesc}{split}{string\, \optional{, maxsplit=0}}
445Identical to the \code{split} function, using the compiled pattern.
446\end{funcdesc}
447
448\begin{funcdesc}{sub}{repl\, string\optional{, count=0}}
449Identical to the \code{sub} function, using the compiled pattern.
450\end{funcdesc}
451
452\begin{funcdesc}{subn}{repl\, string\optional{, count=0}}
453Identical to the \code{subn} function, using the compiled pattern.
454\end{funcdesc}
455
456\renewcommand{\indexsubitem}{(regex attribute)}
457
458\begin{datadesc}{flags}
459The flags argument used when the regex object was compiled, or 0 if no
460flags were provided.
461\end{datadesc}
462
463\begin{datadesc}{groupindex}
464A dictionary mapping any symbolic group names (defined by
465\code{?P<\var{id}>}) to group numbers. The dictionary is empty if no
466symbolic groups were used in the pattern.
467\end{datadesc}
468
469\begin{datadesc}{pattern}
470The pattern string from which the regex object was compiled.
471\end{datadesc}
472
473\subsection{Match Objects}
474Match objects support the following methods and attributes:
475
Guido van Rossum1acceb01997-08-14 23:12:18 +0000476\begin{funcdesc}{start}{group}
477\end{funcdesc}
478
479\begin{funcdesc}{end}{group}
Guido van Rossum0b334101997-12-08 17:33:40 +0000480Return the indices of the start and end of the substring
481matched by \var{group}. Return \code{None} if \var{group} exists but
482did not contribute to the match. Note that for a match object
483\code{m}, and a group \code{g} that did contribute to the match, the
484substring matched by group \code{g} is
Guido van Rossum1acceb01997-08-14 23:12:18 +0000485\bcode\begin{verbatim}
486 m.string[m.start(g):m.end(g)]
487\end{verbatim}\ecode
488%
489Note too that \code{m.start(\var{group})} will equal
490\code{m.end(\var{group})} if \var{group} matched a null string. For example,
491after \code{m = re.search('b(c?)', 'cba')}, \code{m.start(0)} is 1,
492\code{m.end(0)} is 2, \code{m.start(1)} and \code{m.end(1)} are both
4932, and \code{m.start(2)} raises an
494\code{IndexError} exception.
495\end{funcdesc}
496
Guido van Rossum0b334101997-12-08 17:33:40 +0000497\begin{funcdesc}{span}{group}
498Return the 2-tuple \code{(start(\var{group}), end(\var{group}))}.
499Note that if \var{group} did not contribute to the match, this is
500\code{(None, None)}.
501\end{funcdesc}
502
Guido van Rossum1acceb01997-08-14 23:12:18 +0000503\begin{funcdesc}{group}{\optional{g1, g2, ...})}
504This method is only valid when the last call to the \code{match}
505or \code{search} method found a match. It returns one or more
506groups of the match. If there is a single \var{index} argument,
507the result is a single string; if there are multiple arguments, the
508result is a tuple with one item per argument. If the \var{index} is
509zero, the corresponding return value is the entire matching string; if
510it is in the inclusive range [1..99], it is the string matching the
511the corresponding parenthesized group (using the default syntax,
512groups are parenthesized using \code{\e (} and \code{\e )}). If no
513such group exists, the corresponding result is \code{None}.
514
Guido van Rossum0b334101997-12-08 17:33:40 +0000515If the regular expression uses the \code{(?P<\var{name}>...)} syntax,
516the \var{index} arguments may also be strings identifying groups by
517their group name.
Guido van Rossum1acceb01997-08-14 23:12:18 +0000518\end{funcdesc}
519
520\begin{datadesc}{pos}
Guido van Rossum0b334101997-12-08 17:33:40 +0000521The value of \var{pos} which was passed to the
522\code{search} or \code{match} function. This is the index into the
523string at which the regex engine started looking for a match.
524\end{datadesc}
525
526\begin{datadesc}{endpos}
527The value of \var{endpos} which was passed to the
528\code{search} or \code{match} function. This is the index into the
529string beyond which the regex engine will not go.
Guido van Rossum1acceb01997-08-14 23:12:18 +0000530\end{datadesc}
531
532\begin{datadesc}{re}
533The regular expression object whose match() or search() method
Guido van Rossum0b334101997-12-08 17:33:40 +0000534produced this match object.
Guido van Rossum1acceb01997-08-14 23:12:18 +0000535\end{datadesc}
536
537\begin{datadesc}{string}
538The string passed to \code{match()} or \code{search()}.
539\end{datadesc}
540
Guido van Rossum1acceb01997-08-14 23:12:18 +0000541\begin{seealso}
542\seetext Jeffrey Friedl, \emph{Mastering Regular Expressions}.
543\end{seealso}
544