~dkuhlman/python-training-materials/Materials

« back to all changes in this revision

Viewing changes to python-2.7.11-docs-html/library/re.html

  • Committer: Dave Kuhlman
  • Date: 2016-02-11 21:17:09 UTC
  • Revision ID: dkuhlman@davekuhlman.org-20160211211709-03yaen3cjempbi2m
Updated Python 2.7 docs; added Python 3.5 docs

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
 
2
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 
3
 
 
4
 
 
5
<html xmlns="http://www.w3.org/1999/xhtml">
 
6
  <head>
 
7
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
 
8
    
 
9
    <title>7.2. re — Regular expression operations &mdash; Python 2.7.11 documentation</title>
 
10
    
 
11
    <link rel="stylesheet" href="../_static/classic.css" type="text/css" />
 
12
    <link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
 
13
    
 
14
    <script type="text/javascript">
 
15
      var DOCUMENTATION_OPTIONS = {
 
16
        URL_ROOT:    '../',
 
17
        VERSION:     '2.7.11',
 
18
        COLLAPSE_INDEX: false,
 
19
        FILE_SUFFIX: '.html',
 
20
        HAS_SOURCE:  true
 
21
      };
 
22
    </script>
 
23
    <script type="text/javascript" src="../_static/jquery.js"></script>
 
24
    <script type="text/javascript" src="../_static/underscore.js"></script>
 
25
    <script type="text/javascript" src="../_static/doctools.js"></script>
 
26
    <script type="text/javascript" src="../_static/sidebar.js"></script>
 
27
    <link rel="search" type="application/opensearchdescription+xml"
 
28
          title="Search within Python 2.7.11 documentation"
 
29
          href="../_static/opensearch.xml"/>
 
30
    <link rel="author" title="About these documents" href="../about.html" />
 
31
    <link rel="copyright" title="Copyright" href="../copyright.html" />
 
32
    <link rel="top" title="Python 2.7.11 documentation" href="../contents.html" />
 
33
    <link rel="up" title="7. String Services" href="strings.html" />
 
34
    <link rel="next" title="7.3. struct — Interpret strings as packed binary data" href="struct.html" />
 
35
    <link rel="prev" title="7.1. string — Common string operations" href="string.html" />
 
36
    <link rel="shortcut icon" type="image/png" href="../_static/py.png" />
 
37
    <script type="text/javascript" src="../_static/copybutton.js"></script>
 
38
    <script type="text/javascript" src="../_static/version_switch.js"></script>
 
39
 
 
40
    
 
41
 
 
42
  </head>
 
43
  <body role="document">  
 
44
    <div class="related" role="navigation" aria-label="related navigation">
 
45
      <h3>Navigation</h3>
 
46
      <ul>
 
47
        <li class="right" style="margin-right: 10px">
 
48
          <a href="../genindex.html" title="General Index"
 
49
             accesskey="I">index</a></li>
 
50
        <li class="right" >
 
51
          <a href="../py-modindex.html" title="Python Module Index"
 
52
             >modules</a> |</li>
 
53
        <li class="right" >
 
54
          <a href="struct.html" title="7.3. struct — Interpret strings as packed binary data"
 
55
             accesskey="N">next</a> |</li>
 
56
        <li class="right" >
 
57
          <a href="string.html" title="7.1. string — Common string operations"
 
58
             accesskey="P">previous</a> |</li>
 
59
        <li><img src="../_static/py.png" alt=""
 
60
                 style="vertical-align: middle; margin-top: -1px"/></li>
 
61
        <li><a href="https://www.python.org/">Python</a> &raquo;</li>
 
62
        <li>
 
63
          <span class="version_switcher_placeholder">2.7.11</span>
 
64
          <a href="../index.html">Documentation</a> &raquo;
 
65
        </li>
 
66
 
 
67
          <li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> &raquo;</li>
 
68
          <li class="nav-item nav-item-2"><a href="strings.html" accesskey="U">7. String Services</a> &raquo;</li> 
 
69
      </ul>
 
70
    </div>    
 
71
 
 
72
    <div class="document">
 
73
      <div class="documentwrapper">
 
74
        <div class="bodywrapper">
 
75
          <div class="body" role="main">
 
76
            
 
77
  <div class="section" id="module-re">
 
78
<span id="re-regular-expression-operations"></span><h1>7.2. <a class="reference internal" href="#module-re" title="re: Regular expression operations."><code class="xref py py-mod docutils literal"><span class="pre">re</span></code></a> &#8212; Regular expression operations<a class="headerlink" href="#module-re" title="Permalink to this headline">¶</a></h1>
 
79
<p>This module provides regular expression matching operations similar to
 
80
those found in Perl. Both patterns and strings to be searched can be
 
81
Unicode strings as well as 8-bit strings.</p>
 
82
<p>Regular expressions use the backslash character (<code class="docutils literal"><span class="pre">'\'</span></code>) to indicate
 
83
special forms or to allow special characters to be used without invoking
 
84
their special meaning.  This collides with Python&#8217;s usage of the same
 
85
character for the same purpose in string literals; for example, to match
 
86
a literal backslash, one might have to write <code class="docutils literal"><span class="pre">'\\\\'</span></code> as the pattern
 
87
string, because the regular expression must be <code class="docutils literal"><span class="pre">\\</span></code>, and each
 
88
backslash must be expressed as <code class="docutils literal"><span class="pre">\\</span></code> inside a regular Python string
 
89
literal.</p>
 
90
<p>The solution is to use Python&#8217;s raw string notation for regular expression
 
91
patterns; backslashes are not handled in any special way in a string literal
 
92
prefixed with <code class="docutils literal"><span class="pre">'r'</span></code>.  So <code class="docutils literal"><span class="pre">r&quot;\n&quot;</span></code> is a two-character string containing
 
93
<code class="docutils literal"><span class="pre">'\'</span></code> and <code class="docutils literal"><span class="pre">'n'</span></code>, while <code class="docutils literal"><span class="pre">&quot;\n&quot;</span></code> is a one-character string containing a
 
94
newline.  Usually patterns will be expressed in Python code using this raw
 
95
string notation.</p>
 
96
<p>It is important to note that most regular expression operations are available as
 
97
module-level functions and <a class="reference internal" href="#re.RegexObject" title="re.RegexObject"><code class="xref py py-class docutils literal"><span class="pre">RegexObject</span></code></a> methods.  The functions are
 
98
shortcuts that don&#8217;t require you to compile a regex object first, but miss some
 
99
fine-tuning parameters.</p>
 
100
<div class="section" id="regular-expression-syntax">
 
101
<span id="re-syntax"></span><h2>7.2.1. Regular Expression Syntax<a class="headerlink" href="#regular-expression-syntax" title="Permalink to this headline">¶</a></h2>
 
102
<p>A regular expression (or RE) specifies a set of strings that matches it; the
 
103
functions in this module let you check if a particular string matches a given
 
104
regular expression (or if a given regular expression matches a particular
 
105
string, which comes down to the same thing).</p>
 
106
<p>Regular expressions can be concatenated to form new regular expressions; if <em>A</em>
 
107
and <em>B</em> are both regular expressions, then <em>AB</em> is also a regular expression.
 
108
In general, if a string <em>p</em> matches <em>A</em> and another string <em>q</em> matches <em>B</em>, the
 
109
string <em>pq</em> will match AB.  This holds unless <em>A</em> or <em>B</em> contain low precedence
 
110
operations; boundary conditions between <em>A</em> and <em>B</em>; or have numbered group
 
111
references.  Thus, complex expressions can easily be constructed from simpler
 
112
primitive expressions like the ones described here.  For details of the theory
 
113
and implementation of regular expressions, consult the Friedl book referenced
 
114
above, or almost any textbook about compiler construction.</p>
 
115
<p>A brief explanation of the format of regular expressions follows.  For further
 
116
information and a gentler presentation, consult the <a class="reference internal" href="../howto/regex.html#regex-howto"><span>Regular Expression HOWTO</span></a>.</p>
 
117
<p>Regular expressions can contain both special and ordinary characters. Most
 
118
ordinary characters, like <code class="docutils literal"><span class="pre">'A'</span></code>, <code class="docutils literal"><span class="pre">'a'</span></code>, or <code class="docutils literal"><span class="pre">'0'</span></code>, are the simplest regular
 
119
expressions; they simply match themselves.  You can concatenate ordinary
 
120
characters, so <code class="docutils literal"><span class="pre">last</span></code> matches the string <code class="docutils literal"><span class="pre">'last'</span></code>.  (In the rest of this
 
121
section, we&#8217;ll write RE&#8217;s in <code class="docutils literal"><span class="pre">this</span> <span class="pre">special</span> <span class="pre">style</span></code>, usually without quotes, and
 
122
strings to be matched <code class="docutils literal"><span class="pre">'in</span> <span class="pre">single</span> <span class="pre">quotes'</span></code>.)</p>
 
123
<p>Some characters, like <code class="docutils literal"><span class="pre">'|'</span></code> or <code class="docutils literal"><span class="pre">'('</span></code>, are special. Special
 
124
characters either stand for classes of ordinary characters, or affect
 
125
how the regular expressions around them are interpreted. Regular
 
126
expression pattern strings may not contain null bytes, but can specify
 
127
the null byte using the <code class="docutils literal"><span class="pre">\number</span></code> notation, e.g., <code class="docutils literal"><span class="pre">'\x00'</span></code>.</p>
 
128
<p>The special characters are:</p>
 
129
<dl class="docutils">
 
130
<dt><code class="docutils literal"><span class="pre">'.'</span></code></dt>
 
131
<dd>(Dot.)  In the default mode, this matches any character except a newline.  If
 
132
the <a class="reference internal" href="#re.DOTALL" title="re.DOTALL"><code class="xref py py-const docutils literal"><span class="pre">DOTALL</span></code></a> flag has been specified, this matches any character
 
133
including a newline.</dd>
 
134
<dt><code class="docutils literal"><span class="pre">'^'</span></code></dt>
 
135
<dd>(Caret.)  Matches the start of the string, and in <a class="reference internal" href="#re.MULTILINE" title="re.MULTILINE"><code class="xref py py-const docutils literal"><span class="pre">MULTILINE</span></code></a> mode also
 
136
matches immediately after each newline.</dd>
 
137
<dt><code class="docutils literal"><span class="pre">'$'</span></code></dt>
 
138
<dd>Matches the end of the string or just before the newline at the end of the
 
139
string, and in <a class="reference internal" href="#re.MULTILINE" title="re.MULTILINE"><code class="xref py py-const docutils literal"><span class="pre">MULTILINE</span></code></a> mode also matches before a newline.  <code class="docutils literal"><span class="pre">foo</span></code>
 
140
matches both &#8216;foo&#8217; and &#8216;foobar&#8217;, while the regular expression <code class="docutils literal"><span class="pre">foo$</span></code> matches
 
141
only &#8216;foo&#8217;.  More interestingly, searching for <code class="docutils literal"><span class="pre">foo.$</span></code> in <code class="docutils literal"><span class="pre">'foo1\nfoo2\n'</span></code>
 
142
matches &#8216;foo2&#8217; normally, but &#8216;foo1&#8217; in <a class="reference internal" href="#re.MULTILINE" title="re.MULTILINE"><code class="xref py py-const docutils literal"><span class="pre">MULTILINE</span></code></a> mode; searching for
 
143
a single <code class="docutils literal"><span class="pre">$</span></code> in <code class="docutils literal"><span class="pre">'foo\n'</span></code> will find two (empty) matches: one just before
 
144
the newline, and one at the end of the string.</dd>
 
145
<dt><code class="docutils literal"><span class="pre">'*'</span></code></dt>
 
146
<dd>Causes the resulting RE to match 0 or more repetitions of the preceding RE, as
 
147
many repetitions as are possible.  <code class="docutils literal"><span class="pre">ab*</span></code> will match &#8216;a&#8217;, &#8216;ab&#8217;, or &#8216;a&#8217; followed
 
148
by any number of &#8216;b&#8217;s.</dd>
 
149
<dt><code class="docutils literal"><span class="pre">'+'</span></code></dt>
 
150
<dd>Causes the resulting RE to match 1 or more repetitions of the preceding RE.
 
151
<code class="docutils literal"><span class="pre">ab+</span></code> will match &#8216;a&#8217; followed by any non-zero number of &#8216;b&#8217;s; it will not
 
152
match just &#8216;a&#8217;.</dd>
 
153
<dt><code class="docutils literal"><span class="pre">'?'</span></code></dt>
 
154
<dd>Causes the resulting RE to match 0 or 1 repetitions of the preceding RE.
 
155
<code class="docutils literal"><span class="pre">ab?</span></code> will match either &#8216;a&#8217; or &#8216;ab&#8217;.</dd>
 
156
<dt><code class="docutils literal"><span class="pre">*?</span></code>, <code class="docutils literal"><span class="pre">+?</span></code>, <code class="docutils literal"><span class="pre">??</span></code></dt>
 
157
<dd>The <code class="docutils literal"><span class="pre">'*'</span></code>, <code class="docutils literal"><span class="pre">'+'</span></code>, and <code class="docutils literal"><span class="pre">'?'</span></code> qualifiers are all <em class="dfn">greedy</em>; they match
 
158
as much text as possible.  Sometimes this behaviour isn&#8217;t desired; if the RE
 
159
<code class="docutils literal"><span class="pre">&lt;.*&gt;</span></code> is matched against <code class="docutils literal"><span class="pre">'&lt;H1&gt;title&lt;/H1&gt;'</span></code>, it will match the entire
 
160
string, and not just <code class="docutils literal"><span class="pre">'&lt;H1&gt;'</span></code>.  Adding <code class="docutils literal"><span class="pre">'?'</span></code> after the qualifier makes it
 
161
perform the match in <em class="dfn">non-greedy</em> or <em class="dfn">minimal</em> fashion; as <em>few</em>
 
162
characters as possible will be matched.  Using <code class="docutils literal"><span class="pre">.*?</span></code> in the previous
 
163
expression will match only <code class="docutils literal"><span class="pre">'&lt;H1&gt;'</span></code>.</dd>
 
164
<dt><code class="docutils literal"><span class="pre">{m}</span></code></dt>
 
165
<dd>Specifies that exactly <em>m</em> copies of the previous RE should be matched; fewer
 
166
matches cause the entire RE not to match.  For example, <code class="docutils literal"><span class="pre">a{6}</span></code> will match
 
167
exactly six <code class="docutils literal"><span class="pre">'a'</span></code> characters, but not five.</dd>
 
168
<dt><code class="docutils literal"><span class="pre">{m,n}</span></code></dt>
 
169
<dd>Causes the resulting RE to match from <em>m</em> to <em>n</em> repetitions of the preceding
 
170
RE, attempting to match as many repetitions as possible.  For example,
 
171
<code class="docutils literal"><span class="pre">a{3,5}</span></code> will match from 3 to 5 <code class="docutils literal"><span class="pre">'a'</span></code> characters.  Omitting <em>m</em> specifies a
 
172
lower bound of zero,  and omitting <em>n</em> specifies an infinite upper bound.  As an
 
173
example, <code class="docutils literal"><span class="pre">a{4,}b</span></code> will match <code class="docutils literal"><span class="pre">aaaab</span></code> or a thousand <code class="docutils literal"><span class="pre">'a'</span></code> characters
 
174
followed by a <code class="docutils literal"><span class="pre">b</span></code>, but not <code class="docutils literal"><span class="pre">aaab</span></code>. The comma may not be omitted or the
 
175
modifier would be confused with the previously described form.</dd>
 
176
<dt><code class="docutils literal"><span class="pre">{m,n}?</span></code></dt>
 
177
<dd>Causes the resulting RE to match from <em>m</em> to <em>n</em> repetitions of the preceding
 
178
RE, attempting to match as <em>few</em> repetitions as possible.  This is the
 
179
non-greedy version of the previous qualifier.  For example, on the
 
180
6-character string <code class="docutils literal"><span class="pre">'aaaaaa'</span></code>, <code class="docutils literal"><span class="pre">a{3,5}</span></code> will match 5 <code class="docutils literal"><span class="pre">'a'</span></code> characters,
 
181
while <code class="docutils literal"><span class="pre">a{3,5}?</span></code> will only match 3 characters.</dd>
 
182
<dt><code class="docutils literal"><span class="pre">'\'</span></code></dt>
 
183
<dd><p class="first">Either escapes special characters (permitting you to match characters like
 
184
<code class="docutils literal"><span class="pre">'*'</span></code>, <code class="docutils literal"><span class="pre">'?'</span></code>, and so forth), or signals a special sequence; special
 
185
sequences are discussed below.</p>
 
186
<p class="last">If you&#8217;re not using a raw string to express the pattern, remember that Python
 
187
also uses the backslash as an escape sequence in string literals; if the escape
 
188
sequence isn&#8217;t recognized by Python&#8217;s parser, the backslash and subsequent
 
189
character are included in the resulting string.  However, if Python would
 
190
recognize the resulting sequence, the backslash should be repeated twice.  This
 
191
is complicated and hard to understand, so it&#8217;s highly recommended that you use
 
192
raw strings for all but the simplest expressions.</p>
 
193
</dd>
 
194
<dt><code class="docutils literal"><span class="pre">[]</span></code></dt>
 
195
<dd><p class="first">Used to indicate a set of characters.  In a set:</p>
 
196
<ul class="last simple">
 
197
<li>Characters can be listed individually, e.g. <code class="docutils literal"><span class="pre">[amk]</span></code> will match <code class="docutils literal"><span class="pre">'a'</span></code>,
 
198
<code class="docutils literal"><span class="pre">'m'</span></code>, or <code class="docutils literal"><span class="pre">'k'</span></code>.</li>
 
199
<li>Ranges of characters can be indicated by giving two characters and separating
 
200
them by a <code class="docutils literal"><span class="pre">'-'</span></code>, for example <code class="docutils literal"><span class="pre">[a-z]</span></code> will match any lowercase ASCII letter,
 
201
<code class="docutils literal"><span class="pre">[0-5][0-9]</span></code> will match all the two-digits numbers from <code class="docutils literal"><span class="pre">00</span></code> to <code class="docutils literal"><span class="pre">59</span></code>, and
 
202
<code class="docutils literal"><span class="pre">[0-9A-Fa-f]</span></code> will match any hexadecimal digit.  If <code class="docutils literal"><span class="pre">-</span></code> is escaped (e.g.
 
203
<code class="docutils literal"><span class="pre">[a\-z]</span></code>) or if it&#8217;s placed as the first or last character (e.g. <code class="docutils literal"><span class="pre">[a-]</span></code>),
 
204
it will match a literal <code class="docutils literal"><span class="pre">'-'</span></code>.</li>
 
205
<li>Special characters lose their special meaning inside sets.  For example,
 
206
<code class="docutils literal"><span class="pre">[(+*)]</span></code> will match any of the literal characters <code class="docutils literal"><span class="pre">'('</span></code>, <code class="docutils literal"><span class="pre">'+'</span></code>,
 
207
<code class="docutils literal"><span class="pre">'*'</span></code>, or <code class="docutils literal"><span class="pre">')'</span></code>.</li>
 
208
<li>Character classes such as <code class="docutils literal"><span class="pre">\w</span></code> or <code class="docutils literal"><span class="pre">\S</span></code> (defined below) are also accepted
 
209
inside a set, although the characters they match depends on whether
 
210
<a class="reference internal" href="#re.LOCALE" title="re.LOCALE"><code class="xref py py-const docutils literal"><span class="pre">LOCALE</span></code></a> or  <a class="reference internal" href="#re.UNICODE" title="re.UNICODE"><code class="xref py py-const docutils literal"><span class="pre">UNICODE</span></code></a> mode is in force.</li>
 
211
<li>Characters that are not within a range can be matched by <em class="dfn">complementing</em>
 
212
the set.  If the first character of the set is <code class="docutils literal"><span class="pre">'^'</span></code>, all the characters
 
213
that are <em>not</em> in the set will be matched.  For example, <code class="docutils literal"><span class="pre">[^5]</span></code> will match
 
214
any character except <code class="docutils literal"><span class="pre">'5'</span></code>, and <code class="docutils literal"><span class="pre">[^^]</span></code> will match any character except
 
215
<code class="docutils literal"><span class="pre">'^'</span></code>.  <code class="docutils literal"><span class="pre">^</span></code> has no special meaning if it&#8217;s not the first character in
 
216
the set.</li>
 
217
<li>To match a literal <code class="docutils literal"><span class="pre">']'</span></code> inside a set, precede it with a backslash, or
 
218
place it at the beginning of the set.  For example, both <code class="docutils literal"><span class="pre">[()[\]{}]</span></code> and
 
219
<code class="docutils literal"><span class="pre">[]()[{}]</span></code> will both match a parenthesis.</li>
 
220
</ul>
 
221
</dd>
 
222
<dt><code class="docutils literal"><span class="pre">'|'</span></code></dt>
 
223
<dd><code class="docutils literal"><span class="pre">A|B</span></code>, where A and B can be arbitrary REs, creates a regular expression that
 
224
will match either A or B.  An arbitrary number of REs can be separated by the
 
225
<code class="docutils literal"><span class="pre">'|'</span></code> in this way.  This can be used inside groups (see below) as well.  As
 
226
the target string is scanned, REs separated by <code class="docutils literal"><span class="pre">'|'</span></code> are tried from left to
 
227
right. When one pattern completely matches, that branch is accepted. This means
 
228
that once <code class="docutils literal"><span class="pre">A</span></code> matches, <code class="docutils literal"><span class="pre">B</span></code> will not be tested further, even if it would
 
229
produce a longer overall match.  In other words, the <code class="docutils literal"><span class="pre">'|'</span></code> operator is never
 
230
greedy.  To match a literal <code class="docutils literal"><span class="pre">'|'</span></code>, use <code class="docutils literal"><span class="pre">\|</span></code>, or enclose it inside a
 
231
character class, as in <code class="docutils literal"><span class="pre">[|]</span></code>.</dd>
 
232
<dt><code class="docutils literal"><span class="pre">(...)</span></code></dt>
 
233
<dd>Matches whatever regular expression is inside the parentheses, and indicates the
 
234
start and end of a group; the contents of a group can be retrieved after a match
 
235
has been performed, and can be matched later in the string with the <code class="docutils literal"><span class="pre">\number</span></code>
 
236
special sequence, described below.  To match the literals <code class="docutils literal"><span class="pre">'('</span></code> or <code class="docutils literal"><span class="pre">')'</span></code>,
 
237
use <code class="docutils literal"><span class="pre">\(</span></code> or <code class="docutils literal"><span class="pre">\)</span></code>, or enclose them inside a character class: <code class="docutils literal"><span class="pre">[(]</span> <span class="pre">[)]</span></code>.</dd>
 
238
<dt><code class="docutils literal"><span class="pre">(?...)</span></code></dt>
 
239
<dd>This is an extension notation (a <code class="docutils literal"><span class="pre">'?'</span></code> following a <code class="docutils literal"><span class="pre">'('</span></code> is not meaningful
 
240
otherwise).  The first character after the <code class="docutils literal"><span class="pre">'?'</span></code> determines what the meaning
 
241
and further syntax of the construct is. Extensions usually do not create a new
 
242
group; <code class="docutils literal"><span class="pre">(?P&lt;name&gt;...)</span></code> is the only exception to this rule. Following are the
 
243
currently supported extensions.</dd>
 
244
<dt><code class="docutils literal"><span class="pre">(?iLmsux)</span></code></dt>
 
245
<dd><p class="first">(One or more letters from the set <code class="docutils literal"><span class="pre">'i'</span></code>, <code class="docutils literal"><span class="pre">'L'</span></code>, <code class="docutils literal"><span class="pre">'m'</span></code>, <code class="docutils literal"><span class="pre">'s'</span></code>,
 
246
<code class="docutils literal"><span class="pre">'u'</span></code>, <code class="docutils literal"><span class="pre">'x'</span></code>.)  The group matches the empty string; the letters
 
247
set the corresponding flags: <a class="reference internal" href="#re.I" title="re.I"><code class="xref py py-const docutils literal"><span class="pre">re.I</span></code></a> (ignore case),
 
248
<a class="reference internal" href="#re.L" title="re.L"><code class="xref py py-const docutils literal"><span class="pre">re.L</span></code></a> (locale dependent), <a class="reference internal" href="#re.M" title="re.M"><code class="xref py py-const docutils literal"><span class="pre">re.M</span></code></a> (multi-line),
 
249
<a class="reference internal" href="#re.S" title="re.S"><code class="xref py py-const docutils literal"><span class="pre">re.S</span></code></a> (dot matches all), <a class="reference internal" href="#re.U" title="re.U"><code class="xref py py-const docutils literal"><span class="pre">re.U</span></code></a> (Unicode dependent),
 
250
and <a class="reference internal" href="#re.X" title="re.X"><code class="xref py py-const docutils literal"><span class="pre">re.X</span></code></a> (verbose), for the entire regular expression. (The
 
251
flags are described in <a class="reference internal" href="#contents-of-module-re"><span>Module Contents</span></a>.) This
 
252
is useful if you wish to include the flags as part of the regular
 
253
expression, instead of passing a <em>flag</em> argument to the
 
254
<a class="reference internal" href="#re.compile" title="re.compile"><code class="xref py py-func docutils literal"><span class="pre">re.compile()</span></code></a> function.</p>
 
255
<p class="last">Note that the <code class="docutils literal"><span class="pre">(?x)</span></code> flag changes how the expression is parsed. It should be
 
256
used first in the expression string, or after one or more whitespace characters.
 
257
If there are non-whitespace characters before the flag, the results are
 
258
undefined.</p>
 
259
</dd>
 
260
<dt><code class="docutils literal"><span class="pre">(?:...)</span></code></dt>
 
261
<dd>A non-capturing version of regular parentheses.  Matches whatever regular
 
262
expression is inside the parentheses, but the substring matched by the group
 
263
<em>cannot</em> be retrieved after performing a match or referenced later in the
 
264
pattern.</dd>
 
265
<dt><code class="docutils literal"><span class="pre">(?P&lt;name&gt;...)</span></code></dt>
 
266
<dd><p class="first">Similar to regular parentheses, but the substring matched by the group is
 
267
accessible via the symbolic group name <em>name</em>.  Group names must be valid
 
268
Python identifiers, and each group name must be defined only once within a
 
269
regular expression.  A symbolic group is also a numbered group, just as if
 
270
the group were not named.</p>
 
271
<p>Named groups can be referenced in three contexts.  If the pattern is
 
272
<code class="docutils literal"><span class="pre">(?P&lt;quote&gt;['&quot;]).*?(?P=quote)</span></code> (i.e. matching a string quoted with either
 
273
single or double quotes):</p>
 
274
<table border="1" class="last docutils">
 
275
<colgroup>
 
276
<col width="53%" />
 
277
<col width="47%" />
 
278
</colgroup>
 
279
<thead valign="bottom">
 
280
<tr class="row-odd"><th class="head">Context of reference to group &#8220;quote&#8221;</th>
 
281
<th class="head">Ways to reference it</th>
 
282
</tr>
 
283
</thead>
 
284
<tbody valign="top">
 
285
<tr class="row-even"><td>in the same pattern itself</td>
 
286
<td><ul class="first last simple">
 
287
<li><code class="docutils literal"><span class="pre">(?P=quote)</span></code> (as shown)</li>
 
288
<li><code class="docutils literal"><span class="pre">\1</span></code></li>
 
289
</ul>
 
290
</td>
 
291
</tr>
 
292
<tr class="row-odd"><td>when processing match object <code class="docutils literal"><span class="pre">m</span></code></td>
 
293
<td><ul class="first last simple">
 
294
<li><code class="docutils literal"><span class="pre">m.group('quote')</span></code></li>
 
295
<li><code class="docutils literal"><span class="pre">m.end('quote')</span></code> (etc.)</li>
 
296
</ul>
 
297
</td>
 
298
</tr>
 
299
<tr class="row-even"><td>in a string passed to the <code class="docutils literal"><span class="pre">repl</span></code>
 
300
argument of <code class="docutils literal"><span class="pre">re.sub()</span></code></td>
 
301
<td><ul class="first last simple">
 
302
<li><code class="docutils literal"><span class="pre">\g&lt;quote&gt;</span></code></li>
 
303
<li><code class="docutils literal"><span class="pre">\g&lt;1&gt;</span></code></li>
 
304
<li><code class="docutils literal"><span class="pre">\1</span></code></li>
 
305
</ul>
 
306
</td>
 
307
</tr>
 
308
</tbody>
 
309
</table>
 
310
</dd>
 
311
<dt><code class="docutils literal"><span class="pre">(?P=name)</span></code></dt>
 
312
<dd>A backreference to a named group; it matches whatever text was matched by the
 
313
earlier group named <em>name</em>.</dd>
 
314
<dt><code class="docutils literal"><span class="pre">(?#...)</span></code></dt>
 
315
<dd>A comment; the contents of the parentheses are simply ignored.</dd>
 
316
<dt><code class="docutils literal"><span class="pre">(?=...)</span></code></dt>
 
317
<dd>Matches if <code class="docutils literal"><span class="pre">...</span></code> matches next, but doesn&#8217;t consume any of the string.  This is
 
318
called a lookahead assertion.  For example, <code class="docutils literal"><span class="pre">Isaac</span> <span class="pre">(?=Asimov)</span></code> will match
 
319
<code class="docutils literal"><span class="pre">'Isaac</span> <span class="pre">'</span></code> only if it&#8217;s followed by <code class="docutils literal"><span class="pre">'Asimov'</span></code>.</dd>
 
320
<dt><code class="docutils literal"><span class="pre">(?!...)</span></code></dt>
 
321
<dd>Matches if <code class="docutils literal"><span class="pre">...</span></code> doesn&#8217;t match next.  This is a negative lookahead assertion.
 
322
For example, <code class="docutils literal"><span class="pre">Isaac</span> <span class="pre">(?!Asimov)</span></code> will match <code class="docutils literal"><span class="pre">'Isaac</span> <span class="pre">'</span></code> only if it&#8217;s <em>not</em>
 
323
followed by <code class="docutils literal"><span class="pre">'Asimov'</span></code>.</dd>
 
324
<dt><code class="docutils literal"><span class="pre">(?&lt;=...)</span></code></dt>
 
325
<dd><p class="first">Matches if the current position in the string is preceded by a match for <code class="docutils literal"><span class="pre">...</span></code>
 
326
that ends at the current position.  This is called a <em class="dfn">positive lookbehind
 
327
assertion</em>. <code class="docutils literal"><span class="pre">(?&lt;=abc)def</span></code> will find a match in <code class="docutils literal"><span class="pre">abcdef</span></code>, since the
 
328
lookbehind will back up 3 characters and check if the contained pattern matches.
 
329
The contained pattern must only match strings of some fixed length, meaning that
 
330
<code class="docutils literal"><span class="pre">abc</span></code> or <code class="docutils literal"><span class="pre">a|b</span></code> are allowed, but <code class="docutils literal"><span class="pre">a*</span></code> and <code class="docutils literal"><span class="pre">a{3,4}</span></code> are not.  Group
 
331
references are not supported even if they match strings of some fixed length.
 
332
Note that
 
333
patterns which start with positive lookbehind assertions will not match at the
 
334
beginning of the string being searched; you will most likely want to use the
 
335
<a class="reference internal" href="#re.search" title="re.search"><code class="xref py py-func docutils literal"><span class="pre">search()</span></code></a> function rather than the <a class="reference internal" href="#re.match" title="re.match"><code class="xref py py-func docutils literal"><span class="pre">match()</span></code></a> function:</p>
 
336
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="kn">import</span> <span class="nn">re</span>
 
337
<span class="gp">&gt;&gt;&gt; </span><span class="n">m</span> <span class="o">=</span> <span class="n">re</span><span class="o">.</span><span class="n">search</span><span class="p">(</span><span class="s1">&#39;(?&lt;=abc)def&#39;</span><span class="p">,</span> <span class="s1">&#39;abcdef&#39;</span><span class="p">)</span>
 
338
<span class="gp">&gt;&gt;&gt; </span><span class="n">m</span><span class="o">.</span><span class="n">group</span><span class="p">(</span><span class="mi">0</span><span class="p">)</span>
 
339
<span class="go">&#39;def&#39;</span>
 
340
</pre></div>
 
341
</div>
 
342
<p>This example looks for a word following a hyphen:</p>
 
343
<div class="last highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">m</span> <span class="o">=</span> <span class="n">re</span><span class="o">.</span><span class="n">search</span><span class="p">(</span><span class="s1">&#39;(?&lt;=-)\w+&#39;</span><span class="p">,</span> <span class="s1">&#39;spam-egg&#39;</span><span class="p">)</span>
 
344
<span class="gp">&gt;&gt;&gt; </span><span class="n">m</span><span class="o">.</span><span class="n">group</span><span class="p">(</span><span class="mi">0</span><span class="p">)</span>
 
345
<span class="go">&#39;egg&#39;</span>
 
346
</pre></div>
 
347
</div>
 
348
</dd>
 
349
<dt><code class="docutils literal"><span class="pre">(?&lt;!...)</span></code></dt>
 
350
<dd>Matches if the current position in the string is not preceded by a match for
 
351
<code class="docutils literal"><span class="pre">...</span></code>.  This is called a <em class="dfn">negative lookbehind assertion</em>.  Similar to
 
352
positive lookbehind assertions, the contained pattern must only match strings of
 
353
some fixed length and shouldn&#8217;t contain group references.
 
354
Patterns which start with negative lookbehind assertions may
 
355
match at the beginning of the string being searched.</dd>
 
356
<dt><code class="docutils literal"><span class="pre">(?(id/name)yes-pattern|no-pattern)</span></code></dt>
 
357
<dd><p class="first">Will try to match with <code class="docutils literal"><span class="pre">yes-pattern</span></code> if the group with given <em>id</em> or <em>name</em>
 
358
exists, and with <code class="docutils literal"><span class="pre">no-pattern</span></code> if it doesn&#8217;t. <code class="docutils literal"><span class="pre">no-pattern</span></code> is optional and
 
359
can be omitted. For example,  <code class="docutils literal"><span class="pre">(&lt;)?(\w+&#64;\w+(?:\.\w+)+)(?(1)&gt;)</span></code> is a poor email
 
360
matching pattern, which will match with <code class="docutils literal"><span class="pre">'&lt;user&#64;host.com&gt;'</span></code> as well as
 
361
<code class="docutils literal"><span class="pre">'user&#64;host.com'</span></code>, but not with <code class="docutils literal"><span class="pre">'&lt;user&#64;host.com'</span></code>.</p>
 
362
<div class="last versionadded">
 
363
<p><span class="versionmodified">New in version 2.4.</span></p>
 
364
</div>
 
365
</dd>
 
366
</dl>
 
367
<p>The special sequences consist of <code class="docutils literal"><span class="pre">'\'</span></code> and a character from the list below.
 
368
If the ordinary character is not on the list, then the resulting RE will match
 
369
the second character.  For example, <code class="docutils literal"><span class="pre">\$</span></code> matches the character <code class="docutils literal"><span class="pre">'$'</span></code>.</p>
 
370
<dl class="docutils">
 
371
<dt><code class="docutils literal"><span class="pre">\number</span></code></dt>
 
372
<dd>Matches the contents of the group of the same number.  Groups are numbered
 
373
starting from 1.  For example, <code class="docutils literal"><span class="pre">(.+)</span> <span class="pre">\1</span></code> matches <code class="docutils literal"><span class="pre">'the</span> <span class="pre">the'</span></code> or <code class="docutils literal"><span class="pre">'55</span> <span class="pre">55'</span></code>,
 
374
but not <code class="docutils literal"><span class="pre">'thethe'</span></code> (note the space after the group).  This special sequence
 
375
can only be used to match one of the first 99 groups.  If the first digit of
 
376
<em>number</em> is 0, or <em>number</em> is 3 octal digits long, it will not be interpreted as
 
377
a group match, but as the character with octal value <em>number</em>. Inside the
 
378
<code class="docutils literal"><span class="pre">'['</span></code> and <code class="docutils literal"><span class="pre">']'</span></code> of a character class, all numeric escapes are treated as
 
379
characters.</dd>
 
380
<dt><code class="docutils literal"><span class="pre">\A</span></code></dt>
 
381
<dd>Matches only at the start of the string.</dd>
 
382
<dt><code class="docutils literal"><span class="pre">\b</span></code></dt>
 
383
<dd>Matches the empty string, but only at the beginning or end of a word.  A word is
 
384
defined as a sequence of alphanumeric or underscore characters, so the end of a
 
385
word is indicated by whitespace or a non-alphanumeric, non-underscore character.
 
386
Note that formally, <code class="docutils literal"><span class="pre">\b</span></code> is defined as the boundary between a <code class="docutils literal"><span class="pre">\w</span></code> and
 
387
a <code class="docutils literal"><span class="pre">\W</span></code> character (or vice versa), or between <code class="docutils literal"><span class="pre">\w</span></code> and the beginning/end
 
388
of the string, so the precise set of characters deemed to be alphanumeric
 
389
depends on the values of the <code class="docutils literal"><span class="pre">UNICODE</span></code> and <code class="docutils literal"><span class="pre">LOCALE</span></code> flags.
 
390
For example, <code class="docutils literal"><span class="pre">r'\bfoo\b'</span></code> matches <code class="docutils literal"><span class="pre">'foo'</span></code>, <code class="docutils literal"><span class="pre">'foo.'</span></code>, <code class="docutils literal"><span class="pre">'(foo)'</span></code>,
 
391
<code class="docutils literal"><span class="pre">'bar</span> <span class="pre">foo</span> <span class="pre">baz'</span></code> but not <code class="docutils literal"><span class="pre">'foobar'</span></code> or <code class="docutils literal"><span class="pre">'foo3'</span></code>.
 
392
Inside a character range, <code class="docutils literal"><span class="pre">\b</span></code> represents the backspace character, for
 
393
compatibility with Python&#8217;s string literals.</dd>
 
394
<dt><code class="docutils literal"><span class="pre">\B</span></code></dt>
 
395
<dd>Matches the empty string, but only when it is <em>not</em> at the beginning or end of a
 
396
word.  This means that <code class="docutils literal"><span class="pre">r'py\B'</span></code> matches <code class="docutils literal"><span class="pre">'python'</span></code>, <code class="docutils literal"><span class="pre">'py3'</span></code>, <code class="docutils literal"><span class="pre">'py2'</span></code>,
 
397
but not <code class="docutils literal"><span class="pre">'py'</span></code>, <code class="docutils literal"><span class="pre">'py.'</span></code>, or <code class="docutils literal"><span class="pre">'py!'</span></code>.
 
398
<code class="docutils literal"><span class="pre">\B</span></code> is just the opposite of <code class="docutils literal"><span class="pre">\b</span></code>, so is also subject to the settings
 
399
of <code class="docutils literal"><span class="pre">LOCALE</span></code> and <code class="docutils literal"><span class="pre">UNICODE</span></code>.</dd>
 
400
<dt><code class="docutils literal"><span class="pre">\d</span></code></dt>
 
401
<dd>When the <a class="reference internal" href="#re.UNICODE" title="re.UNICODE"><code class="xref py py-const docutils literal"><span class="pre">UNICODE</span></code></a> flag is not specified, matches any decimal digit; this
 
402
is equivalent to the set <code class="docutils literal"><span class="pre">[0-9]</span></code>.  With <a class="reference internal" href="#re.UNICODE" title="re.UNICODE"><code class="xref py py-const docutils literal"><span class="pre">UNICODE</span></code></a>, it will match
 
403
whatever is classified as a decimal digit in the Unicode character properties
 
404
database.</dd>
 
405
<dt><code class="docutils literal"><span class="pre">\D</span></code></dt>
 
406
<dd>When the <a class="reference internal" href="#re.UNICODE" title="re.UNICODE"><code class="xref py py-const docutils literal"><span class="pre">UNICODE</span></code></a> flag is not specified, matches any non-digit
 
407
character; this is equivalent to the set  <code class="docutils literal"><span class="pre">[^0-9]</span></code>.  With <a class="reference internal" href="#re.UNICODE" title="re.UNICODE"><code class="xref py py-const docutils literal"><span class="pre">UNICODE</span></code></a>, it
 
408
will match  anything other than character marked as digits in the Unicode
 
409
character  properties database.</dd>
 
410
<dt><code class="docutils literal"><span class="pre">\s</span></code></dt>
 
411
<dd>When the <a class="reference internal" href="#re.UNICODE" title="re.UNICODE"><code class="xref py py-const docutils literal"><span class="pre">UNICODE</span></code></a> flag is not specified, it matches any whitespace
 
412
character, this is equivalent to the set <code class="docutils literal"><span class="pre">[</span> <span class="pre">\t\n\r\f\v]</span></code>. The
 
413
<a class="reference internal" href="#re.LOCALE" title="re.LOCALE"><code class="xref py py-const docutils literal"><span class="pre">LOCALE</span></code></a> flag has no extra effect on matching of the space.
 
414
If <a class="reference internal" href="#re.UNICODE" title="re.UNICODE"><code class="xref py py-const docutils literal"><span class="pre">UNICODE</span></code></a> is set, this will match the characters <code class="docutils literal"><span class="pre">[</span> <span class="pre">\t\n\r\f\v]</span></code>
 
415
plus whatever is classified as space in the Unicode character properties
 
416
database.</dd>
 
417
<dt><code class="docutils literal"><span class="pre">\S</span></code></dt>
 
418
<dd>When the <a class="reference internal" href="#re.UNICODE" title="re.UNICODE"><code class="xref py py-const docutils literal"><span class="pre">UNICODE</span></code></a> flag is not specified, matches any non-whitespace
 
419
character; this is equivalent to the set <code class="docutils literal"><span class="pre">[^</span> <span class="pre">\t\n\r\f\v]</span></code> The
 
420
<a class="reference internal" href="#re.LOCALE" title="re.LOCALE"><code class="xref py py-const docutils literal"><span class="pre">LOCALE</span></code></a> flag has no extra effect on non-whitespace match.  If
 
421
<a class="reference internal" href="#re.UNICODE" title="re.UNICODE"><code class="xref py py-const docutils literal"><span class="pre">UNICODE</span></code></a> is set, then any character not marked as space in the
 
422
Unicode character properties database is matched.</dd>
 
423
<dt><code class="docutils literal"><span class="pre">\w</span></code></dt>
 
424
<dd>When the <a class="reference internal" href="#re.LOCALE" title="re.LOCALE"><code class="xref py py-const docutils literal"><span class="pre">LOCALE</span></code></a> and <a class="reference internal" href="#re.UNICODE" title="re.UNICODE"><code class="xref py py-const docutils literal"><span class="pre">UNICODE</span></code></a> flags are not specified, matches
 
425
any alphanumeric character and the underscore; this is equivalent to the set
 
426
<code class="docutils literal"><span class="pre">[a-zA-Z0-9_]</span></code>.  With <a class="reference internal" href="#re.LOCALE" title="re.LOCALE"><code class="xref py py-const docutils literal"><span class="pre">LOCALE</span></code></a>, it will match the set <code class="docutils literal"><span class="pre">[0-9_]</span></code> plus
 
427
whatever characters are defined as alphanumeric for the current locale.  If
 
428
<a class="reference internal" href="#re.UNICODE" title="re.UNICODE"><code class="xref py py-const docutils literal"><span class="pre">UNICODE</span></code></a> is set, this will match the characters <code class="docutils literal"><span class="pre">[0-9_]</span></code> plus whatever
 
429
is classified as alphanumeric in the Unicode character properties database.</dd>
 
430
<dt><code class="docutils literal"><span class="pre">\W</span></code></dt>
 
431
<dd>When the <a class="reference internal" href="#re.LOCALE" title="re.LOCALE"><code class="xref py py-const docutils literal"><span class="pre">LOCALE</span></code></a> and <a class="reference internal" href="#re.UNICODE" title="re.UNICODE"><code class="xref py py-const docutils literal"><span class="pre">UNICODE</span></code></a> flags are not specified, matches
 
432
any non-alphanumeric character; this is equivalent to the set <code class="docutils literal"><span class="pre">[^a-zA-Z0-9_]</span></code>.
 
433
With <a class="reference internal" href="#re.LOCALE" title="re.LOCALE"><code class="xref py py-const docutils literal"><span class="pre">LOCALE</span></code></a>, it will match any character not in the set <code class="docutils literal"><span class="pre">[0-9_]</span></code>, and
 
434
not defined as alphanumeric for the current locale. If <a class="reference internal" href="#re.UNICODE" title="re.UNICODE"><code class="xref py py-const docutils literal"><span class="pre">UNICODE</span></code></a> is set,
 
435
this will match anything other than <code class="docutils literal"><span class="pre">[0-9_]</span></code> plus characters classified as
 
436
not alphanumeric in the Unicode character properties database.</dd>
 
437
<dt><code class="docutils literal"><span class="pre">\Z</span></code></dt>
 
438
<dd>Matches only at the end of the string.</dd>
 
439
</dl>
 
440
<p>If both <a class="reference internal" href="#re.LOCALE" title="re.LOCALE"><code class="xref py py-const docutils literal"><span class="pre">LOCALE</span></code></a> and <a class="reference internal" href="#re.UNICODE" title="re.UNICODE"><code class="xref py py-const docutils literal"><span class="pre">UNICODE</span></code></a> flags are included for a
 
441
particular sequence, then <a class="reference internal" href="#re.LOCALE" title="re.LOCALE"><code class="xref py py-const docutils literal"><span class="pre">LOCALE</span></code></a> flag takes effect first followed by
 
442
the <a class="reference internal" href="#re.UNICODE" title="re.UNICODE"><code class="xref py py-const docutils literal"><span class="pre">UNICODE</span></code></a>.</p>
 
443
<p>Most of the standard escapes supported by Python string literals are also
 
444
accepted by the regular expression parser:</p>
 
445
<div class="highlight-python"><div class="highlight"><pre>\a      \b      \f      \n
 
446
\r      \t      \v      \x
 
447
\\
 
448
</pre></div>
 
449
</div>
 
450
<p>(Note that <code class="docutils literal"><span class="pre">\b</span></code> is used to represent word boundaries, and means &#8220;backspace&#8221;
 
451
only inside character classes.)</p>
 
452
<p>Octal escapes are included in a limited form: If the first digit is a 0, or if
 
453
there are three octal digits, it is considered an octal escape. Otherwise, it is
 
454
a group reference.  As for string literals, octal escapes are always at most
 
455
three digits in length.</p>
 
456
<div class="admonition seealso">
 
457
<p class="first admonition-title">See also</p>
 
458
<dl class="last docutils">
 
459
<dt>Mastering Regular Expressions</dt>
 
460
<dd>Book on regular expressions by Jeffrey Friedl, published by O&#8217;Reilly.  The
 
461
second edition of the book no longer covers Python at all, but the first
 
462
edition covered writing good regular expression patterns in great detail.</dd>
 
463
</dl>
 
464
</div>
 
465
</div>
 
466
<div class="section" id="module-contents">
 
467
<span id="contents-of-module-re"></span><h2>7.2.2. Module Contents<a class="headerlink" href="#module-contents" title="Permalink to this headline">¶</a></h2>
 
468
<p>The module defines several functions, constants, and an exception. Some of the
 
469
functions are simplified versions of the full featured methods for compiled
 
470
regular expressions.  Most non-trivial applications always use the compiled
 
471
form.</p>
 
472
<dl class="function">
 
473
<dt id="re.compile">
 
474
<code class="descclassname">re.</code><code class="descname">compile</code><span class="sig-paren">(</span><em>pattern</em>, <em>flags=0</em><span class="sig-paren">)</span><a class="headerlink" href="#re.compile" title="Permalink to this definition">¶</a></dt>
 
475
<dd><p>Compile a regular expression pattern into a regular expression object, which
 
476
can be used for matching using its <a class="reference internal" href="#re.RegexObject.match" title="re.RegexObject.match"><code class="xref py py-func docutils literal"><span class="pre">match()</span></code></a> and
 
477
<a class="reference internal" href="#re.RegexObject.search" title="re.RegexObject.search"><code class="xref py py-func docutils literal"><span class="pre">search()</span></code></a> methods, described below.</p>
 
478
<p>The expression&#8217;s behaviour can be modified by specifying a <em>flags</em> value.
 
479
Values can be any of the following variables, combined using bitwise OR (the
 
480
<code class="docutils literal"><span class="pre">|</span></code> operator).</p>
 
481
<p>The sequence</p>
 
482
<div class="highlight-python"><div class="highlight"><pre><span class="n">prog</span> <span class="o">=</span> <span class="n">re</span><span class="o">.</span><span class="n">compile</span><span class="p">(</span><span class="n">pattern</span><span class="p">)</span>
 
483
<span class="n">result</span> <span class="o">=</span> <span class="n">prog</span><span class="o">.</span><span class="n">match</span><span class="p">(</span><span class="n">string</span><span class="p">)</span>
 
484
</pre></div>
 
485
</div>
 
486
<p>is equivalent to</p>
 
487
<div class="highlight-python"><div class="highlight"><pre><span class="n">result</span> <span class="o">=</span> <span class="n">re</span><span class="o">.</span><span class="n">match</span><span class="p">(</span><span class="n">pattern</span><span class="p">,</span> <span class="n">string</span><span class="p">)</span>
 
488
</pre></div>
 
489
</div>
 
490
<p>but using <a class="reference internal" href="#re.compile" title="re.compile"><code class="xref py py-func docutils literal"><span class="pre">re.compile()</span></code></a> and saving the resulting regular expression
 
491
object for reuse is more efficient when the expression will be used several
 
492
times in a single program.</p>
 
493
<div class="admonition note">
 
494
<p class="first admonition-title">Note</p>
 
495
<p class="last">The compiled versions of the most recent patterns passed to
 
496
<a class="reference internal" href="#re.match" title="re.match"><code class="xref py py-func docutils literal"><span class="pre">re.match()</span></code></a>, <a class="reference internal" href="#re.search" title="re.search"><code class="xref py py-func docutils literal"><span class="pre">re.search()</span></code></a> or <a class="reference internal" href="#re.compile" title="re.compile"><code class="xref py py-func docutils literal"><span class="pre">re.compile()</span></code></a> are cached, so
 
497
programs that use only a few regular expressions at a time needn&#8217;t worry
 
498
about compiling regular expressions.</p>
 
499
</div>
 
500
</dd></dl>
 
501
 
 
502
<dl class="data">
 
503
<dt id="re.DEBUG">
 
504
<code class="descclassname">re.</code><code class="descname">DEBUG</code><a class="headerlink" href="#re.DEBUG" title="Permalink to this definition">¶</a></dt>
 
505
<dd><p>Display debug information about compiled expression.</p>
 
506
</dd></dl>
 
507
 
 
508
<dl class="data">
 
509
<dt id="re.I">
 
510
<code class="descclassname">re.</code><code class="descname">I</code><a class="headerlink" href="#re.I" title="Permalink to this definition">¶</a></dt>
 
511
<dt id="re.IGNORECASE">
 
512
<code class="descclassname">re.</code><code class="descname">IGNORECASE</code><a class="headerlink" href="#re.IGNORECASE" title="Permalink to this definition">¶</a></dt>
 
513
<dd><p>Perform case-insensitive matching; expressions like <code class="docutils literal"><span class="pre">[A-Z]</span></code> will match
 
514
lowercase letters, too.  This is not affected by the current locale.</p>
 
515
</dd></dl>
 
516
 
 
517
<dl class="data">
 
518
<dt id="re.L">
 
519
<code class="descclassname">re.</code><code class="descname">L</code><a class="headerlink" href="#re.L" title="Permalink to this definition">¶</a></dt>
 
520
<dt id="re.LOCALE">
 
521
<code class="descclassname">re.</code><code class="descname">LOCALE</code><a class="headerlink" href="#re.LOCALE" title="Permalink to this definition">¶</a></dt>
 
522
<dd><p>Make <code class="docutils literal"><span class="pre">\w</span></code>, <code class="docutils literal"><span class="pre">\W</span></code>, <code class="docutils literal"><span class="pre">\b</span></code>, <code class="docutils literal"><span class="pre">\B</span></code>, <code class="docutils literal"><span class="pre">\s</span></code> and <code class="docutils literal"><span class="pre">\S</span></code> dependent on the
 
523
current locale.</p>
 
524
</dd></dl>
 
525
 
 
526
<dl class="data">
 
527
<dt id="re.M">
 
528
<code class="descclassname">re.</code><code class="descname">M</code><a class="headerlink" href="#re.M" title="Permalink to this definition">¶</a></dt>
 
529
<dt id="re.MULTILINE">
 
530
<code class="descclassname">re.</code><code class="descname">MULTILINE</code><a class="headerlink" href="#re.MULTILINE" title="Permalink to this definition">¶</a></dt>
 
531
<dd><p>When specified, the pattern character <code class="docutils literal"><span class="pre">'^'</span></code> matches at the beginning of the
 
532
string and at the beginning of each line (immediately following each newline);
 
533
and the pattern character <code class="docutils literal"><span class="pre">'$'</span></code> matches at the end of the string and at the
 
534
end of each line (immediately preceding each newline).  By default, <code class="docutils literal"><span class="pre">'^'</span></code>
 
535
matches only at the beginning of the string, and <code class="docutils literal"><span class="pre">'$'</span></code> only at the end of the
 
536
string and immediately before the newline (if any) at the end of the string.</p>
 
537
</dd></dl>
 
538
 
 
539
<dl class="data">
 
540
<dt id="re.S">
 
541
<code class="descclassname">re.</code><code class="descname">S</code><a class="headerlink" href="#re.S" title="Permalink to this definition">¶</a></dt>
 
542
<dt id="re.DOTALL">
 
543
<code class="descclassname">re.</code><code class="descname">DOTALL</code><a class="headerlink" href="#re.DOTALL" title="Permalink to this definition">¶</a></dt>
 
544
<dd><p>Make the <code class="docutils literal"><span class="pre">'.'</span></code> special character match any character at all, including a
 
545
newline; without this flag, <code class="docutils literal"><span class="pre">'.'</span></code> will match anything <em>except</em> a newline.</p>
 
546
</dd></dl>
 
547
 
 
548
<dl class="data">
 
549
<dt id="re.U">
 
550
<code class="descclassname">re.</code><code class="descname">U</code><a class="headerlink" href="#re.U" title="Permalink to this definition">¶</a></dt>
 
551
<dt id="re.UNICODE">
 
552
<code class="descclassname">re.</code><code class="descname">UNICODE</code><a class="headerlink" href="#re.UNICODE" title="Permalink to this definition">¶</a></dt>
 
553
<dd><p>Make <code class="docutils literal"><span class="pre">\w</span></code>, <code class="docutils literal"><span class="pre">\W</span></code>, <code class="docutils literal"><span class="pre">\b</span></code>, <code class="docutils literal"><span class="pre">\B</span></code>, <code class="docutils literal"><span class="pre">\d</span></code>, <code class="docutils literal"><span class="pre">\D</span></code>, <code class="docutils literal"><span class="pre">\s</span></code> and <code class="docutils literal"><span class="pre">\S</span></code> dependent
 
554
on the Unicode character properties database.</p>
 
555
<div class="versionadded">
 
556
<p><span class="versionmodified">New in version 2.0.</span></p>
 
557
</div>
 
558
</dd></dl>
 
559
 
 
560
<dl class="data">
 
561
<dt id="re.X">
 
562
<code class="descclassname">re.</code><code class="descname">X</code><a class="headerlink" href="#re.X" title="Permalink to this definition">¶</a></dt>
 
563
<dt id="re.VERBOSE">
 
564
<code class="descclassname">re.</code><code class="descname">VERBOSE</code><a class="headerlink" href="#re.VERBOSE" title="Permalink to this definition">¶</a></dt>
 
565
<dd><p>This flag allows you to write regular expressions that look nicer and are
 
566
more readable by allowing you to visually separate logical sections of the
 
567
pattern and add comments. Whitespace within the pattern is ignored, except
 
568
when in a character class or when preceded by an unescaped backslash.
 
569
When a line contains a <code class="docutils literal"><span class="pre">#</span></code> that is not in a character class and is not
 
570
preceded by an unescaped backslash, all characters from the leftmost such
 
571
<code class="docutils literal"><span class="pre">#</span></code> through the end of the line are ignored.</p>
 
572
<p>This means that the two following regular expression objects that match a
 
573
decimal number are functionally equal:</p>
 
574
<div class="highlight-python"><div class="highlight"><pre><span class="n">a</span> <span class="o">=</span> <span class="n">re</span><span class="o">.</span><span class="n">compile</span><span class="p">(</span><span class="s2">r&quot;&quot;&quot;\d +  # the integral part</span>
 
575
<span class="s2">                   \.    # the decimal point</span>
 
576
<span class="s2">                   \d *  # some fractional digits&quot;&quot;&quot;</span><span class="p">,</span> <span class="n">re</span><span class="o">.</span><span class="n">X</span><span class="p">)</span>
 
577
<span class="n">b</span> <span class="o">=</span> <span class="n">re</span><span class="o">.</span><span class="n">compile</span><span class="p">(</span><span class="s2">r&quot;\d+\.\d*&quot;</span><span class="p">)</span>
 
578
</pre></div>
 
579
</div>
 
580
</dd></dl>
 
581
 
 
582
<dl class="function">
 
583
<dt id="re.search">
 
584
<code class="descclassname">re.</code><code class="descname">search</code><span class="sig-paren">(</span><em>pattern</em>, <em>string</em>, <em>flags=0</em><span class="sig-paren">)</span><a class="headerlink" href="#re.search" title="Permalink to this definition">¶</a></dt>
 
585
<dd><p>Scan through <em>string</em> looking for the first location where the regular expression
 
586
<em>pattern</em> produces a match, and return a corresponding <a class="reference internal" href="#re.MatchObject" title="re.MatchObject"><code class="xref py py-class docutils literal"><span class="pre">MatchObject</span></code></a>
 
587
instance. Return <code class="docutils literal"><span class="pre">None</span></code> if no position in the string matches the pattern; note
 
588
that this is different from finding a zero-length match at some point in the
 
589
string.</p>
 
590
</dd></dl>
 
591
 
 
592
<dl class="function">
 
593
<dt id="re.match">
 
594
<code class="descclassname">re.</code><code class="descname">match</code><span class="sig-paren">(</span><em>pattern</em>, <em>string</em>, <em>flags=0</em><span class="sig-paren">)</span><a class="headerlink" href="#re.match" title="Permalink to this definition">¶</a></dt>
 
595
<dd><p>If zero or more characters at the beginning of <em>string</em> match the regular
 
596
expression <em>pattern</em>, return a corresponding <a class="reference internal" href="#re.MatchObject" title="re.MatchObject"><code class="xref py py-class docutils literal"><span class="pre">MatchObject</span></code></a> instance.
 
597
Return <code class="docutils literal"><span class="pre">None</span></code> if the string does not match the pattern; note that this is
 
598
different from a zero-length match.</p>
 
599
<p>Note that even in <a class="reference internal" href="#re.MULTILINE" title="re.MULTILINE"><code class="xref py py-const docutils literal"><span class="pre">MULTILINE</span></code></a> mode, <a class="reference internal" href="#re.match" title="re.match"><code class="xref py py-func docutils literal"><span class="pre">re.match()</span></code></a> will only match
 
600
at the beginning of the string and not at the beginning of each line.</p>
 
601
<p>If you want to locate a match anywhere in <em>string</em>, use <a class="reference internal" href="#re.search" title="re.search"><code class="xref py py-func docutils literal"><span class="pre">search()</span></code></a>
 
602
instead (see also <a class="reference internal" href="#search-vs-match"><span>search() vs. match()</span></a>).</p>
 
603
</dd></dl>
 
604
 
 
605
<dl class="function">
 
606
<dt id="re.split">
 
607
<code class="descclassname">re.</code><code class="descname">split</code><span class="sig-paren">(</span><em>pattern</em>, <em>string</em>, <em>maxsplit=0</em>, <em>flags=0</em><span class="sig-paren">)</span><a class="headerlink" href="#re.split" title="Permalink to this definition">¶</a></dt>
 
608
<dd><p>Split <em>string</em> by the occurrences of <em>pattern</em>.  If capturing parentheses are
 
609
used in <em>pattern</em>, then the text of all groups in the pattern are also returned
 
610
as part of the resulting list. If <em>maxsplit</em> is nonzero, at most <em>maxsplit</em>
 
611
splits occur, and the remainder of the string is returned as the final element
 
612
of the list.  (Incompatibility note: in the original Python 1.5 release,
 
613
<em>maxsplit</em> was ignored.  This has been fixed in later releases.)</p>
 
614
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">re</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="s1">&#39;\W+&#39;</span><span class="p">,</span> <span class="s1">&#39;Words, words, words.&#39;</span><span class="p">)</span>
 
615
<span class="go">[&#39;Words&#39;, &#39;words&#39;, &#39;words&#39;, &#39;&#39;]</span>
 
616
<span class="gp">&gt;&gt;&gt; </span><span class="n">re</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="s1">&#39;(\W+)&#39;</span><span class="p">,</span> <span class="s1">&#39;Words, words, words.&#39;</span><span class="p">)</span>
 
617
<span class="go">[&#39;Words&#39;, &#39;, &#39;, &#39;words&#39;, &#39;, &#39;, &#39;words&#39;, &#39;.&#39;, &#39;&#39;]</span>
 
618
<span class="gp">&gt;&gt;&gt; </span><span class="n">re</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="s1">&#39;\W+&#39;</span><span class="p">,</span> <span class="s1">&#39;Words, words, words.&#39;</span><span class="p">,</span> <span class="mi">1</span><span class="p">)</span>
 
619
<span class="go">[&#39;Words&#39;, &#39;words, words.&#39;]</span>
 
620
<span class="gp">&gt;&gt;&gt; </span><span class="n">re</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="s1">&#39;[a-f]+&#39;</span><span class="p">,</span> <span class="s1">&#39;0a3B9&#39;</span><span class="p">,</span> <span class="n">flags</span><span class="o">=</span><span class="n">re</span><span class="o">.</span><span class="n">IGNORECASE</span><span class="p">)</span>
 
621
<span class="go">[&#39;0&#39;, &#39;3&#39;, &#39;9&#39;]</span>
 
622
</pre></div>
 
623
</div>
 
624
<p>If there are capturing groups in the separator and it matches at the start of
 
625
the string, the result will start with an empty string.  The same holds for
 
626
the end of the string:</p>
 
627
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">re</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="s1">&#39;(\W+)&#39;</span><span class="p">,</span> <span class="s1">&#39;...words, words...&#39;</span><span class="p">)</span>
 
628
<span class="go">[&#39;&#39;, &#39;...&#39;, &#39;words&#39;, &#39;, &#39;, &#39;words&#39;, &#39;...&#39;, &#39;&#39;]</span>
 
629
</pre></div>
 
630
</div>
 
631
<p>That way, separator components are always found at the same relative
 
632
indices within the result list (e.g., if there&#8217;s one capturing group
 
633
in the separator, the 0th, the 2nd and so forth).</p>
 
634
<p>Note that <em>split</em> will never split a string on an empty pattern match.
 
635
For example:</p>
 
636
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">re</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="s1">&#39;x*&#39;</span><span class="p">,</span> <span class="s1">&#39;foo&#39;</span><span class="p">)</span>
 
637
<span class="go">[&#39;foo&#39;]</span>
 
638
<span class="gp">&gt;&gt;&gt; </span><span class="n">re</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="s2">&quot;(?m)^$&quot;</span><span class="p">,</span> <span class="s2">&quot;foo</span><span class="se">\n\n</span><span class="s2">bar</span><span class="se">\n</span><span class="s2">&quot;</span><span class="p">)</span>
 
639
<span class="go">[&#39;foo\n\nbar\n&#39;]</span>
 
640
</pre></div>
 
641
</div>
 
642
<div class="versionchanged">
 
643
<p><span class="versionmodified">Changed in version 2.7: </span>Added the optional flags argument.</p>
 
644
</div>
 
645
</dd></dl>
 
646
 
 
647
<dl class="function">
 
648
<dt id="re.findall">
 
649
<code class="descclassname">re.</code><code class="descname">findall</code><span class="sig-paren">(</span><em>pattern</em>, <em>string</em>, <em>flags=0</em><span class="sig-paren">)</span><a class="headerlink" href="#re.findall" title="Permalink to this definition">¶</a></dt>
 
650
<dd><p>Return all non-overlapping matches of <em>pattern</em> in <em>string</em>, as a list of
 
651
strings.  The <em>string</em> is scanned left-to-right, and matches are returned in
 
652
the order found.  If one or more groups are present in the pattern, return a
 
653
list of groups; this will be a list of tuples if the pattern has more than
 
654
one group.  Empty matches are included in the result unless they touch the
 
655
beginning of another match.</p>
 
656
<div class="versionadded">
 
657
<p><span class="versionmodified">New in version 1.5.2.</span></p>
 
658
</div>
 
659
<div class="versionchanged">
 
660
<p><span class="versionmodified">Changed in version 2.4: </span>Added the optional flags argument.</p>
 
661
</div>
 
662
</dd></dl>
 
663
 
 
664
<dl class="function">
 
665
<dt id="re.finditer">
 
666
<code class="descclassname">re.</code><code class="descname">finditer</code><span class="sig-paren">(</span><em>pattern</em>, <em>string</em>, <em>flags=0</em><span class="sig-paren">)</span><a class="headerlink" href="#re.finditer" title="Permalink to this definition">¶</a></dt>
 
667
<dd><p>Return an <a class="reference internal" href="../glossary.html#term-iterator"><span class="xref std std-term">iterator</span></a> yielding <a class="reference internal" href="#re.MatchObject" title="re.MatchObject"><code class="xref py py-class docutils literal"><span class="pre">MatchObject</span></code></a> instances over all
 
668
non-overlapping matches for the RE <em>pattern</em> in <em>string</em>.  The <em>string</em> is
 
669
scanned left-to-right, and matches are returned in the order found.  Empty
 
670
matches are included in the result unless they touch the beginning of another
 
671
match.</p>
 
672
<div class="versionadded">
 
673
<p><span class="versionmodified">New in version 2.2.</span></p>
 
674
</div>
 
675
<div class="versionchanged">
 
676
<p><span class="versionmodified">Changed in version 2.4: </span>Added the optional flags argument.</p>
 
677
</div>
 
678
</dd></dl>
 
679
 
 
680
<dl class="function">
 
681
<dt id="re.sub">
 
682
<code class="descclassname">re.</code><code class="descname">sub</code><span class="sig-paren">(</span><em>pattern</em>, <em>repl</em>, <em>string</em>, <em>count=0</em>, <em>flags=0</em><span class="sig-paren">)</span><a class="headerlink" href="#re.sub" title="Permalink to this definition">¶</a></dt>
 
683
<dd><p>Return the string obtained by replacing the leftmost non-overlapping occurrences
 
684
of <em>pattern</em> in <em>string</em> by the replacement <em>repl</em>.  If the pattern isn&#8217;t found,
 
685
<em>string</em> is returned unchanged.  <em>repl</em> can be a string or a function; if it is
 
686
a string, any backslash escapes in it are processed.  That is, <code class="docutils literal"><span class="pre">\n</span></code> is
 
687
converted to a single newline character, <code class="docutils literal"><span class="pre">\r</span></code> is converted to a carriage return, and
 
688
so forth.  Unknown escapes such as <code class="docutils literal"><span class="pre">\j</span></code> are left alone.  Backreferences, such
 
689
as <code class="docutils literal"><span class="pre">\6</span></code>, are replaced with the substring matched by group 6 in the pattern.
 
690
For example:</p>
 
691
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">re</span><span class="o">.</span><span class="n">sub</span><span class="p">(</span><span class="s1">r&#39;def\s+([a-zA-Z_][a-zA-Z_0-9]*)\s*\(\s*\):&#39;</span><span class="p">,</span>
 
692
<span class="gp">... </span>       <span class="s1">r&#39;static PyObject*\npy_\1(void)\n{&#39;</span><span class="p">,</span>
 
693
<span class="gp">... </span>       <span class="s1">&#39;def myfunc():&#39;</span><span class="p">)</span>
 
694
<span class="go">&#39;static PyObject*\npy_myfunc(void)\n{&#39;</span>
 
695
</pre></div>
 
696
</div>
 
697
<p>If <em>repl</em> is a function, it is called for every non-overlapping occurrence of
 
698
<em>pattern</em>.  The function takes a single match object argument, and returns the
 
699
replacement string.  For example:</p>
 
700
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="k">def</span> <span class="nf">dashrepl</span><span class="p">(</span><span class="n">matchobj</span><span class="p">):</span>
 
701
<span class="gp">... </span>    <span class="k">if</span> <span class="n">matchobj</span><span class="o">.</span><span class="n">group</span><span class="p">(</span><span class="mi">0</span><span class="p">)</span> <span class="o">==</span> <span class="s1">&#39;-&#39;</span><span class="p">:</span> <span class="k">return</span> <span class="s1">&#39; &#39;</span>
 
702
<span class="gp">... </span>    <span class="k">else</span><span class="p">:</span> <span class="k">return</span> <span class="s1">&#39;-&#39;</span>
 
703
<span class="gp">&gt;&gt;&gt; </span><span class="n">re</span><span class="o">.</span><span class="n">sub</span><span class="p">(</span><span class="s1">&#39;-{1,2}&#39;</span><span class="p">,</span> <span class="n">dashrepl</span><span class="p">,</span> <span class="s1">&#39;pro----gram-files&#39;</span><span class="p">)</span>
 
704
<span class="go">&#39;pro--gram files&#39;</span>
 
705
<span class="gp">&gt;&gt;&gt; </span><span class="n">re</span><span class="o">.</span><span class="n">sub</span><span class="p">(</span><span class="s1">r&#39;\sAND\s&#39;</span><span class="p">,</span> <span class="s1">&#39; &amp; &#39;</span><span class="p">,</span> <span class="s1">&#39;Baked Beans And Spam&#39;</span><span class="p">,</span> <span class="n">flags</span><span class="o">=</span><span class="n">re</span><span class="o">.</span><span class="n">IGNORECASE</span><span class="p">)</span>
 
706
<span class="go">&#39;Baked Beans &amp; Spam&#39;</span>
 
707
</pre></div>
 
708
</div>
 
709
<p>The pattern may be a string or an RE object.</p>
 
710
<p>The optional argument <em>count</em> is the maximum number of pattern occurrences to be
 
711
replaced; <em>count</em> must be a non-negative integer.  If omitted or zero, all
 
712
occurrences will be replaced. Empty matches for the pattern are replaced only
 
713
when not adjacent to a previous match, so <code class="docutils literal"><span class="pre">sub('x*',</span> <span class="pre">'-',</span> <span class="pre">'abc')</span></code> returns
 
714
<code class="docutils literal"><span class="pre">'-a-b-c-'</span></code>.</p>
 
715
<p>In string-type <em>repl</em> arguments, in addition to the character escapes and
 
716
backreferences described above,
 
717
<code class="docutils literal"><span class="pre">\g&lt;name&gt;</span></code> will use the substring matched by the group named <code class="docutils literal"><span class="pre">name</span></code>, as
 
718
defined by the <code class="docutils literal"><span class="pre">(?P&lt;name&gt;...)</span></code> syntax. <code class="docutils literal"><span class="pre">\g&lt;number&gt;</span></code> uses the corresponding
 
719
group number; <code class="docutils literal"><span class="pre">\g&lt;2&gt;</span></code> is therefore equivalent to <code class="docutils literal"><span class="pre">\2</span></code>, but isn&#8217;t ambiguous
 
720
in a replacement such as <code class="docutils literal"><span class="pre">\g&lt;2&gt;0</span></code>.  <code class="docutils literal"><span class="pre">\20</span></code> would be interpreted as a
 
721
reference to group 20, not a reference to group 2 followed by the literal
 
722
character <code class="docutils literal"><span class="pre">'0'</span></code>.  The backreference <code class="docutils literal"><span class="pre">\g&lt;0&gt;</span></code> substitutes in the entire
 
723
substring matched by the RE.</p>
 
724
<div class="versionchanged">
 
725
<p><span class="versionmodified">Changed in version 2.7: </span>Added the optional flags argument.</p>
 
726
</div>
 
727
</dd></dl>
 
728
 
 
729
<dl class="function">
 
730
<dt id="re.subn">
 
731
<code class="descclassname">re.</code><code class="descname">subn</code><span class="sig-paren">(</span><em>pattern</em>, <em>repl</em>, <em>string</em>, <em>count=0</em>, <em>flags=0</em><span class="sig-paren">)</span><a class="headerlink" href="#re.subn" title="Permalink to this definition">¶</a></dt>
 
732
<dd><p>Perform the same operation as <a class="reference internal" href="#re.sub" title="re.sub"><code class="xref py py-func docutils literal"><span class="pre">sub()</span></code></a>, but return a tuple <code class="docutils literal"><span class="pre">(new_string,</span>
 
733
<span class="pre">number_of_subs_made)</span></code>.</p>
 
734
<div class="versionchanged">
 
735
<p><span class="versionmodified">Changed in version 2.7: </span>Added the optional flags argument.</p>
 
736
</div>
 
737
</dd></dl>
 
738
 
 
739
<dl class="function">
 
740
<dt id="re.escape">
 
741
<code class="descclassname">re.</code><code class="descname">escape</code><span class="sig-paren">(</span><em>string</em><span class="sig-paren">)</span><a class="headerlink" href="#re.escape" title="Permalink to this definition">¶</a></dt>
 
742
<dd><p>Return <em>string</em> with all non-alphanumerics backslashed; this is useful if you
 
743
want to match an arbitrary literal string that may have regular expression
 
744
metacharacters in it.</p>
 
745
</dd></dl>
 
746
 
 
747
<dl class="function">
 
748
<dt id="re.purge">
 
749
<code class="descclassname">re.</code><code class="descname">purge</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#re.purge" title="Permalink to this definition">¶</a></dt>
 
750
<dd><p>Clear the regular expression cache.</p>
 
751
</dd></dl>
 
752
 
 
753
<dl class="exception">
 
754
<dt id="re.error">
 
755
<em class="property">exception </em><code class="descclassname">re.</code><code class="descname">error</code><a class="headerlink" href="#re.error" title="Permalink to this definition">¶</a></dt>
 
756
<dd><p>Exception raised when a string passed to one of the functions here is not a
 
757
valid regular expression (for example, it might contain unmatched parentheses)
 
758
or when some other error occurs during compilation or matching.  It is never an
 
759
error if a string contains no match for a pattern.</p>
 
760
</dd></dl>
 
761
 
 
762
</div>
 
763
<div class="section" id="regular-expression-objects">
 
764
<span id="re-objects"></span><h2>7.2.3. Regular Expression Objects<a class="headerlink" href="#regular-expression-objects" title="Permalink to this headline">¶</a></h2>
 
765
<dl class="class">
 
766
<dt id="re.RegexObject">
 
767
<em class="property">class </em><code class="descclassname">re.</code><code class="descname">RegexObject</code><a class="headerlink" href="#re.RegexObject" title="Permalink to this definition">¶</a></dt>
 
768
<dd><p>The <a class="reference internal" href="#re.RegexObject" title="re.RegexObject"><code class="xref py py-class docutils literal"><span class="pre">RegexObject</span></code></a> class supports the following methods and attributes:</p>
 
769
<dl class="method">
 
770
<dt id="re.RegexObject.search">
 
771
<code class="descname">search</code><span class="sig-paren">(</span><em>string</em><span class="optional">[</span>, <em>pos</em><span class="optional">[</span>, <em>endpos</em><span class="optional">]</span><span class="optional">]</span><span class="sig-paren">)</span><a class="headerlink" href="#re.RegexObject.search" title="Permalink to this definition">¶</a></dt>
 
772
<dd><p>Scan through <em>string</em> looking for a location where this regular expression
 
773
produces a match, and return a corresponding <a class="reference internal" href="#re.MatchObject" title="re.MatchObject"><code class="xref py py-class docutils literal"><span class="pre">MatchObject</span></code></a> instance.
 
774
Return <code class="docutils literal"><span class="pre">None</span></code> if no position in the string matches the pattern; note that this
 
775
is different from finding a zero-length match at some point in the string.</p>
 
776
<p>The optional second parameter <em>pos</em> gives an index in the string where the
 
777
search is to start; it defaults to <code class="docutils literal"><span class="pre">0</span></code>.  This is not completely equivalent to
 
778
slicing the string; the <code class="docutils literal"><span class="pre">'^'</span></code> pattern character matches at the real beginning
 
779
of the string and at positions just after a newline, but not necessarily at the
 
780
index where the search is to start.</p>
 
781
<p>The optional parameter <em>endpos</em> limits how far the string will be searched; it
 
782
will be as if the string is <em>endpos</em> characters long, so only the characters
 
783
from <em>pos</em> to <code class="docutils literal"><span class="pre">endpos</span> <span class="pre">-</span> <span class="pre">1</span></code> will be searched for a match.  If <em>endpos</em> is less
 
784
than <em>pos</em>, no match will be found, otherwise, if <em>rx</em> is a compiled regular
 
785
expression object, <code class="docutils literal"><span class="pre">rx.search(string,</span> <span class="pre">0,</span> <span class="pre">50)</span></code> is equivalent to
 
786
<code class="docutils literal"><span class="pre">rx.search(string[:50],</span> <span class="pre">0)</span></code>.</p>
 
787
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">pattern</span> <span class="o">=</span> <span class="n">re</span><span class="o">.</span><span class="n">compile</span><span class="p">(</span><span class="s2">&quot;d&quot;</span><span class="p">)</span>
 
788
<span class="gp">&gt;&gt;&gt; </span><span class="n">pattern</span><span class="o">.</span><span class="n">search</span><span class="p">(</span><span class="s2">&quot;dog&quot;</span><span class="p">)</span>     <span class="c1"># Match at index 0</span>
 
789
<span class="go">&lt;_sre.SRE_Match object at ...&gt;</span>
 
790
<span class="gp">&gt;&gt;&gt; </span><span class="n">pattern</span><span class="o">.</span><span class="n">search</span><span class="p">(</span><span class="s2">&quot;dog&quot;</span><span class="p">,</span> <span class="mi">1</span><span class="p">)</span>  <span class="c1"># No match; search doesn&#39;t include the &quot;d&quot;</span>
 
791
</pre></div>
 
792
</div>
 
793
</dd></dl>
 
794
 
 
795
<dl class="method">
 
796
<dt id="re.RegexObject.match">
 
797
<code class="descname">match</code><span class="sig-paren">(</span><em>string</em><span class="optional">[</span>, <em>pos</em><span class="optional">[</span>, <em>endpos</em><span class="optional">]</span><span class="optional">]</span><span class="sig-paren">)</span><a class="headerlink" href="#re.RegexObject.match" title="Permalink to this definition">¶</a></dt>
 
798
<dd><p>If zero or more characters at the <em>beginning</em> of <em>string</em> match this regular
 
799
expression, return a corresponding <a class="reference internal" href="#re.MatchObject" title="re.MatchObject"><code class="xref py py-class docutils literal"><span class="pre">MatchObject</span></code></a> instance.  Return
 
800
<code class="docutils literal"><span class="pre">None</span></code> if the string does not match the pattern; note that this is different
 
801
from a zero-length match.</p>
 
802
<p>The optional <em>pos</em> and <em>endpos</em> parameters have the same meaning as for the
 
803
<a class="reference internal" href="#re.RegexObject.search" title="re.RegexObject.search"><code class="xref py py-meth docutils literal"><span class="pre">search()</span></code></a> method.</p>
 
804
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">pattern</span> <span class="o">=</span> <span class="n">re</span><span class="o">.</span><span class="n">compile</span><span class="p">(</span><span class="s2">&quot;o&quot;</span><span class="p">)</span>
 
805
<span class="gp">&gt;&gt;&gt; </span><span class="n">pattern</span><span class="o">.</span><span class="n">match</span><span class="p">(</span><span class="s2">&quot;dog&quot;</span><span class="p">)</span>      <span class="c1"># No match as &quot;o&quot; is not at the start of &quot;dog&quot;.</span>
 
806
<span class="gp">&gt;&gt;&gt; </span><span class="n">pattern</span><span class="o">.</span><span class="n">match</span><span class="p">(</span><span class="s2">&quot;dog&quot;</span><span class="p">,</span> <span class="mi">1</span><span class="p">)</span>   <span class="c1"># Match as &quot;o&quot; is the 2nd character of &quot;dog&quot;.</span>
 
807
<span class="go">&lt;_sre.SRE_Match object at ...&gt;</span>
 
808
</pre></div>
 
809
</div>
 
810
<p>If you want to locate a match anywhere in <em>string</em>, use
 
811
<a class="reference internal" href="#re.RegexObject.search" title="re.RegexObject.search"><code class="xref py py-meth docutils literal"><span class="pre">search()</span></code></a> instead (see also <a class="reference internal" href="#search-vs-match"><span>search() vs. match()</span></a>).</p>
 
812
</dd></dl>
 
813
 
 
814
<dl class="method">
 
815
<dt id="re.RegexObject.split">
 
816
<code class="descname">split</code><span class="sig-paren">(</span><em>string</em>, <em>maxsplit=0</em><span class="sig-paren">)</span><a class="headerlink" href="#re.RegexObject.split" title="Permalink to this definition">¶</a></dt>
 
817
<dd><p>Identical to the <a class="reference internal" href="#re.split" title="re.split"><code class="xref py py-func docutils literal"><span class="pre">split()</span></code></a> function, using the compiled pattern.</p>
 
818
</dd></dl>
 
819
 
 
820
<dl class="method">
 
821
<dt id="re.RegexObject.findall">
 
822
<code class="descname">findall</code><span class="sig-paren">(</span><em>string</em><span class="optional">[</span>, <em>pos</em><span class="optional">[</span>, <em>endpos</em><span class="optional">]</span><span class="optional">]</span><span class="sig-paren">)</span><a class="headerlink" href="#re.RegexObject.findall" title="Permalink to this definition">¶</a></dt>
 
823
<dd><p>Similar to the <a class="reference internal" href="#re.findall" title="re.findall"><code class="xref py py-func docutils literal"><span class="pre">findall()</span></code></a> function, using the compiled pattern, but
 
824
also accepts optional <em>pos</em> and <em>endpos</em> parameters that limit the search
 
825
region like for <a class="reference internal" href="#re.match" title="re.match"><code class="xref py py-meth docutils literal"><span class="pre">match()</span></code></a>.</p>
 
826
</dd></dl>
 
827
 
 
828
<dl class="method">
 
829
<dt id="re.RegexObject.finditer">
 
830
<code class="descname">finditer</code><span class="sig-paren">(</span><em>string</em><span class="optional">[</span>, <em>pos</em><span class="optional">[</span>, <em>endpos</em><span class="optional">]</span><span class="optional">]</span><span class="sig-paren">)</span><a class="headerlink" href="#re.RegexObject.finditer" title="Permalink to this definition">¶</a></dt>
 
831
<dd><p>Similar to the <a class="reference internal" href="#re.finditer" title="re.finditer"><code class="xref py py-func docutils literal"><span class="pre">finditer()</span></code></a> function, using the compiled pattern, but
 
832
also accepts optional <em>pos</em> and <em>endpos</em> parameters that limit the search
 
833
region like for <a class="reference internal" href="#re.match" title="re.match"><code class="xref py py-meth docutils literal"><span class="pre">match()</span></code></a>.</p>
 
834
</dd></dl>
 
835
 
 
836
<dl class="method">
 
837
<dt id="re.RegexObject.sub">
 
838
<code class="descname">sub</code><span class="sig-paren">(</span><em>repl</em>, <em>string</em>, <em>count=0</em><span class="sig-paren">)</span><a class="headerlink" href="#re.RegexObject.sub" title="Permalink to this definition">¶</a></dt>
 
839
<dd><p>Identical to the <a class="reference internal" href="#re.sub" title="re.sub"><code class="xref py py-func docutils literal"><span class="pre">sub()</span></code></a> function, using the compiled pattern.</p>
 
840
</dd></dl>
 
841
 
 
842
<dl class="method">
 
843
<dt id="re.RegexObject.subn">
 
844
<code class="descname">subn</code><span class="sig-paren">(</span><em>repl</em>, <em>string</em>, <em>count=0</em><span class="sig-paren">)</span><a class="headerlink" href="#re.RegexObject.subn" title="Permalink to this definition">¶</a></dt>
 
845
<dd><p>Identical to the <a class="reference internal" href="#re.subn" title="re.subn"><code class="xref py py-func docutils literal"><span class="pre">subn()</span></code></a> function, using the compiled pattern.</p>
 
846
</dd></dl>
 
847
 
 
848
<dl class="attribute">
 
849
<dt id="re.RegexObject.flags">
 
850
<code class="descname">flags</code><a class="headerlink" href="#re.RegexObject.flags" title="Permalink to this definition">¶</a></dt>
 
851
<dd><p>The regex matching flags.  This is a combination of the flags given to
 
852
<a class="reference internal" href="#re.compile" title="re.compile"><code class="xref py py-func docutils literal"><span class="pre">compile()</span></code></a> and any <code class="docutils literal"><span class="pre">(?...)</span></code> inline flags in the pattern.</p>
 
853
</dd></dl>
 
854
 
 
855
<dl class="attribute">
 
856
<dt id="re.RegexObject.groups">
 
857
<code class="descname">groups</code><a class="headerlink" href="#re.RegexObject.groups" title="Permalink to this definition">¶</a></dt>
 
858
<dd><p>The number of capturing groups in the pattern.</p>
 
859
</dd></dl>
 
860
 
 
861
<dl class="attribute">
 
862
<dt id="re.RegexObject.groupindex">
 
863
<code class="descname">groupindex</code><a class="headerlink" href="#re.RegexObject.groupindex" title="Permalink to this definition">¶</a></dt>
 
864
<dd><p>A dictionary mapping any symbolic group names defined by <code class="docutils literal"><span class="pre">(?P&lt;id&gt;)</span></code> to group
 
865
numbers.  The dictionary is empty if no symbolic groups were used in the
 
866
pattern.</p>
 
867
</dd></dl>
 
868
 
 
869
<dl class="attribute">
 
870
<dt id="re.RegexObject.pattern">
 
871
<code class="descname">pattern</code><a class="headerlink" href="#re.RegexObject.pattern" title="Permalink to this definition">¶</a></dt>
 
872
<dd><p>The pattern string from which the RE object was compiled.</p>
 
873
</dd></dl>
 
874
 
 
875
</dd></dl>
 
876
 
 
877
</div>
 
878
<div class="section" id="match-objects">
 
879
<span id="id1"></span><h2>7.2.4. Match Objects<a class="headerlink" href="#match-objects" title="Permalink to this headline">¶</a></h2>
 
880
<dl class="class">
 
881
<dt id="re.MatchObject">
 
882
<em class="property">class </em><code class="descclassname">re.</code><code class="descname">MatchObject</code><a class="headerlink" href="#re.MatchObject" title="Permalink to this definition">¶</a></dt>
 
883
<dd><p>Match objects always have a boolean value of <code class="docutils literal"><span class="pre">True</span></code>.
 
884
Since <code class="xref py py-meth docutils literal"><span class="pre">match()</span></code> and <code class="xref py py-meth docutils literal"><span class="pre">search()</span></code> return <code class="docutils literal"><span class="pre">None</span></code>
 
885
when there is no match, you can test whether there was a match with a simple
 
886
<code class="docutils literal"><span class="pre">if</span></code> statement:</p>
 
887
<div class="highlight-python"><div class="highlight"><pre><span class="n">match</span> <span class="o">=</span> <span class="n">re</span><span class="o">.</span><span class="n">search</span><span class="p">(</span><span class="n">pattern</span><span class="p">,</span> <span class="n">string</span><span class="p">)</span>
 
888
<span class="k">if</span> <span class="n">match</span><span class="p">:</span>
 
889
    <span class="n">process</span><span class="p">(</span><span class="n">match</span><span class="p">)</span>
 
890
</pre></div>
 
891
</div>
 
892
<p>Match objects support the following methods and attributes:</p>
 
893
<dl class="method">
 
894
<dt id="re.MatchObject.expand">
 
895
<code class="descname">expand</code><span class="sig-paren">(</span><em>template</em><span class="sig-paren">)</span><a class="headerlink" href="#re.MatchObject.expand" title="Permalink to this definition">¶</a></dt>
 
896
<dd><p>Return the string obtained by doing backslash substitution on the template
 
897
string <em>template</em>, as done by the <a class="reference internal" href="#re.RegexObject.sub" title="re.RegexObject.sub"><code class="xref py py-meth docutils literal"><span class="pre">sub()</span></code></a> method.  Escapes
 
898
such as <code class="docutils literal"><span class="pre">\n</span></code> are converted to the appropriate characters, and numeric
 
899
backreferences (<code class="docutils literal"><span class="pre">\1</span></code>, <code class="docutils literal"><span class="pre">\2</span></code>) and named backreferences (<code class="docutils literal"><span class="pre">\g&lt;1&gt;</span></code>,
 
900
<code class="docutils literal"><span class="pre">\g&lt;name&gt;</span></code>) are replaced by the contents of the corresponding group.</p>
 
901
</dd></dl>
 
902
 
 
903
<dl class="method">
 
904
<dt id="re.MatchObject.group">
 
905
<code class="descname">group</code><span class="sig-paren">(</span><span class="optional">[</span><em>group1</em>, <em>...</em><span class="optional">]</span><span class="sig-paren">)</span><a class="headerlink" href="#re.MatchObject.group" title="Permalink to this definition">¶</a></dt>
 
906
<dd><p>Returns one or more subgroups of the match.  If there is a single argument, the
 
907
result is a single string; if there are multiple arguments, the result is a
 
908
tuple with one item per argument. Without arguments, <em>group1</em> defaults to zero
 
909
(the whole match is returned). If a <em>groupN</em> argument is zero, the corresponding
 
910
return value is the entire matching string; if it is in the inclusive range
 
911
[1..99], it is the string matching the corresponding parenthesized group.  If a
 
912
group number is negative or larger than the number of groups defined in the
 
913
pattern, an <a class="reference internal" href="exceptions.html#exceptions.IndexError" title="exceptions.IndexError"><code class="xref py py-exc docutils literal"><span class="pre">IndexError</span></code></a> exception is raised. If a group is contained in a
 
914
part of the pattern that did not match, the corresponding result is <code class="docutils literal"><span class="pre">None</span></code>.
 
915
If a group is contained in a part of the pattern that matched multiple times,
 
916
the last match is returned.</p>
 
917
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">m</span> <span class="o">=</span> <span class="n">re</span><span class="o">.</span><span class="n">match</span><span class="p">(</span><span class="s2">r&quot;(\w+) (\w+)&quot;</span><span class="p">,</span> <span class="s2">&quot;Isaac Newton, physicist&quot;</span><span class="p">)</span>
 
918
<span class="gp">&gt;&gt;&gt; </span><span class="n">m</span><span class="o">.</span><span class="n">group</span><span class="p">(</span><span class="mi">0</span><span class="p">)</span>       <span class="c1"># The entire match</span>
 
919
<span class="go">&#39;Isaac Newton&#39;</span>
 
920
<span class="gp">&gt;&gt;&gt; </span><span class="n">m</span><span class="o">.</span><span class="n">group</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span>       <span class="c1"># The first parenthesized subgroup.</span>
 
921
<span class="go">&#39;Isaac&#39;</span>
 
922
<span class="gp">&gt;&gt;&gt; </span><span class="n">m</span><span class="o">.</span><span class="n">group</span><span class="p">(</span><span class="mi">2</span><span class="p">)</span>       <span class="c1"># The second parenthesized subgroup.</span>
 
923
<span class="go">&#39;Newton&#39;</span>
 
924
<span class="gp">&gt;&gt;&gt; </span><span class="n">m</span><span class="o">.</span><span class="n">group</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">)</span>    <span class="c1"># Multiple arguments give us a tuple.</span>
 
925
<span class="go">(&#39;Isaac&#39;, &#39;Newton&#39;)</span>
 
926
</pre></div>
 
927
</div>
 
928
<p>If the regular expression uses the <code class="docutils literal"><span class="pre">(?P&lt;name&gt;...)</span></code> syntax, the <em>groupN</em>
 
929
arguments may also be strings identifying groups by their group name.  If a
 
930
string argument is not used as a group name in the pattern, an <a class="reference internal" href="exceptions.html#exceptions.IndexError" title="exceptions.IndexError"><code class="xref py py-exc docutils literal"><span class="pre">IndexError</span></code></a>
 
931
exception is raised.</p>
 
932
<p>A moderately complicated example:</p>
 
933
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">m</span> <span class="o">=</span> <span class="n">re</span><span class="o">.</span><span class="n">match</span><span class="p">(</span><span class="s2">r&quot;(?P&lt;first_name&gt;\w+) (?P&lt;last_name&gt;\w+)&quot;</span><span class="p">,</span> <span class="s2">&quot;Malcolm Reynolds&quot;</span><span class="p">)</span>
 
934
<span class="gp">&gt;&gt;&gt; </span><span class="n">m</span><span class="o">.</span><span class="n">group</span><span class="p">(</span><span class="s1">&#39;first_name&#39;</span><span class="p">)</span>
 
935
<span class="go">&#39;Malcolm&#39;</span>
 
936
<span class="gp">&gt;&gt;&gt; </span><span class="n">m</span><span class="o">.</span><span class="n">group</span><span class="p">(</span><span class="s1">&#39;last_name&#39;</span><span class="p">)</span>
 
937
<span class="go">&#39;Reynolds&#39;</span>
 
938
</pre></div>
 
939
</div>
 
940
<p>Named groups can also be referred to by their index:</p>
 
941
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">m</span><span class="o">.</span><span class="n">group</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span>
 
942
<span class="go">&#39;Malcolm&#39;</span>
 
943
<span class="gp">&gt;&gt;&gt; </span><span class="n">m</span><span class="o">.</span><span class="n">group</span><span class="p">(</span><span class="mi">2</span><span class="p">)</span>
 
944
<span class="go">&#39;Reynolds&#39;</span>
 
945
</pre></div>
 
946
</div>
 
947
<p>If a group matches multiple times, only the last match is accessible:</p>
 
948
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">m</span> <span class="o">=</span> <span class="n">re</span><span class="o">.</span><span class="n">match</span><span class="p">(</span><span class="s2">r&quot;(..)+&quot;</span><span class="p">,</span> <span class="s2">&quot;a1b2c3&quot;</span><span class="p">)</span>  <span class="c1"># Matches 3 times.</span>
 
949
<span class="gp">&gt;&gt;&gt; </span><span class="n">m</span><span class="o">.</span><span class="n">group</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span>                        <span class="c1"># Returns only the last match.</span>
 
950
<span class="go">&#39;c3&#39;</span>
 
951
</pre></div>
 
952
</div>
 
953
</dd></dl>
 
954
 
 
955
<dl class="method">
 
956
<dt id="re.MatchObject.groups">
 
957
<code class="descname">groups</code><span class="sig-paren">(</span><span class="optional">[</span><em>default</em><span class="optional">]</span><span class="sig-paren">)</span><a class="headerlink" href="#re.MatchObject.groups" title="Permalink to this definition">¶</a></dt>
 
958
<dd><p>Return a tuple containing all the subgroups of the match, from 1 up to however
 
959
many groups are in the pattern.  The <em>default</em> argument is used for groups that
 
960
did not participate in the match; it defaults to <code class="docutils literal"><span class="pre">None</span></code>.  (Incompatibility
 
961
note: in the original Python 1.5 release, if the tuple was one element long, a
 
962
string would be returned instead.  In later versions (from 1.5.1 on), a
 
963
singleton tuple is returned in such cases.)</p>
 
964
<p>For example:</p>
 
965
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">m</span> <span class="o">=</span> <span class="n">re</span><span class="o">.</span><span class="n">match</span><span class="p">(</span><span class="s2">r&quot;(\d+)\.(\d+)&quot;</span><span class="p">,</span> <span class="s2">&quot;24.1632&quot;</span><span class="p">)</span>
 
966
<span class="gp">&gt;&gt;&gt; </span><span class="n">m</span><span class="o">.</span><span class="n">groups</span><span class="p">()</span>
 
967
<span class="go">(&#39;24&#39;, &#39;1632&#39;)</span>
 
968
</pre></div>
 
969
</div>
 
970
<p>If we make the decimal place and everything after it optional, not all groups
 
971
might participate in the match.  These groups will default to <code class="docutils literal"><span class="pre">None</span></code> unless
 
972
the <em>default</em> argument is given:</p>
 
973
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">m</span> <span class="o">=</span> <span class="n">re</span><span class="o">.</span><span class="n">match</span><span class="p">(</span><span class="s2">r&quot;(\d+)\.?(\d+)?&quot;</span><span class="p">,</span> <span class="s2">&quot;24&quot;</span><span class="p">)</span>
 
974
<span class="gp">&gt;&gt;&gt; </span><span class="n">m</span><span class="o">.</span><span class="n">groups</span><span class="p">()</span>      <span class="c1"># Second group defaults to None.</span>
 
975
<span class="go">(&#39;24&#39;, None)</span>
 
976
<span class="gp">&gt;&gt;&gt; </span><span class="n">m</span><span class="o">.</span><span class="n">groups</span><span class="p">(</span><span class="s1">&#39;0&#39;</span><span class="p">)</span>   <span class="c1"># Now, the second group defaults to &#39;0&#39;.</span>
 
977
<span class="go">(&#39;24&#39;, &#39;0&#39;)</span>
 
978
</pre></div>
 
979
</div>
 
980
</dd></dl>
 
981
 
 
982
<dl class="method">
 
983
<dt id="re.MatchObject.groupdict">
 
984
<code class="descname">groupdict</code><span class="sig-paren">(</span><span class="optional">[</span><em>default</em><span class="optional">]</span><span class="sig-paren">)</span><a class="headerlink" href="#re.MatchObject.groupdict" title="Permalink to this definition">¶</a></dt>
 
985
<dd><p>Return a dictionary containing all the <em>named</em> subgroups of the match, keyed by
 
986
the subgroup name.  The <em>default</em> argument is used for groups that did not
 
987
participate in the match; it defaults to <code class="docutils literal"><span class="pre">None</span></code>.  For example:</p>
 
988
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">m</span> <span class="o">=</span> <span class="n">re</span><span class="o">.</span><span class="n">match</span><span class="p">(</span><span class="s2">r&quot;(?P&lt;first_name&gt;\w+) (?P&lt;last_name&gt;\w+)&quot;</span><span class="p">,</span> <span class="s2">&quot;Malcolm Reynolds&quot;</span><span class="p">)</span>
 
989
<span class="gp">&gt;&gt;&gt; </span><span class="n">m</span><span class="o">.</span><span class="n">groupdict</span><span class="p">()</span>
 
990
<span class="go">{&#39;first_name&#39;: &#39;Malcolm&#39;, &#39;last_name&#39;: &#39;Reynolds&#39;}</span>
 
991
</pre></div>
 
992
</div>
 
993
</dd></dl>
 
994
 
 
995
<dl class="method">
 
996
<dt id="re.MatchObject.start">
 
997
<code class="descname">start</code><span class="sig-paren">(</span><span class="optional">[</span><em>group</em><span class="optional">]</span><span class="sig-paren">)</span><a class="headerlink" href="#re.MatchObject.start" title="Permalink to this definition">¶</a></dt>
 
998
<dt id="re.MatchObject.end">
 
999
<code class="descname">end</code><span class="sig-paren">(</span><span class="optional">[</span><em>group</em><span class="optional">]</span><span class="sig-paren">)</span><a class="headerlink" href="#re.MatchObject.end" title="Permalink to this definition">¶</a></dt>
 
1000
<dd><p>Return the indices of the start and end of the substring matched by <em>group</em>;
 
1001
<em>group</em> defaults to zero (meaning the whole matched substring). Return <code class="docutils literal"><span class="pre">-1</span></code> if
 
1002
<em>group</em> exists but did not contribute to the match.  For a match object <em>m</em>, and
 
1003
a group <em>g</em> that did contribute to the match, the substring matched by group <em>g</em>
 
1004
(equivalent to <code class="docutils literal"><span class="pre">m.group(g)</span></code>) is</p>
 
1005
<div class="highlight-python"><div class="highlight"><pre><span class="n">m</span><span class="o">.</span><span class="n">string</span><span class="p">[</span><span class="n">m</span><span class="o">.</span><span class="n">start</span><span class="p">(</span><span class="n">g</span><span class="p">):</span><span class="n">m</span><span class="o">.</span><span class="n">end</span><span class="p">(</span><span class="n">g</span><span class="p">)]</span>
 
1006
</pre></div>
 
1007
</div>
 
1008
<p>Note that <code class="docutils literal"><span class="pre">m.start(group)</span></code> will equal <code class="docutils literal"><span class="pre">m.end(group)</span></code> if <em>group</em> matched a
 
1009
null string.  For example, after <code class="docutils literal"><span class="pre">m</span> <span class="pre">=</span> <span class="pre">re.search('b(c?)',</span> <span class="pre">'cba')</span></code>,
 
1010
<code class="docutils literal"><span class="pre">m.start(0)</span></code> is 1, <code class="docutils literal"><span class="pre">m.end(0)</span></code> is 2, <code class="docutils literal"><span class="pre">m.start(1)</span></code> and <code class="docutils literal"><span class="pre">m.end(1)</span></code> are both
 
1011
2, and <code class="docutils literal"><span class="pre">m.start(2)</span></code> raises an <a class="reference internal" href="exceptions.html#exceptions.IndexError" title="exceptions.IndexError"><code class="xref py py-exc docutils literal"><span class="pre">IndexError</span></code></a> exception.</p>
 
1012
<p>An example that will remove <em>remove_this</em> from email addresses:</p>
 
1013
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">email</span> <span class="o">=</span> <span class="s2">&quot;tony@tiremove_thisger.net&quot;</span>
 
1014
<span class="gp">&gt;&gt;&gt; </span><span class="n">m</span> <span class="o">=</span> <span class="n">re</span><span class="o">.</span><span class="n">search</span><span class="p">(</span><span class="s2">&quot;remove_this&quot;</span><span class="p">,</span> <span class="n">email</span><span class="p">)</span>
 
1015
<span class="gp">&gt;&gt;&gt; </span><span class="n">email</span><span class="p">[:</span><span class="n">m</span><span class="o">.</span><span class="n">start</span><span class="p">()]</span> <span class="o">+</span> <span class="n">email</span><span class="p">[</span><span class="n">m</span><span class="o">.</span><span class="n">end</span><span class="p">():]</span>
 
1016
<span class="go">&#39;tony@tiger.net&#39;</span>
 
1017
</pre></div>
 
1018
</div>
 
1019
</dd></dl>
 
1020
 
 
1021
<dl class="method">
 
1022
<dt id="re.MatchObject.span">
 
1023
<code class="descname">span</code><span class="sig-paren">(</span><span class="optional">[</span><em>group</em><span class="optional">]</span><span class="sig-paren">)</span><a class="headerlink" href="#re.MatchObject.span" title="Permalink to this definition">¶</a></dt>
 
1024
<dd><p>For <a class="reference internal" href="#re.MatchObject" title="re.MatchObject"><code class="xref py py-class docutils literal"><span class="pre">MatchObject</span></code></a> <em>m</em>, return the 2-tuple <code class="docutils literal"><span class="pre">(m.start(group),</span>
 
1025
<span class="pre">m.end(group))</span></code>. Note that if <em>group</em> did not contribute to the match, this is
 
1026
<code class="docutils literal"><span class="pre">(-1,</span> <span class="pre">-1)</span></code>.  <em>group</em> defaults to zero, the entire match.</p>
 
1027
</dd></dl>
 
1028
 
 
1029
<dl class="attribute">
 
1030
<dt id="re.MatchObject.pos">
 
1031
<code class="descname">pos</code><a class="headerlink" href="#re.MatchObject.pos" title="Permalink to this definition">¶</a></dt>
 
1032
<dd><p>The value of <em>pos</em> which was passed to the <a class="reference internal" href="#re.RegexObject.search" title="re.RegexObject.search"><code class="xref py py-meth docutils literal"><span class="pre">search()</span></code></a> or
 
1033
<a class="reference internal" href="#re.RegexObject.match" title="re.RegexObject.match"><code class="xref py py-meth docutils literal"><span class="pre">match()</span></code></a> method of the <a class="reference internal" href="#re.RegexObject" title="re.RegexObject"><code class="xref py py-class docutils literal"><span class="pre">RegexObject</span></code></a>.  This is the
 
1034
index into the string at which the RE engine started looking for a match.</p>
 
1035
</dd></dl>
 
1036
 
 
1037
<dl class="attribute">
 
1038
<dt id="re.MatchObject.endpos">
 
1039
<code class="descname">endpos</code><a class="headerlink" href="#re.MatchObject.endpos" title="Permalink to this definition">¶</a></dt>
 
1040
<dd><p>The value of <em>endpos</em> which was passed to the <a class="reference internal" href="#re.RegexObject.search" title="re.RegexObject.search"><code class="xref py py-meth docutils literal"><span class="pre">search()</span></code></a> or
 
1041
<a class="reference internal" href="#re.RegexObject.match" title="re.RegexObject.match"><code class="xref py py-meth docutils literal"><span class="pre">match()</span></code></a> method of the <a class="reference internal" href="#re.RegexObject" title="re.RegexObject"><code class="xref py py-class docutils literal"><span class="pre">RegexObject</span></code></a>.  This is the
 
1042
index into the string beyond which the RE engine will not go.</p>
 
1043
</dd></dl>
 
1044
 
 
1045
<dl class="attribute">
 
1046
<dt id="re.MatchObject.lastindex">
 
1047
<code class="descname">lastindex</code><a class="headerlink" href="#re.MatchObject.lastindex" title="Permalink to this definition">¶</a></dt>
 
1048
<dd><p>The integer index of the last matched capturing group, or <code class="docutils literal"><span class="pre">None</span></code> if no group
 
1049
was matched at all. For example, the expressions <code class="docutils literal"><span class="pre">(a)b</span></code>, <code class="docutils literal"><span class="pre">((a)(b))</span></code>, and
 
1050
<code class="docutils literal"><span class="pre">((ab))</span></code> will have <code class="docutils literal"><span class="pre">lastindex</span> <span class="pre">==</span> <span class="pre">1</span></code> if applied to the string <code class="docutils literal"><span class="pre">'ab'</span></code>, while
 
1051
the expression <code class="docutils literal"><span class="pre">(a)(b)</span></code> will have <code class="docutils literal"><span class="pre">lastindex</span> <span class="pre">==</span> <span class="pre">2</span></code>, if applied to the same
 
1052
string.</p>
 
1053
</dd></dl>
 
1054
 
 
1055
<dl class="attribute">
 
1056
<dt id="re.MatchObject.lastgroup">
 
1057
<code class="descname">lastgroup</code><a class="headerlink" href="#re.MatchObject.lastgroup" title="Permalink to this definition">¶</a></dt>
 
1058
<dd><p>The name of the last matched capturing group, or <code class="docutils literal"><span class="pre">None</span></code> if the group didn&#8217;t
 
1059
have a name, or if no group was matched at all.</p>
 
1060
</dd></dl>
 
1061
 
 
1062
<dl class="attribute">
 
1063
<dt id="re.MatchObject.re">
 
1064
<code class="descname">re</code><a class="headerlink" href="#re.MatchObject.re" title="Permalink to this definition">¶</a></dt>
 
1065
<dd><p>The regular expression object whose <a class="reference internal" href="#re.RegexObject.match" title="re.RegexObject.match"><code class="xref py py-meth docutils literal"><span class="pre">match()</span></code></a> or
 
1066
<a class="reference internal" href="#re.RegexObject.search" title="re.RegexObject.search"><code class="xref py py-meth docutils literal"><span class="pre">search()</span></code></a> method produced this <a class="reference internal" href="#re.MatchObject" title="re.MatchObject"><code class="xref py py-class docutils literal"><span class="pre">MatchObject</span></code></a>
 
1067
instance.</p>
 
1068
</dd></dl>
 
1069
 
 
1070
<dl class="attribute">
 
1071
<dt id="re.MatchObject.string">
 
1072
<code class="descname">string</code><a class="headerlink" href="#re.MatchObject.string" title="Permalink to this definition">¶</a></dt>
 
1073
<dd><p>The string passed to <a class="reference internal" href="#re.RegexObject.match" title="re.RegexObject.match"><code class="xref py py-meth docutils literal"><span class="pre">match()</span></code></a> or
 
1074
<a class="reference internal" href="#re.RegexObject.search" title="re.RegexObject.search"><code class="xref py py-meth docutils literal"><span class="pre">search()</span></code></a>.</p>
 
1075
</dd></dl>
 
1076
 
 
1077
</dd></dl>
 
1078
 
 
1079
</div>
 
1080
<div class="section" id="examples">
 
1081
<h2>7.2.5. Examples<a class="headerlink" href="#examples" title="Permalink to this headline">¶</a></h2>
 
1082
<div class="section" id="checking-for-a-pair">
 
1083
<h3>7.2.5.1. Checking For a Pair<a class="headerlink" href="#checking-for-a-pair" title="Permalink to this headline">¶</a></h3>
 
1084
<p>In this example, we&#8217;ll use the following helper function to display match
 
1085
objects a little more gracefully:</p>
 
1086
<div class="highlight-python"><div class="highlight"><pre><span class="k">def</span> <span class="nf">displaymatch</span><span class="p">(</span><span class="n">match</span><span class="p">):</span>
 
1087
    <span class="k">if</span> <span class="n">match</span> <span class="ow">is</span> <span class="bp">None</span><span class="p">:</span>
 
1088
        <span class="k">return</span> <span class="bp">None</span>
 
1089
    <span class="k">return</span> <span class="s1">&#39;&lt;Match: </span><span class="si">%r</span><span class="s1">, groups=</span><span class="si">%r</span><span class="s1">&gt;&#39;</span> <span class="o">%</span> <span class="p">(</span><span class="n">match</span><span class="o">.</span><span class="n">group</span><span class="p">(),</span> <span class="n">match</span><span class="o">.</span><span class="n">groups</span><span class="p">())</span>
 
1090
</pre></div>
 
1091
</div>
 
1092
<p>Suppose you are writing a poker program where a player&#8217;s hand is represented as
 
1093
a 5-character string with each character representing a card, &#8220;a&#8221; for ace, &#8220;k&#8221;
 
1094
for king, &#8220;q&#8221; for queen, &#8220;j&#8221; for jack, &#8220;t&#8221; for 10, and &#8220;2&#8221; through &#8220;9&#8221;
 
1095
representing the card with that value.</p>
 
1096
<p>To see if a given string is a valid hand, one could do the following:</p>
 
1097
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">valid</span> <span class="o">=</span> <span class="n">re</span><span class="o">.</span><span class="n">compile</span><span class="p">(</span><span class="s2">r&quot;^[a2-9tjqk]{5}$&quot;</span><span class="p">)</span>
 
1098
<span class="gp">&gt;&gt;&gt; </span><span class="n">displaymatch</span><span class="p">(</span><span class="n">valid</span><span class="o">.</span><span class="n">match</span><span class="p">(</span><span class="s2">&quot;akt5q&quot;</span><span class="p">))</span>  <span class="c1"># Valid.</span>
 
1099
<span class="go">&quot;&lt;Match: &#39;akt5q&#39;, groups=()&gt;&quot;</span>
 
1100
<span class="gp">&gt;&gt;&gt; </span><span class="n">displaymatch</span><span class="p">(</span><span class="n">valid</span><span class="o">.</span><span class="n">match</span><span class="p">(</span><span class="s2">&quot;akt5e&quot;</span><span class="p">))</span>  <span class="c1"># Invalid.</span>
 
1101
<span class="gp">&gt;&gt;&gt; </span><span class="n">displaymatch</span><span class="p">(</span><span class="n">valid</span><span class="o">.</span><span class="n">match</span><span class="p">(</span><span class="s2">&quot;akt&quot;</span><span class="p">))</span>    <span class="c1"># Invalid.</span>
 
1102
<span class="gp">&gt;&gt;&gt; </span><span class="n">displaymatch</span><span class="p">(</span><span class="n">valid</span><span class="o">.</span><span class="n">match</span><span class="p">(</span><span class="s2">&quot;727ak&quot;</span><span class="p">))</span>  <span class="c1"># Valid.</span>
 
1103
<span class="go">&quot;&lt;Match: &#39;727ak&#39;, groups=()&gt;&quot;</span>
 
1104
</pre></div>
 
1105
</div>
 
1106
<p>That last hand, <code class="docutils literal"><span class="pre">&quot;727ak&quot;</span></code>, contained a pair, or two of the same valued cards.
 
1107
To match this with a regular expression, one could use backreferences as such:</p>
 
1108
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">pair</span> <span class="o">=</span> <span class="n">re</span><span class="o">.</span><span class="n">compile</span><span class="p">(</span><span class="s2">r&quot;.*(.).*\1&quot;</span><span class="p">)</span>
 
1109
<span class="gp">&gt;&gt;&gt; </span><span class="n">displaymatch</span><span class="p">(</span><span class="n">pair</span><span class="o">.</span><span class="n">match</span><span class="p">(</span><span class="s2">&quot;717ak&quot;</span><span class="p">))</span>     <span class="c1"># Pair of 7s.</span>
 
1110
<span class="go">&quot;&lt;Match: &#39;717&#39;, groups=(&#39;7&#39;,)&gt;&quot;</span>
 
1111
<span class="gp">&gt;&gt;&gt; </span><span class="n">displaymatch</span><span class="p">(</span><span class="n">pair</span><span class="o">.</span><span class="n">match</span><span class="p">(</span><span class="s2">&quot;718ak&quot;</span><span class="p">))</span>     <span class="c1"># No pairs.</span>
 
1112
<span class="gp">&gt;&gt;&gt; </span><span class="n">displaymatch</span><span class="p">(</span><span class="n">pair</span><span class="o">.</span><span class="n">match</span><span class="p">(</span><span class="s2">&quot;354aa&quot;</span><span class="p">))</span>     <span class="c1"># Pair of aces.</span>
 
1113
<span class="go">&quot;&lt;Match: &#39;354aa&#39;, groups=(&#39;a&#39;,)&gt;&quot;</span>
 
1114
</pre></div>
 
1115
</div>
 
1116
<p>To find out what card the pair consists of, one could use the
 
1117
<a class="reference internal" href="#re.MatchObject.group" title="re.MatchObject.group"><code class="xref py py-meth docutils literal"><span class="pre">group()</span></code></a> method of <a class="reference internal" href="#re.MatchObject" title="re.MatchObject"><code class="xref py py-class docutils literal"><span class="pre">MatchObject</span></code></a> in the following
 
1118
manner:</p>
 
1119
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">pair</span><span class="o">.</span><span class="n">match</span><span class="p">(</span><span class="s2">&quot;717ak&quot;</span><span class="p">)</span><span class="o">.</span><span class="n">group</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span>
 
1120
<span class="go">&#39;7&#39;</span>
 
1121
 
 
1122
<span class="go"># Error because re.match() returns None, which doesn&#39;t have a group() method:</span>
 
1123
<span class="gp">&gt;&gt;&gt; </span><span class="n">pair</span><span class="o">.</span><span class="n">match</span><span class="p">(</span><span class="s2">&quot;718ak&quot;</span><span class="p">)</span><span class="o">.</span><span class="n">group</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span>
 
1124
<span class="gt">Traceback (most recent call last):</span>
 
1125
  File <span class="nb">&quot;&lt;pyshell#23&gt;&quot;</span>, line <span class="m">1</span>, in <span class="n">&lt;module&gt;</span>
 
1126
    <span class="n">re</span><span class="o">.</span><span class="n">match</span><span class="p">(</span><span class="s2">r&quot;.*(.).*\1&quot;</span><span class="p">,</span> <span class="s2">&quot;718ak&quot;</span><span class="p">)</span><span class="o">.</span><span class="n">group</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span>
 
1127
<span class="gr">AttributeError</span>: <span class="n">&#39;NoneType&#39; object has no attribute &#39;group&#39;</span>
 
1128
 
 
1129
<span class="gp">&gt;&gt;&gt; </span><span class="n">pair</span><span class="o">.</span><span class="n">match</span><span class="p">(</span><span class="s2">&quot;354aa&quot;</span><span class="p">)</span><span class="o">.</span><span class="n">group</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span>
 
1130
<span class="go">&#39;a&#39;</span>
 
1131
</pre></div>
 
1132
</div>
 
1133
</div>
 
1134
<div class="section" id="simulating-scanf">
 
1135
<h3>7.2.5.2. Simulating scanf()<a class="headerlink" href="#simulating-scanf" title="Permalink to this headline">¶</a></h3>
 
1136
<p id="index-0">Python does not currently have an equivalent to <code class="xref c c-func docutils literal"><span class="pre">scanf()</span></code>.  Regular
 
1137
expressions are generally more powerful, though also more verbose, than
 
1138
<code class="xref c c-func docutils literal"><span class="pre">scanf()</span></code> format strings.  The table below offers some more-or-less
 
1139
equivalent mappings between <code class="xref c c-func docutils literal"><span class="pre">scanf()</span></code> format tokens and regular
 
1140
expressions.</p>
 
1141
<table border="1" class="docutils">
 
1142
<colgroup>
 
1143
<col width="42%" />
 
1144
<col width="58%" />
 
1145
</colgroup>
 
1146
<thead valign="bottom">
 
1147
<tr class="row-odd"><th class="head"><code class="xref c c-func docutils literal"><span class="pre">scanf()</span></code> Token</th>
 
1148
<th class="head">Regular Expression</th>
 
1149
</tr>
 
1150
</thead>
 
1151
<tbody valign="top">
 
1152
<tr class="row-even"><td><code class="docutils literal"><span class="pre">%c</span></code></td>
 
1153
<td><code class="docutils literal"><span class="pre">.</span></code></td>
 
1154
</tr>
 
1155
<tr class="row-odd"><td><code class="docutils literal"><span class="pre">%5c</span></code></td>
 
1156
<td><code class="docutils literal"><span class="pre">.{5}</span></code></td>
 
1157
</tr>
 
1158
<tr class="row-even"><td><code class="docutils literal"><span class="pre">%d</span></code></td>
 
1159
<td><code class="docutils literal"><span class="pre">[-+]?\d+</span></code></td>
 
1160
</tr>
 
1161
<tr class="row-odd"><td><code class="docutils literal"><span class="pre">%e</span></code>, <code class="docutils literal"><span class="pre">%E</span></code>, <code class="docutils literal"><span class="pre">%f</span></code>, <code class="docutils literal"><span class="pre">%g</span></code></td>
 
1162
<td><code class="docutils literal"><span class="pre">[-+]?(\d+(\.\d*)?|\.\d+)([eE][-+]?\d+)?</span></code></td>
 
1163
</tr>
 
1164
<tr class="row-even"><td><code class="docutils literal"><span class="pre">%i</span></code></td>
 
1165
<td><code class="docutils literal"><span class="pre">[-+]?(0[xX][\dA-Fa-f]+|0[0-7]*|\d+)</span></code></td>
 
1166
</tr>
 
1167
<tr class="row-odd"><td><code class="docutils literal"><span class="pre">%o</span></code></td>
 
1168
<td><code class="docutils literal"><span class="pre">[-+]?[0-7]+</span></code></td>
 
1169
</tr>
 
1170
<tr class="row-even"><td><code class="docutils literal"><span class="pre">%s</span></code></td>
 
1171
<td><code class="docutils literal"><span class="pre">\S+</span></code></td>
 
1172
</tr>
 
1173
<tr class="row-odd"><td><code class="docutils literal"><span class="pre">%u</span></code></td>
 
1174
<td><code class="docutils literal"><span class="pre">\d+</span></code></td>
 
1175
</tr>
 
1176
<tr class="row-even"><td><code class="docutils literal"><span class="pre">%x</span></code>, <code class="docutils literal"><span class="pre">%X</span></code></td>
 
1177
<td><code class="docutils literal"><span class="pre">[-+]?(0[xX])?[\dA-Fa-f]+</span></code></td>
 
1178
</tr>
 
1179
</tbody>
 
1180
</table>
 
1181
<p>To extract the filename and numbers from a string like</p>
 
1182
<div class="highlight-python"><div class="highlight"><pre>/usr/sbin/sendmail - 0 errors, 4 warnings
 
1183
</pre></div>
 
1184
</div>
 
1185
<p>you would use a <code class="xref c c-func docutils literal"><span class="pre">scanf()</span></code> format like</p>
 
1186
<div class="highlight-python"><div class="highlight"><pre>%s - %d errors, %d warnings
 
1187
</pre></div>
 
1188
</div>
 
1189
<p>The equivalent regular expression would be</p>
 
1190
<div class="highlight-python"><div class="highlight"><pre>(\S+) - (\d+) errors, (\d+) warnings
 
1191
</pre></div>
 
1192
</div>
 
1193
</div>
 
1194
<div class="section" id="search-vs-match">
 
1195
<span id="id2"></span><h3>7.2.5.3. search() vs. match()<a class="headerlink" href="#search-vs-match" title="Permalink to this headline">¶</a></h3>
 
1196
<p>Python offers two different primitive operations based on regular expressions:
 
1197
<a class="reference internal" href="#re.match" title="re.match"><code class="xref py py-func docutils literal"><span class="pre">re.match()</span></code></a> checks for a match only at the beginning of the string, while
 
1198
<a class="reference internal" href="#re.search" title="re.search"><code class="xref py py-func docutils literal"><span class="pre">re.search()</span></code></a> checks for a match anywhere in the string (this is what Perl
 
1199
does by default).</p>
 
1200
<p>For example:</p>
 
1201
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">re</span><span class="o">.</span><span class="n">match</span><span class="p">(</span><span class="s2">&quot;c&quot;</span><span class="p">,</span> <span class="s2">&quot;abcdef&quot;</span><span class="p">)</span>  <span class="c1"># No match</span>
 
1202
<span class="gp">&gt;&gt;&gt; </span><span class="n">re</span><span class="o">.</span><span class="n">search</span><span class="p">(</span><span class="s2">&quot;c&quot;</span><span class="p">,</span> <span class="s2">&quot;abcdef&quot;</span><span class="p">)</span> <span class="c1"># Match</span>
 
1203
<span class="go">&lt;_sre.SRE_Match object at ...&gt;</span>
 
1204
</pre></div>
 
1205
</div>
 
1206
<p>Regular expressions beginning with <code class="docutils literal"><span class="pre">'^'</span></code> can be used with <a class="reference internal" href="#re.search" title="re.search"><code class="xref py py-func docutils literal"><span class="pre">search()</span></code></a> to
 
1207
restrict the match at the beginning of the string:</p>
 
1208
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">re</span><span class="o">.</span><span class="n">match</span><span class="p">(</span><span class="s2">&quot;c&quot;</span><span class="p">,</span> <span class="s2">&quot;abcdef&quot;</span><span class="p">)</span>  <span class="c1"># No match</span>
 
1209
<span class="gp">&gt;&gt;&gt; </span><span class="n">re</span><span class="o">.</span><span class="n">search</span><span class="p">(</span><span class="s2">&quot;^c&quot;</span><span class="p">,</span> <span class="s2">&quot;abcdef&quot;</span><span class="p">)</span> <span class="c1"># No match</span>
 
1210
<span class="gp">&gt;&gt;&gt; </span><span class="n">re</span><span class="o">.</span><span class="n">search</span><span class="p">(</span><span class="s2">&quot;^a&quot;</span><span class="p">,</span> <span class="s2">&quot;abcdef&quot;</span><span class="p">)</span>  <span class="c1"># Match</span>
 
1211
<span class="go">&lt;_sre.SRE_Match object at ...&gt;</span>
 
1212
</pre></div>
 
1213
</div>
 
1214
<p>Note however that in <a class="reference internal" href="#re.MULTILINE" title="re.MULTILINE"><code class="xref py py-const docutils literal"><span class="pre">MULTILINE</span></code></a> mode <a class="reference internal" href="#re.match" title="re.match"><code class="xref py py-func docutils literal"><span class="pre">match()</span></code></a> only matches at the
 
1215
beginning of the string, whereas using <a class="reference internal" href="#re.search" title="re.search"><code class="xref py py-func docutils literal"><span class="pre">search()</span></code></a> with a regular expression
 
1216
beginning with <code class="docutils literal"><span class="pre">'^'</span></code> will match at the beginning of each line.</p>
 
1217
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">re</span><span class="o">.</span><span class="n">match</span><span class="p">(</span><span class="s1">&#39;X&#39;</span><span class="p">,</span> <span class="s1">&#39;A</span><span class="se">\n</span><span class="s1">B</span><span class="se">\n</span><span class="s1">X&#39;</span><span class="p">,</span> <span class="n">re</span><span class="o">.</span><span class="n">MULTILINE</span><span class="p">)</span>  <span class="c1"># No match</span>
 
1218
<span class="gp">&gt;&gt;&gt; </span><span class="n">re</span><span class="o">.</span><span class="n">search</span><span class="p">(</span><span class="s1">&#39;^X&#39;</span><span class="p">,</span> <span class="s1">&#39;A</span><span class="se">\n</span><span class="s1">B</span><span class="se">\n</span><span class="s1">X&#39;</span><span class="p">,</span> <span class="n">re</span><span class="o">.</span><span class="n">MULTILINE</span><span class="p">)</span>  <span class="c1"># Match</span>
 
1219
<span class="go">&lt;_sre.SRE_Match object at ...&gt;</span>
 
1220
</pre></div>
 
1221
</div>
 
1222
</div>
 
1223
<div class="section" id="making-a-phonebook">
 
1224
<h3>7.2.5.4. Making a Phonebook<a class="headerlink" href="#making-a-phonebook" title="Permalink to this headline">¶</a></h3>
 
1225
<p><a class="reference internal" href="#re.split" title="re.split"><code class="xref py py-func docutils literal"><span class="pre">split()</span></code></a> splits a string into a list delimited by the passed pattern.  The
 
1226
method is invaluable for converting textual data into data structures that can be
 
1227
easily read and modified by Python as demonstrated in the following example that
 
1228
creates a phonebook.</p>
 
1229
<p>First, here is the input.  Normally it may come from a file, here we are using
 
1230
triple-quoted string syntax:</p>
 
1231
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">text</span> <span class="o">=</span> <span class="s2">&quot;&quot;&quot;Ross McFluff: 834.345.1254 155 Elm Street</span>
 
1232
<span class="gp">...</span><span class="s2"></span>
 
1233
<span class="gp">... </span><span class="s2">Ronald Heathmore: 892.345.3428 436 Finley Avenue</span>
 
1234
<span class="gp">... </span><span class="s2">Frank Burger: 925.541.7625 662 South Dogwood Way</span>
 
1235
<span class="gp">...</span><span class="s2"></span>
 
1236
<span class="gp">...</span><span class="s2"></span>
 
1237
<span class="gp">... </span><span class="s2">Heather Albrecht: 548.326.4584 919 Park Place&quot;&quot;&quot;</span>
 
1238
</pre></div>
 
1239
</div>
 
1240
<p>The entries are separated by one or more newlines. Now we convert the string
 
1241
into a list with each nonempty line having its own entry:</p>
 
1242
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">entries</span> <span class="o">=</span> <span class="n">re</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="s2">&quot;</span><span class="se">\n</span><span class="s2">+&quot;</span><span class="p">,</span> <span class="n">text</span><span class="p">)</span>
 
1243
<span class="gp">&gt;&gt;&gt; </span><span class="n">entries</span>
 
1244
<span class="go">[&#39;Ross McFluff: 834.345.1254 155 Elm Street&#39;,</span>
 
1245
<span class="go">&#39;Ronald Heathmore: 892.345.3428 436 Finley Avenue&#39;,</span>
 
1246
<span class="go">&#39;Frank Burger: 925.541.7625 662 South Dogwood Way&#39;,</span>
 
1247
<span class="go">&#39;Heather Albrecht: 548.326.4584 919 Park Place&#39;]</span>
 
1248
</pre></div>
 
1249
</div>
 
1250
<p>Finally, split each entry into a list with first name, last name, telephone
 
1251
number, and address.  We use the <code class="docutils literal"><span class="pre">maxsplit</span></code> parameter of <a class="reference internal" href="#re.split" title="re.split"><code class="xref py py-func docutils literal"><span class="pre">split()</span></code></a>
 
1252
because the address has spaces, our splitting pattern, in it:</p>
 
1253
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="p">[</span><span class="n">re</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="s2">&quot;:? &quot;</span><span class="p">,</span> <span class="n">entry</span><span class="p">,</span> <span class="mi">3</span><span class="p">)</span> <span class="k">for</span> <span class="n">entry</span> <span class="ow">in</span> <span class="n">entries</span><span class="p">]</span>
 
1254
<span class="go">[[&#39;Ross&#39;, &#39;McFluff&#39;, &#39;834.345.1254&#39;, &#39;155 Elm Street&#39;],</span>
 
1255
<span class="go">[&#39;Ronald&#39;, &#39;Heathmore&#39;, &#39;892.345.3428&#39;, &#39;436 Finley Avenue&#39;],</span>
 
1256
<span class="go">[&#39;Frank&#39;, &#39;Burger&#39;, &#39;925.541.7625&#39;, &#39;662 South Dogwood Way&#39;],</span>
 
1257
<span class="go">[&#39;Heather&#39;, &#39;Albrecht&#39;, &#39;548.326.4584&#39;, &#39;919 Park Place&#39;]]</span>
 
1258
</pre></div>
 
1259
</div>
 
1260
<p>The <code class="docutils literal"><span class="pre">:?</span></code> pattern matches the colon after the last name, so that it does not
 
1261
occur in the result list.  With a <code class="docutils literal"><span class="pre">maxsplit</span></code> of <code class="docutils literal"><span class="pre">4</span></code>, we could separate the
 
1262
house number from the street name:</p>
 
1263
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="p">[</span><span class="n">re</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="s2">&quot;:? &quot;</span><span class="p">,</span> <span class="n">entry</span><span class="p">,</span> <span class="mi">4</span><span class="p">)</span> <span class="k">for</span> <span class="n">entry</span> <span class="ow">in</span> <span class="n">entries</span><span class="p">]</span>
 
1264
<span class="go">[[&#39;Ross&#39;, &#39;McFluff&#39;, &#39;834.345.1254&#39;, &#39;155&#39;, &#39;Elm Street&#39;],</span>
 
1265
<span class="go">[&#39;Ronald&#39;, &#39;Heathmore&#39;, &#39;892.345.3428&#39;, &#39;436&#39;, &#39;Finley Avenue&#39;],</span>
 
1266
<span class="go">[&#39;Frank&#39;, &#39;Burger&#39;, &#39;925.541.7625&#39;, &#39;662&#39;, &#39;South Dogwood Way&#39;],</span>
 
1267
<span class="go">[&#39;Heather&#39;, &#39;Albrecht&#39;, &#39;548.326.4584&#39;, &#39;919&#39;, &#39;Park Place&#39;]]</span>
 
1268
</pre></div>
 
1269
</div>
 
1270
</div>
 
1271
<div class="section" id="text-munging">
 
1272
<h3>7.2.5.5. Text Munging<a class="headerlink" href="#text-munging" title="Permalink to this headline">¶</a></h3>
 
1273
<p><a class="reference internal" href="#re.sub" title="re.sub"><code class="xref py py-func docutils literal"><span class="pre">sub()</span></code></a> replaces every occurrence of a pattern with a string or the
 
1274
result of a function.  This example demonstrates using <a class="reference internal" href="#re.sub" title="re.sub"><code class="xref py py-func docutils literal"><span class="pre">sub()</span></code></a> with
 
1275
a function to &#8220;munge&#8221; text, or randomize the order of all the characters
 
1276
in each word of a sentence except for the first and last characters:</p>
 
1277
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="k">def</span> <span class="nf">repl</span><span class="p">(</span><span class="n">m</span><span class="p">):</span>
 
1278
<span class="gp">... </span>  <span class="n">inner_word</span> <span class="o">=</span> <span class="nb">list</span><span class="p">(</span><span class="n">m</span><span class="o">.</span><span class="n">group</span><span class="p">(</span><span class="mi">2</span><span class="p">))</span>
 
1279
<span class="gp">... </span>  <span class="n">random</span><span class="o">.</span><span class="n">shuffle</span><span class="p">(</span><span class="n">inner_word</span><span class="p">)</span>
 
1280
<span class="gp">... </span>  <span class="k">return</span> <span class="n">m</span><span class="o">.</span><span class="n">group</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span> <span class="o">+</span> <span class="s2">&quot;&quot;</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">inner_word</span><span class="p">)</span> <span class="o">+</span> <span class="n">m</span><span class="o">.</span><span class="n">group</span><span class="p">(</span><span class="mi">3</span><span class="p">)</span>
 
1281
<span class="gp">&gt;&gt;&gt; </span><span class="n">text</span> <span class="o">=</span> <span class="s2">&quot;Professor Abdolmalek, please report your absences promptly.&quot;</span>
 
1282
<span class="gp">&gt;&gt;&gt; </span><span class="n">re</span><span class="o">.</span><span class="n">sub</span><span class="p">(</span><span class="s2">r&quot;(\w)(\w+)(\w)&quot;</span><span class="p">,</span> <span class="n">repl</span><span class="p">,</span> <span class="n">text</span><span class="p">)</span>
 
1283
<span class="go">&#39;Poefsrosr Aealmlobdk, pslaee reorpt your abnseces plmrptoy.&#39;</span>
 
1284
<span class="gp">&gt;&gt;&gt; </span><span class="n">re</span><span class="o">.</span><span class="n">sub</span><span class="p">(</span><span class="s2">r&quot;(\w)(\w+)(\w)&quot;</span><span class="p">,</span> <span class="n">repl</span><span class="p">,</span> <span class="n">text</span><span class="p">)</span>
 
1285
<span class="go">&#39;Pofsroser Aodlambelk, plasee reoprt yuor asnebces potlmrpy.&#39;</span>
 
1286
</pre></div>
 
1287
</div>
 
1288
</div>
 
1289
<div class="section" id="finding-all-adverbs">
 
1290
<h3>7.2.5.6. Finding all Adverbs<a class="headerlink" href="#finding-all-adverbs" title="Permalink to this headline">¶</a></h3>
 
1291
<p><a class="reference internal" href="#re.findall" title="re.findall"><code class="xref py py-func docutils literal"><span class="pre">findall()</span></code></a> matches <em>all</em> occurrences of a pattern, not just the first
 
1292
one as <a class="reference internal" href="#re.search" title="re.search"><code class="xref py py-func docutils literal"><span class="pre">search()</span></code></a> does.  For example, if one was a writer and wanted to
 
1293
find all of the adverbs in some text, he or she might use <a class="reference internal" href="#re.findall" title="re.findall"><code class="xref py py-func docutils literal"><span class="pre">findall()</span></code></a> in
 
1294
the following manner:</p>
 
1295
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">text</span> <span class="o">=</span> <span class="s2">&quot;He was carefully disguised but captured quickly by police.&quot;</span>
 
1296
<span class="gp">&gt;&gt;&gt; </span><span class="n">re</span><span class="o">.</span><span class="n">findall</span><span class="p">(</span><span class="s2">r&quot;\w+ly&quot;</span><span class="p">,</span> <span class="n">text</span><span class="p">)</span>
 
1297
<span class="go">[&#39;carefully&#39;, &#39;quickly&#39;]</span>
 
1298
</pre></div>
 
1299
</div>
 
1300
</div>
 
1301
<div class="section" id="finding-all-adverbs-and-their-positions">
 
1302
<h3>7.2.5.7. Finding all Adverbs and their Positions<a class="headerlink" href="#finding-all-adverbs-and-their-positions" title="Permalink to this headline">¶</a></h3>
 
1303
<p>If one wants more information about all matches of a pattern than the matched
 
1304
text, <a class="reference internal" href="#re.finditer" title="re.finditer"><code class="xref py py-func docutils literal"><span class="pre">finditer()</span></code></a> is useful as it provides instances of
 
1305
<a class="reference internal" href="#re.MatchObject" title="re.MatchObject"><code class="xref py py-class docutils literal"><span class="pre">MatchObject</span></code></a> instead of strings.  Continuing with the previous example,
 
1306
if one was a writer who wanted to find all of the adverbs <em>and their positions</em>
 
1307
in some text, he or she would use <a class="reference internal" href="#re.finditer" title="re.finditer"><code class="xref py py-func docutils literal"><span class="pre">finditer()</span></code></a> in the following manner:</p>
 
1308
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">text</span> <span class="o">=</span> <span class="s2">&quot;He was carefully disguised but captured quickly by police.&quot;</span>
 
1309
<span class="gp">&gt;&gt;&gt; </span><span class="k">for</span> <span class="n">m</span> <span class="ow">in</span> <span class="n">re</span><span class="o">.</span><span class="n">finditer</span><span class="p">(</span><span class="s2">r&quot;\w+ly&quot;</span><span class="p">,</span> <span class="n">text</span><span class="p">):</span>
 
1310
<span class="gp">... </span>    <span class="k">print</span> <span class="s1">&#39;</span><span class="si">%02d</span><span class="s1">-</span><span class="si">%02d</span><span class="s1">: </span><span class="si">%s</span><span class="s1">&#39;</span> <span class="o">%</span> <span class="p">(</span><span class="n">m</span><span class="o">.</span><span class="n">start</span><span class="p">(),</span> <span class="n">m</span><span class="o">.</span><span class="n">end</span><span class="p">(),</span> <span class="n">m</span><span class="o">.</span><span class="n">group</span><span class="p">(</span><span class="mi">0</span><span class="p">))</span>
 
1311
<span class="go">07-16: carefully</span>
 
1312
<span class="go">40-47: quickly</span>
 
1313
</pre></div>
 
1314
</div>
 
1315
</div>
 
1316
<div class="section" id="raw-string-notation">
 
1317
<h3>7.2.5.8. Raw String Notation<a class="headerlink" href="#raw-string-notation" title="Permalink to this headline">¶</a></h3>
 
1318
<p>Raw string notation (<code class="docutils literal"><span class="pre">r&quot;text&quot;</span></code>) keeps regular expressions sane.  Without it,
 
1319
every backslash (<code class="docutils literal"><span class="pre">'\'</span></code>) in a regular expression would have to be prefixed with
 
1320
another one to escape it.  For example, the two following lines of code are
 
1321
functionally identical:</p>
 
1322
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">re</span><span class="o">.</span><span class="n">match</span><span class="p">(</span><span class="s2">r&quot;\W(.)\1\W&quot;</span><span class="p">,</span> <span class="s2">&quot; ff &quot;</span><span class="p">)</span>
 
1323
<span class="go">&lt;_sre.SRE_Match object at ...&gt;</span>
 
1324
<span class="gp">&gt;&gt;&gt; </span><span class="n">re</span><span class="o">.</span><span class="n">match</span><span class="p">(</span><span class="s2">&quot;</span><span class="se">\\</span><span class="s2">W(.)</span><span class="se">\\</span><span class="s2">1</span><span class="se">\\</span><span class="s2">W&quot;</span><span class="p">,</span> <span class="s2">&quot; ff &quot;</span><span class="p">)</span>
 
1325
<span class="go">&lt;_sre.SRE_Match object at ...&gt;</span>
 
1326
</pre></div>
 
1327
</div>
 
1328
<p>When one wants to match a literal backslash, it must be escaped in the regular
 
1329
expression.  With raw string notation, this means <code class="docutils literal"><span class="pre">r&quot;\\&quot;</span></code>.  Without raw string
 
1330
notation, one must use <code class="docutils literal"><span class="pre">&quot;\\\\&quot;</span></code>, making the following lines of code
 
1331
functionally identical:</p>
 
1332
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">re</span><span class="o">.</span><span class="n">match</span><span class="p">(</span><span class="s2">r&quot;</span><span class="se">\\</span><span class="s2">&quot;</span><span class="p">,</span> <span class="s2">r&quot;</span><span class="se">\\</span><span class="s2">&quot;</span><span class="p">)</span>
 
1333
<span class="go">&lt;_sre.SRE_Match object at ...&gt;</span>
 
1334
<span class="gp">&gt;&gt;&gt; </span><span class="n">re</span><span class="o">.</span><span class="n">match</span><span class="p">(</span><span class="s2">&quot;</span><span class="se">\\\\</span><span class="s2">&quot;</span><span class="p">,</span> <span class="s2">r&quot;</span><span class="se">\\</span><span class="s2">&quot;</span><span class="p">)</span>
 
1335
<span class="go">&lt;_sre.SRE_Match object at ...&gt;</span>
 
1336
</pre></div>
 
1337
</div>
 
1338
</div>
 
1339
</div>
 
1340
</div>
 
1341
 
 
1342
 
 
1343
          </div>
 
1344
        </div>
 
1345
      </div>
 
1346
      <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
 
1347
        <div class="sphinxsidebarwrapper">
 
1348
  <h3><a href="../contents.html">Table Of Contents</a></h3>
 
1349
  <ul>
 
1350
<li><a class="reference internal" href="#">7.2. <code class="docutils literal"><span class="pre">re</span></code> &#8212; Regular expression operations</a><ul>
 
1351
<li><a class="reference internal" href="#regular-expression-syntax">7.2.1. Regular Expression Syntax</a></li>
 
1352
<li><a class="reference internal" href="#module-contents">7.2.2. Module Contents</a></li>
 
1353
<li><a class="reference internal" href="#regular-expression-objects">7.2.3. Regular Expression Objects</a></li>
 
1354
<li><a class="reference internal" href="#match-objects">7.2.4. Match Objects</a></li>
 
1355
<li><a class="reference internal" href="#examples">7.2.5. Examples</a><ul>
 
1356
<li><a class="reference internal" href="#checking-for-a-pair">7.2.5.1. Checking For a Pair</a></li>
 
1357
<li><a class="reference internal" href="#simulating-scanf">7.2.5.2. Simulating scanf()</a></li>
 
1358
<li><a class="reference internal" href="#search-vs-match">7.2.5.3. search() vs. match()</a></li>
 
1359
<li><a class="reference internal" href="#making-a-phonebook">7.2.5.4. Making a Phonebook</a></li>
 
1360
<li><a class="reference internal" href="#text-munging">7.2.5.5. Text Munging</a></li>
 
1361
<li><a class="reference internal" href="#finding-all-adverbs">7.2.5.6. Finding all Adverbs</a></li>
 
1362
<li><a class="reference internal" href="#finding-all-adverbs-and-their-positions">7.2.5.7. Finding all Adverbs and their Positions</a></li>
 
1363
<li><a class="reference internal" href="#raw-string-notation">7.2.5.8. Raw String Notation</a></li>
 
1364
</ul>
 
1365
</li>
 
1366
</ul>
 
1367
</li>
 
1368
</ul>
 
1369
 
 
1370
  <h4>Previous topic</h4>
 
1371
  <p class="topless"><a href="string.html"
 
1372
                        title="previous chapter">7.1. <code class="docutils literal"><span class="pre">string</span></code> &#8212; Common string operations</a></p>
 
1373
  <h4>Next topic</h4>
 
1374
  <p class="topless"><a href="struct.html"
 
1375
                        title="next chapter">7.3. <code class="docutils literal"><span class="pre">struct</span></code> &#8212; Interpret strings as packed binary data</a></p>
 
1376
<h3>This Page</h3>
 
1377
<ul class="this-page-menu">
 
1378
  <li><a href="../bugs.html">Report a Bug</a></li>
 
1379
  <li><a href="../_sources/library/re.txt"
 
1380
         rel="nofollow">Show Source</a></li>
 
1381
</ul>
 
1382
 
 
1383
<div id="searchbox" style="display: none" role="search">
 
1384
  <h3>Quick search</h3>
 
1385
    <form class="search" action="../search.html" method="get">
 
1386
      <input type="text" name="q" />
 
1387
      <input type="submit" value="Go" />
 
1388
      <input type="hidden" name="check_keywords" value="yes" />
 
1389
      <input type="hidden" name="area" value="default" />
 
1390
    </form>
 
1391
    <p class="searchtip" style="font-size: 90%">
 
1392
    Enter search terms or a module, class or function name.
 
1393
    </p>
 
1394
</div>
 
1395
<script type="text/javascript">$('#searchbox').show(0);</script>
 
1396
        </div>
 
1397
      </div>
 
1398
      <div class="clearer"></div>
 
1399
    </div>  
 
1400
    <div class="related" role="navigation" aria-label="related navigation">
 
1401
      <h3>Navigation</h3>
 
1402
      <ul>
 
1403
        <li class="right" style="margin-right: 10px">
 
1404
          <a href="../genindex.html" title="General Index"
 
1405
             >index</a></li>
 
1406
        <li class="right" >
 
1407
          <a href="../py-modindex.html" title="Python Module Index"
 
1408
             >modules</a> |</li>
 
1409
        <li class="right" >
 
1410
          <a href="struct.html" title="7.3. struct — Interpret strings as packed binary data"
 
1411
             >next</a> |</li>
 
1412
        <li class="right" >
 
1413
          <a href="string.html" title="7.1. string — Common string operations"
 
1414
             >previous</a> |</li>
 
1415
        <li><img src="../_static/py.png" alt=""
 
1416
                 style="vertical-align: middle; margin-top: -1px"/></li>
 
1417
        <li><a href="https://www.python.org/">Python</a> &raquo;</li>
 
1418
        <li>
 
1419
          <span class="version_switcher_placeholder">2.7.11</span>
 
1420
          <a href="../index.html">Documentation</a> &raquo;
 
1421
        </li>
 
1422
 
 
1423
          <li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> &raquo;</li>
 
1424
          <li class="nav-item nav-item-2"><a href="strings.html" >7. String Services</a> &raquo;</li> 
 
1425
      </ul>
 
1426
    </div>  
 
1427
    <div class="footer">
 
1428
    &copy; <a href="../copyright.html">Copyright</a> 1990-2016, Python Software Foundation.
 
1429
    <br />
 
1430
    The Python Software Foundation is a non-profit corporation.
 
1431
    <a href="https://www.python.org/psf/donations/">Please donate.</a>
 
1432
    <br />
 
1433
    Last updated on Jan 23, 2016.
 
1434
    <a href="../bugs.html">Found a bug</a>?
 
1435
    <br />
 
1436
    Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 1.3.3.
 
1437
    </div>
 
1438
 
 
1439
  </body>
 
1440
</html>
 
 
b'\\ No newline at end of file'