| 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="match-objects.html" /> |
| 13 | <link rel="prev" href="node115.html" /> |
| 14 | <link rel="parent" href="module-re.html" /> |
| 15 | <link rel="next" href="match-objects.html" /> |
| 16 | <meta name='aesop' content='information' /> |
| 17 | <title>4.2.4 Regular Expression 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.3 Module Contents" |
| 25 | href="node115.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.5 Match Objects" |
| 31 | href="match-objects.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="node115.html">4.2.3 Module Contents</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="match-objects.html">4.2.5 Match Objects</A> |
| 50 | </div> |
| 51 | <hr /></div> |
| 52 | </DIV> |
| 53 | <!--End of Navigation Panel--> |
| 54 | |
| 55 | <H2><A NAME="SECTION006240000000000000000"></A><A NAME="re-objects"></A> |
| 56 | <BR> |
| 57 | 4.2.4 Regular Expression Objects |
| 58 | </H2> |
| 59 | |
| 60 | <P> |
| 61 | Compiled regular expression objects support the following methods and |
| 62 | attributes: |
| 63 | |
| 64 | <P> |
| 65 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> |
| 66 | <td><nobr><b><tt id='l2h-891' xml:id='l2h-891' class="method">match</tt></b>(</nobr></td> |
| 67 | <td><var>string</var><big>[</big><var>, pos</var><big>[</big><var>, |
| 68 | endpos</var><big>]</big><var></var><big>]</big><var></var>)</td></tr></table></dt> |
| 69 | <dd> |
| 70 | If zero or more characters at the beginning of <var>string</var> match |
| 71 | this regular expression, return a corresponding |
| 72 | <tt class="class">MatchObject</tt> instance. Return <code>None</code> if the string does not |
| 73 | match the pattern; note that this is different from a zero-length |
| 74 | match. |
| 75 | |
| 76 | <P> |
| 77 | <span class="note"><b class="label">Note:</b> |
| 78 | If you want to locate a match anywhere in |
| 79 | <var>string</var>, use <tt class="method">search()</tt> instead.</span> |
| 80 | |
| 81 | <P> |
| 82 | The optional second parameter <var>pos</var> gives an index in the string |
| 83 | where the search is to start; it defaults to <code>0</code>. This is not |
| 84 | completely equivalent to slicing the string; the |
| 85 | <code>'^'</code> pattern |
| 86 | character matches at the real beginning of the string and at positions |
| 87 | just after a newline, but not necessarily at the index where the search |
| 88 | is to start. |
| 89 | |
| 90 | <P> |
| 91 | The optional parameter <var>endpos</var> limits how far the string will |
| 92 | be searched; it will be as if the string is <var>endpos</var> characters |
| 93 | long, so only the characters from <var>pos</var> to <code><var>endpos</var> - |
| 94 | 1</code> will be searched for a match. If <var>endpos</var> is less than |
| 95 | <var>pos</var>, no match will be found, otherwise, if <var>rx</var> is a |
| 96 | compiled regular expression object, |
| 97 | <code><var>rx</var>.match(<var>string</var>, 0, 50)</code> is equivalent to |
| 98 | <code><var>rx</var>.match(<var>string</var>[:50], 0)</code>. |
| 99 | </dl> |
| 100 | |
| 101 | <P> |
| 102 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> |
| 103 | <td><nobr><b><tt id='l2h-892' xml:id='l2h-892' class="method">search</tt></b>(</nobr></td> |
| 104 | <td><var>string</var><big>[</big><var>, pos</var><big>[</big><var>, |
| 105 | endpos</var><big>]</big><var></var><big>]</big><var></var>)</td></tr></table></dt> |
| 106 | <dd> |
| 107 | Scan through <var>string</var> looking for a location where this regular |
| 108 | expression produces a match, and return a |
| 109 | corresponding <tt class="class">MatchObject</tt> instance. Return <code>None</code> if no |
| 110 | position in the string matches the pattern; note that this is |
| 111 | different from finding a zero-length match at some point in the string. |
| 112 | |
| 113 | <P> |
| 114 | The optional <var>pos</var> and <var>endpos</var> parameters have the same |
| 115 | meaning as for the <tt class="method">match()</tt> method. |
| 116 | </dl> |
| 117 | |
| 118 | <P> |
| 119 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> |
| 120 | <td><nobr><b><tt id='l2h-893' xml:id='l2h-893' class="method">split</tt></b>(</nobr></td> |
| 121 | <td><var>string</var><big>[</big><var>, |
| 122 | maxsplit<code> = 0</code></var><big>]</big><var></var>)</td></tr></table></dt> |
| 123 | <dd> |
| 124 | Identical to the <tt class="function">split()</tt> function, using the compiled pattern. |
| 125 | </dl> |
| 126 | |
| 127 | <P> |
| 128 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> |
| 129 | <td><nobr><b><tt id='l2h-894' xml:id='l2h-894' class="method">findall</tt></b>(</nobr></td> |
| 130 | <td><var>string</var><big>[</big><var>, pos</var><big>[</big><var>, |
| 131 | endpos</var><big>]</big><var></var><big>]</big><var></var>)</td></tr></table></dt> |
| 132 | <dd> |
| 133 | Identical to the <tt class="function">findall()</tt> function, using the compiled pattern. |
| 134 | </dl> |
| 135 | |
| 136 | <P> |
| 137 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> |
| 138 | <td><nobr><b><tt id='l2h-895' xml:id='l2h-895' class="method">finditer</tt></b>(</nobr></td> |
| 139 | <td><var>string</var><big>[</big><var>, pos</var><big>[</big><var>, |
| 140 | endpos</var><big>]</big><var></var><big>]</big><var></var>)</td></tr></table></dt> |
| 141 | <dd> |
| 142 | Identical to the <tt class="function">finditer()</tt> function, using the compiled pattern. |
| 143 | </dl> |
| 144 | |
| 145 | <P> |
| 146 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> |
| 147 | <td><nobr><b><tt id='l2h-896' xml:id='l2h-896' class="method">sub</tt></b>(</nobr></td> |
| 148 | <td><var>repl, string</var><big>[</big><var>, count<code> = 0</code></var><big>]</big><var></var>)</td></tr></table></dt> |
| 149 | <dd> |
| 150 | Identical to the <tt class="function">sub()</tt> function, using the compiled pattern. |
| 151 | </dl> |
| 152 | |
| 153 | <P> |
| 154 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> |
| 155 | <td><nobr><b><tt id='l2h-897' xml:id='l2h-897' class="method">subn</tt></b>(</nobr></td> |
| 156 | <td><var>repl, string</var><big>[</big><var>, |
| 157 | count<code> = 0</code></var><big>]</big><var></var>)</td></tr></table></dt> |
| 158 | <dd> |
| 159 | Identical to the <tt class="function">subn()</tt> function, using the compiled pattern. |
| 160 | </dl> |
| 161 | |
| 162 | <P> |
| 163 | <dl><dt><b><tt id='l2h-898' xml:id='l2h-898' class="member">flags</tt></b></dt> |
| 164 | <dd> |
| 165 | The flags argument used when the RE object was compiled, or |
| 166 | <code>0</code> if no flags were provided. |
| 167 | </dl> |
| 168 | |
| 169 | <P> |
| 170 | <dl><dt><b><tt id='l2h-899' xml:id='l2h-899' class="member">groupindex</tt></b></dt> |
| 171 | <dd> |
| 172 | A dictionary mapping any symbolic group names defined by |
| 173 | <tt class="regexp">(?P<<var>id</var>>)</tt> to group numbers. The dictionary is empty if no |
| 174 | symbolic groups were used in the pattern. |
| 175 | </dl> |
| 176 | |
| 177 | <P> |
| 178 | <dl><dt><b><tt id='l2h-900' xml:id='l2h-900' class="member">pattern</tt></b></dt> |
| 179 | <dd> |
| 180 | The pattern string from which the RE object was compiled. |
| 181 | </dl> |
| 182 | |
| 183 | <P> |
| 184 | |
| 185 | <DIV CLASS="navigation"> |
| 186 | <div class='online-navigation'> |
| 187 | <p></p><hr /> |
| 188 | <table align="center" width="100%" cellpadding="0" cellspacing="2"> |
| 189 | <tr> |
| 190 | <td class='online-navigation'><a rel="prev" title="4.2.3 Module Contents" |
| 191 | href="node115.html"><img src='../icons/previous.png' |
| 192 | border='0' height='32' alt='Previous Page' width='32' /></A></td> |
| 193 | <td class='online-navigation'><a rel="parent" title="4.2 re " |
| 194 | href="module-re.html"><img src='../icons/up.png' |
| 195 | border='0' height='32' alt='Up One Level' width='32' /></A></td> |
| 196 | <td class='online-navigation'><a rel="next" title="4.2.5 Match Objects" |
| 197 | href="match-objects.html"><img src='../icons/next.png' |
| 198 | border='0' height='32' alt='Next Page' width='32' /></A></td> |
| 199 | <td align="center" width="100%">Python Library Reference</td> |
| 200 | <td class='online-navigation'><a rel="contents" title="Table of Contents" |
| 201 | href="contents.html"><img src='../icons/contents.png' |
| 202 | border='0' height='32' alt='Contents' width='32' /></A></td> |
| 203 | <td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png' |
| 204 | border='0' height='32' alt='Module Index' width='32' /></a></td> |
| 205 | <td class='online-navigation'><a rel="index" title="Index" |
| 206 | href="genindex.html"><img src='../icons/index.png' |
| 207 | border='0' height='32' alt='Index' width='32' /></A></td> |
| 208 | </tr></table> |
| 209 | <div class='online-navigation'> |
| 210 | <b class="navlabel">Previous:</b> |
| 211 | <a class="sectref" rel="prev" href="node115.html">4.2.3 Module Contents</A> |
| 212 | <b class="navlabel">Up:</b> |
| 213 | <a class="sectref" rel="parent" href="module-re.html">4.2 re </A> |
| 214 | <b class="navlabel">Next:</b> |
| 215 | <a class="sectref" rel="next" href="match-objects.html">4.2.5 Match Objects</A> |
| 216 | </div> |
| 217 | </div> |
| 218 | <hr /> |
| 219 | <span class="release-info">Release 2.4.2, documentation updated on 28 September 2005.</span> |
| 220 | </DIV> |
| 221 | <!--End of Navigation Panel--> |
| 222 | <ADDRESS> |
| 223 | See <i><a href="about.html">About this document...</a></i> for information on suggesting changes. |
| 224 | </ADDRESS> |
| 225 | </BODY> |
| 226 | </HTML> |