Initial commit of OpenSPARC T2 architecture model.
[OpenSPARC-T2-SAM] / sam-t2 / devtools / v8plus / html / python / lib / match-objects.html
CommitLineData
920dae64
AT
1<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
2<html>
3<head>
4<link rel="STYLESHEET" href="lib.css" type='text/css' />
5<link rel="SHORTCUT ICON" href="../icons/pyfav.png" type="image/png" />
6<link rel='start' href='../index.html' title='Python Documentation Index' />
7<link rel="first" href="lib.html" title='Python Library Reference' />
8<link rel='contents' href='contents.html' title="Contents" />
9<link rel='index' href='genindex.html' title='Index' />
10<link rel='last' href='about.html' title='About this document...' />
11<link rel='help' href='about.html' title='About this document...' />
12<link rel="next" href="node118.html" />
13<link rel="prev" href="re-objects.html" />
14<link rel="parent" href="module-re.html" />
15<link rel="next" href="node118.html" />
16<meta name='aesop' content='information' />
17<title>4.2.5 Match Objects </title>
18</head>
19<body>
20<DIV CLASS="navigation">
21<div id='top-navigation-panel' xml:id='top-navigation-panel'>
22<table align="center" width="100%" cellpadding="0" cellspacing="2">
23<tr>
24<td class='online-navigation'><a rel="prev" title="4.2.4 Regular Expression Objects"
25 href="re-objects.html"><img src='../icons/previous.png'
26 border='0' height='32' alt='Previous Page' width='32' /></A></td>
27<td class='online-navigation'><a rel="parent" title="4.2 re "
28 href="module-re.html"><img src='../icons/up.png'
29 border='0' height='32' alt='Up One Level' width='32' /></A></td>
30<td class='online-navigation'><a rel="next" title="4.2.6 Examples"
31 href="node118.html"><img src='../icons/next.png'
32 border='0' height='32' alt='Next Page' width='32' /></A></td>
33<td align="center" width="100%">Python Library Reference</td>
34<td class='online-navigation'><a rel="contents" title="Table of Contents"
35 href="contents.html"><img src='../icons/contents.png'
36 border='0' height='32' alt='Contents' width='32' /></A></td>
37<td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png'
38 border='0' height='32' alt='Module Index' width='32' /></a></td>
39<td class='online-navigation'><a rel="index" title="Index"
40 href="genindex.html"><img src='../icons/index.png'
41 border='0' height='32' alt='Index' width='32' /></A></td>
42</tr></table>
43<div class='online-navigation'>
44<b class="navlabel">Previous:</b>
45<a class="sectref" rel="prev" href="re-objects.html">4.2.4 Regular Expression Objects</A>
46<b class="navlabel">Up:</b>
47<a class="sectref" rel="parent" href="module-re.html">4.2 re </A>
48<b class="navlabel">Next:</b>
49<a class="sectref" rel="next" href="node118.html">4.2.6 Examples</A>
50</div>
51<hr /></div>
52</DIV>
53<!--End of Navigation Panel-->
54
55<H2><A NAME="SECTION006250000000000000000"></A><A NAME="match-objects"></A>
56<BR>
574.2.5 Match Objects
58</H2>
59
60<P>
61<tt class="class">MatchObject</tt> instances support the following methods and
62attributes:
63
64<P>
65<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
66 <td><nobr><b><tt id='l2h-901' xml:id='l2h-901' class="method">expand</tt></b>(</nobr></td>
67 <td><var>template</var>)</td></tr></table></dt>
68<dd>
69 Return the string obtained by doing backslash substitution on the
70template string <var>template</var>, as done by the <tt class="method">sub()</tt> method.
71Escapes such as "<tt class="samp">&#92;n</tt>" are converted to the appropriate
72characters, and numeric backreferences ("<tt class="samp">&#92;1</tt>", "<tt class="samp">&#92;2</tt>") and
73named backreferences ("<tt class="samp">&#92;g&lt;1&gt;</tt>", "<tt class="samp">&#92;g&lt;name&gt;</tt>") are replaced
74by the contents of the corresponding group.
75</dl>
76
77<P>
78<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
79 <td><nobr><b><tt id='l2h-902' xml:id='l2h-902' class="method">group</tt></b>(</nobr></td>
80 <td><var></var><big>[</big><var>group1, ...</var><big>]</big><var></var>)</td></tr></table></dt>
81<dd>
82Returns one or more subgroups of the match. If there is a single
83argument, the result is a single string; if there are
84multiple arguments, the result is a tuple with one item per argument.
85Without arguments, <var>group1</var> defaults to zero (the whole match
86is returned).
87If a <var>groupN</var> argument is zero, the corresponding return value is the
88entire matching string; if it is in the inclusive range [1..99], it is
89the string matching the corresponding parenthesized group. If a
90group number is negative or larger than the number of groups defined
91in the pattern, an <tt class="exception">IndexError</tt> exception is raised.
92If a group is contained in a part of the pattern that did not match,
93the corresponding result is <code>None</code>. If a group is contained in a
94part of the pattern that matched multiple times, the last match is
95returned.
96
97<P>
98If the regular expression uses the <tt class="regexp">(?P&lt;<var>name</var>&gt;...)</tt> syntax,
99the <var>groupN</var> arguments may also be strings identifying groups by
100their group name. If a string argument is not used as a group name in
101the pattern, an <tt class="exception">IndexError</tt> exception is raised.
102
103<P>
104A moderately complicated example:
105
106<P>
107<div class="verbatim"><pre>
108m = re.match(r"(?P&lt;int&gt;\d+)\.(\d*)", '3.14')
109</pre></div>
110
111<P>
112After performing this match, <code>m.group(1)</code> is <code>'3'</code>, as is
113<code>m.group('int')</code>, and <code>m.group(2)</code> is <code>'14'</code>.
114</dl>
115
116<P>
117<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
118 <td><nobr><b><tt id='l2h-903' xml:id='l2h-903' class="method">groups</tt></b>(</nobr></td>
119 <td><var></var><big>[</big><var>default</var><big>]</big><var></var>)</td></tr></table></dt>
120<dd>
121Return a tuple containing all the subgroups of the match, from 1 up to
122however many groups are in the pattern. The <var>default</var> argument is
123used for groups that did not participate in the match; it defaults to
124<code>None</code>. (Incompatibility note: in the original Python 1.5
125release, if the tuple was one element long, a string would be returned
126instead. In later versions (from 1.5.1 on), a singleton tuple is
127returned in such cases.)
128</dl>
129
130<P>
131<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
132 <td><nobr><b><tt id='l2h-904' xml:id='l2h-904' class="method">groupdict</tt></b>(</nobr></td>
133 <td><var></var><big>[</big><var>default</var><big>]</big><var></var>)</td></tr></table></dt>
134<dd>
135Return a dictionary containing all the <em>named</em> subgroups of the
136match, keyed by the subgroup name. The <var>default</var> argument is
137used for groups that did not participate in the match; it defaults to
138<code>None</code>.
139</dl>
140
141<P>
142<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
143 <td><nobr><b><tt id='l2h-905' xml:id='l2h-905' class="method">start</tt></b>(</nobr></td>
144 <td><var></var><big>[</big><var>group</var><big>]</big><var></var>)</td></tr></table></dt>
145<dd>
146<dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
147 <td><nobr><b><tt id='l2h-906' xml:id='l2h-906' class="method">end</tt></b>(</nobr></td>
148 <td><var></var><big>[</big><var>group</var><big>]</big><var></var>)</td></tr></table></dt>
149<dd>Return the indices of the start and end of the substring
150matched by <var>group</var>; <var>group</var> defaults to zero (meaning the whole
151matched substring).
152Return <code>-1</code> if <var>group</var> exists but
153did not contribute to the match. For a match object
154<var>m</var>, and a group <var>g</var> that did contribute to the match, the
155substring matched by group <var>g</var> (equivalent to
156<code><var>m</var>.group(<var>g</var>)</code>) is
157
158<P>
159<div class="verbatim"><pre>
160m.string[m.start(g):m.end(g)]
161</pre></div>
162
163<P>
164Note that
165<code>m.start(<var>group</var>)</code> will equal <code>m.end(<var>group</var>)</code> if
166<var>group</var> matched a null string. For example, after <code><var>m</var> =
167re.search('b(c?)', 'cba')</code>, <code><var>m</var>.start(0)</code> is 1,
168<code><var>m</var>.end(0)</code> is 2, <code><var>m</var>.start(1)</code> and
169<code><var>m</var>.end(1)</code> are both 2, and <code><var>m</var>.start(2)</code> raises
170an <tt class="exception">IndexError</tt> exception.
171</dl>
172
173<P>
174<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
175 <td><nobr><b><tt id='l2h-907' xml:id='l2h-907' class="method">span</tt></b>(</nobr></td>
176 <td><var></var><big>[</big><var>group</var><big>]</big><var></var>)</td></tr></table></dt>
177<dd>
178For <tt class="class">MatchObject</tt> <var>m</var>, return the 2-tuple
179<code>(<var>m</var>.start(<var>group</var>), <var>m</var>.end(<var>group</var>))</code>.
180Note that if <var>group</var> did not contribute to the match, this is
181<code>(-1, -1)</code>. Again, <var>group</var> defaults to zero.
182</dl>
183
184<P>
185<dl><dt><b><tt id='l2h-908' xml:id='l2h-908' class="member">pos</tt></b></dt>
186<dd>
187The value of <var>pos</var> which was passed to the <tt class="function">search()</tt> or
188<tt class="function">match()</tt> method of the <tt class="class">RegexObject</tt>. This is the
189index into the string at which the RE engine started looking for a
190match.
191</dl>
192
193<P>
194<dl><dt><b><tt id='l2h-909' xml:id='l2h-909' class="member">endpos</tt></b></dt>
195<dd>
196The value of <var>endpos</var> which was passed to the <tt class="function">search()</tt>
197or <tt class="function">match()</tt> method of the <tt class="class">RegexObject</tt>. This is the
198index into the string beyond which the RE engine will not go.
199</dl>
200
201<P>
202<dl><dt><b><tt id='l2h-910' xml:id='l2h-910' class="member">lastindex</tt></b></dt>
203<dd>
204The integer index of the last matched capturing group, or <code>None</code>
205if no group was matched at all. For example, the expressions
206<tt class="regexp">(a)b</tt>, <tt class="regexp">((a)(b))</tt>, and <tt class="regexp">((ab))</tt> will have
207<code>lastindex == 1</code> if applied to the string <code>'ab'</code>,
208while the expression <tt class="regexp">(a)(b)</tt> will have <code>lastindex == 2</code>,
209if applied to the same string.
210</dl>
211
212<P>
213<dl><dt><b><tt id='l2h-911' xml:id='l2h-911' class="member">lastgroup</tt></b></dt>
214<dd>
215The name of the last matched capturing group, or <code>None</code> if the
216group didn't have a name, or if no group was matched at all.
217</dl>
218
219<P>
220<dl><dt><b><tt id='l2h-912' xml:id='l2h-912' class="member">re</tt></b></dt>
221<dd>
222The regular expression object whose <tt class="method">match()</tt> or
223<tt class="method">search()</tt> method produced this <tt class="class">MatchObject</tt> instance.
224</dl>
225
226<P>
227<dl><dt><b><tt id='l2h-913' xml:id='l2h-913' class="member">string</tt></b></dt>
228<dd>
229The string passed to <tt class="function">match()</tt> or <tt class="function">search()</tt>.
230</dl>
231
232<P>
233
234<DIV CLASS="navigation">
235<div class='online-navigation'>
236<p></p><hr />
237<table align="center" width="100%" cellpadding="0" cellspacing="2">
238<tr>
239<td class='online-navigation'><a rel="prev" title="4.2.4 Regular Expression Objects"
240 href="re-objects.html"><img src='../icons/previous.png'
241 border='0' height='32' alt='Previous Page' width='32' /></A></td>
242<td class='online-navigation'><a rel="parent" title="4.2 re "
243 href="module-re.html"><img src='../icons/up.png'
244 border='0' height='32' alt='Up One Level' width='32' /></A></td>
245<td class='online-navigation'><a rel="next" title="4.2.6 Examples"
246 href="node118.html"><img src='../icons/next.png'
247 border='0' height='32' alt='Next Page' width='32' /></A></td>
248<td align="center" width="100%">Python Library Reference</td>
249<td class='online-navigation'><a rel="contents" title="Table of Contents"
250 href="contents.html"><img src='../icons/contents.png'
251 border='0' height='32' alt='Contents' width='32' /></A></td>
252<td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png'
253 border='0' height='32' alt='Module Index' width='32' /></a></td>
254<td class='online-navigation'><a rel="index" title="Index"
255 href="genindex.html"><img src='../icons/index.png'
256 border='0' height='32' alt='Index' width='32' /></A></td>
257</tr></table>
258<div class='online-navigation'>
259<b class="navlabel">Previous:</b>
260<a class="sectref" rel="prev" href="re-objects.html">4.2.4 Regular Expression Objects</A>
261<b class="navlabel">Up:</b>
262<a class="sectref" rel="parent" href="module-re.html">4.2 re </A>
263<b class="navlabel">Next:</b>
264<a class="sectref" rel="next" href="node118.html">4.2.6 Examples</A>
265</div>
266</div>
267<hr />
268<span class="release-info">Release 2.4.2, documentation updated on 28 September 2005.</span>
269</DIV>
270<!--End of Navigation Panel-->
271<ADDRESS>
272See <i><a href="about.html">About this document...</a></i> for information on suggesting changes.
273</ADDRESS>
274</BODY>
275</HTML>