📄 match-objects.html
字号:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>4.2.5 Match Objects </title>
<META NAME="description" CONTENT="4.2.5 Match Objects ">
<META NAME="keywords" CONTENT="lib">
<META NAME="resource-type" CONTENT="document">
<META NAME="distribution" CONTENT="global">
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<link rel="STYLESHEET" href="lib.css" tppabs="http://www.python.org/doc/current/lib/lib.css">
<LINK REL="previous" href="re-objects.html" tppabs="http://www.python.org/doc/current/lib/re-objects.html">
<LINK REL="up" href="module-re.html" tppabs="http://www.python.org/doc/current/lib/module-re.html">
<LINK REL="next" href="module-struct.html" tppabs="http://www.python.org/doc/current/lib/module-struct.html">
</head>
<body>
<DIV CLASS="navigation"><table align="center" width="100%" cellpadding="0" cellspacing="2">
<tr>
<td><A href="re-objects.html" tppabs="http://www.python.org/doc/current/lib/re-objects.html"><img src="previous.gif" tppabs="http://www.python.org/doc/current/icons/previous.gif" border="0" height="32"
alt="Previous Page" width="32"></A></td>
<td><A href="module-re.html" tppabs="http://www.python.org/doc/current/lib/module-re.html"><img src="up.gif" tppabs="http://www.python.org/doc/current/icons/up.gif" border="0" height="32"
alt="Up One Level" width="32"></A></td>
<td><A href="module-struct.html" tppabs="http://www.python.org/doc/current/lib/module-struct.html"><img src="next.gif" tppabs="http://www.python.org/doc/current/icons/next.gif" border="0" height="32"
alt="Next Page" width="32"></A></td>
<td align="center" width="100%">Python Library Reference</td>
<td><A href="contents.html" tppabs="http://www.python.org/doc/current/lib/contents.html"><img src="contents.gif" tppabs="http://www.python.org/doc/current/icons/contents.gif" border="0" height="32"
alt="Contents" width="32"></A></td>
<td><a href="modindex.html" tppabs="http://www.python.org/doc/current/lib/modindex.html" title="Module Index"><img src="modules.gif" tppabs="http://www.python.org/doc/current/icons/modules.gif" border="0" height="32"
alt="Module Index" width="32"></a></td>
<td><A href="genindex.html" tppabs="http://www.python.org/doc/current/lib/genindex.html"><img src="index.gif" tppabs="http://www.python.org/doc/current/icons/index.gif" border="0" height="32"
alt="Index" width="32"></A></td>
</tr></table>
<b class="navlabel">Previous:</b> <a class="sectref" href="re-objects.html" tppabs="http://www.python.org/doc/current/lib/re-objects.html">4.2.4 Regular Expression Objects</A>
<b class="navlabel">Up:</b> <a class="sectref" href="module-re.html" tppabs="http://www.python.org/doc/current/lib/module-re.html">4.2 re </A>
<b class="navlabel">Next:</b> <a class="sectref" href="module-struct.html" tppabs="http://www.python.org/doc/current/lib/module-struct.html">4.3 struct </A>
<br><hr></DIV>
<!--End of Navigation Panel-->
<H2>
<BR>
4.2.5 Match Objects
</H2>
<P>
<tt class="class">MatchObject</tt> instances support the following methods and attributes:
<P>
<dl><dt><b><a name='l2h-620'><tt class='method'>expand</tt></a></b> (<var>template</var>)
<dd>
Return the string obtained by doing backslash substitution on the
template string <var>template</var>, as done by the <tt class="method">sub()</tt> method.
Escapes such as "<tt class="samp">\n</tt>" are converted to the appropriate
characters, and numeric backreferences ("<tt class="samp">\1</tt>", "<tt class="samp">\2</tt>") and named
backreferences ("<tt class="samp">\g<1></tt>", "<tt class="samp">\g<name></tt>") are replaced by the contents of the
corresponding group.
</dl>
<P>
<dl><dt><b><a name='l2h-621'><tt class='method'>group</tt></a></b> (<big>[</big><var>group1, ...</var><big>]</big>)
<dd>
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</var> defaults to zero (i.e. the whole match
is returned).
If a <var>groupN</var> 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 a
group number is negative or larger than the number of groups defined
in the pattern, an <tt class="exception">IndexError</tt> exception is raised.
If a group is contained in a part of the pattern that did not match,
the corresponding result is <code>-1</code>. If a group is contained in a
part of the pattern that matched multiple times, the last match is
returned.
<P>
If the regular expression uses the <tt class="regexp">(?P<<var>name</var>>...)</tt> syntax,
the <var>groupN</var> arguments may also be strings identifying groups by
their group name. If a string argument is not used as a group name in
the pattern, an <tt class="exception">IndexError</tt> exception is raised.
<P>
A moderately complicated example:
<P>
<dl><dd><pre class="verbatim">
m = re.match(r"(?P<int>\d+)\.(\d*)", '3.14')
</pre></dl>
<P>
After performing this match, <code>m.group(1)</code> is <code>'3'</code>, as is
<code>m.group('int')</code>, and <code>m.group(2)</code> is <code>'14'</code>.
</dl>
<P>
<dl><dt><b><a name='l2h-622'><tt class='method'>groups</tt></a></b> (<big>[</big><var>default</var><big>]</big>)
<dd>
Return a tuple containing all the subgroups of the match, from 1 up to
however many groups are in the pattern. The <var>default</var> argument is
used for groups that did not participate in the match; it defaults to
<code>None</code>. (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 (from 1.5.1 on), a singleton tuple is
returned in such cases.)
</dl>
<P>
<dl><dt><b><a name='l2h-623'><tt class='method'>groupdict</tt></a></b> (<big>[</big><var>default</var><big>]</big>)
<dd>
Return a dictionary containing all the <i>named</i> subgroups of the
match, keyed by the subgroup name. The <var>default</var> argument is
used for groups that did not participate in the match; it defaults to
<code>None</code>.
</dl>
<P>
<dl><dt><b><a name='l2h-624'><tt class='method'>start</tt></a></b> (<big>[</big><var>group</var><big>]</big>)
<dd>
<dt><b><tt class='function'>end</tt></b> (<var><big>[</big>group<big>]</big></var>)
<dd>
Return the indices of the start and end of the substring
matched by <var>group</var>; <var>group</var> defaults to zero (meaning the whole
matched substring).
Return <code>-1</code> if <var>group</var> exists but
did not contribute to the match. For a match object
<var>m</var>, and a group <var>g</var> that did contribute to the match, the
substring matched by group <var>g</var> (equivalent to
<code><var>m</var>.group(<var>g</var>)</code>) is
<P>
<dl><dd><pre class="verbatim">
m.string[m.start(g):m.end(g)]
</pre></dl>
<P>
Note that
<code>m.start(<var>group</var>)</code> will equal <code>m.end(<var>group</var>)</code> if
<var>group</var> matched a null string. For example, after <code><var>m</var> =
re.search('b(c?)', 'cba')</code>, <code><var>m</var>.start(0)</code> is 1,
<code><var>m</var>.end(0)</code> is 2, <code><var>m</var>.start(1)</code> and
<code><var>m</var>.end(1)</code> are both 2, and <code><var>m</var>.start(2)</code> raises
an <tt class="exception">IndexError</tt> exception.
</dl>
<P>
<dl><dt><b><a name='l2h-625'><tt class='method'>span</tt></a></b> (<big>[</big><var>group</var><big>]</big>)
<dd>
For <tt class="class">MatchObject</tt> <var>m</var>, return the 2-tuple
<code>(<var>m</var>.start(<var>group</var>), <var>m</var>.end(<var>group</var>))</code>.
Note that if <var>group</var> did not contribute to the match, this is
<code>(-1, -1)</code>. Again, <var>group</var> defaults to zero.
</dl>
<P>
<dl><dt><b><a name='l2h-626'><tt class='member'>pos</tt></a></b>
<dd>
The value of <var>pos</var> which was passed to the
<tt class="function">search()</tt> or <tt class="function">match()</tt> function. This is the index into
the string at which the regex engine started looking for a match.
</dl>
<P>
<dl><dt><b><a name='l2h-627'><tt class='member'>endpos</tt></a></b>
<dd>
The value of <var>endpos</var> which was passed to the
<tt class="function">search()</tt> or <tt class="function">match()</tt> function. This is the index into
the string beyond which the regex engine will not go.
</dl>
<P>
<dl><dt><b><a name='l2h-628'><tt class='member'>re</tt></a></b>
<dd>
The regular expression object whose <tt class="method">match()</tt> or
<tt class="method">search()</tt> method produced this <tt class="class">MatchObject</tt> instance.
</dl>
<P>
<dl><dt><b><a name='l2h-629'><tt class='member'>string</tt></a></b>
<dd>
The string passed to <tt class="function">match()</tt> or <tt class="function">search()</tt>.
</dl>
<P>
<div class='seealso'>
<p class='heading'><b>See Also:</b></p>
<div class="seetext"><p>Jeffrey Friedl, <em class='citetitle'
>Mastering Regular Expressions</em>,
O'Reilly. The Python material in this book dates from before the
<tt class="module">re</tt> module, but it covers writing good regular expression
patterns in great detail.</div>
</div>
<DIV CLASS="navigation"><p><hr><table align="center" width="100%" cellpadding="0" cellspacing="2">
<tr>
<td><A href="re-objects.html" tppabs="http://www.python.org/doc/current/lib/re-objects.html"><img src="previous.gif" tppabs="http://www.python.org/doc/current/icons/previous.gif" border="0" height="32"
alt="Previous Page" width="32"></A></td>
<td><A href="module-re.html" tppabs="http://www.python.org/doc/current/lib/module-re.html"><img src="up.gif" tppabs="http://www.python.org/doc/current/icons/up.gif" border="0" height="32"
alt="Up One Level" width="32"></A></td>
<td><A href="module-struct.html" tppabs="http://www.python.org/doc/current/lib/module-struct.html"><img src="next.gif" tppabs="http://www.python.org/doc/current/icons/next.gif" border="0" height="32"
alt="Next Page" width="32"></A></td>
<td align="center" width="100%">Python Library Reference</td>
<td><A href="contents.html" tppabs="http://www.python.org/doc/current/lib/contents.html"><img src="contents.gif" tppabs="http://www.python.org/doc/current/icons/contents.gif" border="0" height="32"
alt="Contents" width="32"></A></td>
<td><a href="modindex.html" tppabs="http://www.python.org/doc/current/lib/modindex.html" title="Module Index"><img src="modules.gif" tppabs="http://www.python.org/doc/current/icons/modules.gif" border="0" height="32"
alt="Module Index" width="32"></a></td>
<td><A href="genindex.html" tppabs="http://www.python.org/doc/current/lib/genindex.html"><img src="index.gif" tppabs="http://www.python.org/doc/current/icons/index.gif" border="0" height="32"
alt="Index" width="32"></A></td>
</tr></table>
<b class="navlabel">Previous:</b> <a class="sectref" href="re-objects.html" tppabs="http://www.python.org/doc/current/lib/re-objects.html">4.2.4 Regular Expression Objects</A>
<b class="navlabel">Up:</b> <a class="sectref" href="module-re.html" tppabs="http://www.python.org/doc/current/lib/module-re.html">4.2 re </A>
<b class="navlabel">Next:</b> <a class="sectref" href="module-struct.html" tppabs="http://www.python.org/doc/current/lib/module-struct.html">4.3 struct </A>
</DIV>
<!--End of Navigation Panel-->
<ADDRESS>
<hr>See <i><a href="about.html" tppabs="http://www.python.org/doc/current/lib/about.html">About this document...</a></i> for information on suggesting changes.
</ADDRESS>
</BODY>
</HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -