Sophie

Sophie

distrib > Mandriva > 2010.1 > x86_64 > media > contrib-backports > by-pkgid > 3ba3bd1608c672ba2129b098a48e9e4d > files > 779

python3-docs-3.2.2-3mdv2010.2.noarch.rpm



<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    
    <title>6.2. re — Regular expression operations &mdash; Python v3.2.2 documentation</title>
    <link rel="stylesheet" href="../_static/default.css" type="text/css" />
    <link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
    <script type="text/javascript">
      var DOCUMENTATION_OPTIONS = {
        URL_ROOT:    '../',
        VERSION:     '3.2.2',
        COLLAPSE_INDEX: false,
        FILE_SUFFIX: '.html',
        HAS_SOURCE:  true
      };
    </script>
    <script type="text/javascript" src="../_static/jquery.js"></script>
    <script type="text/javascript" src="../_static/underscore.js"></script>
    <script type="text/javascript" src="../_static/doctools.js"></script>
    <script type="text/javascript" src="../_static/sidebar.js"></script>
    <link rel="search" type="application/opensearchdescription+xml"
          title="Search within Python v3.2.2 documentation"
          href="../_static/opensearch.xml"/>
    <link rel="author" title="About these documents" href="../about.html" />
    <link rel="copyright" title="Copyright" href="../copyright.html" />
    <link rel="top" title="Python v3.2.2 documentation" href="../index.html" />
    <link rel="up" title="6. String Services" href="strings.html" />
    <link rel="next" title="6.3. struct — Interpret bytes as packed binary data" href="struct.html" />
    <link rel="prev" title="6.1. string — Common string operations" href="string.html" />
    <link rel="shortcut icon" type="image/png" href="../_static/py.png" />
 

  </head>
  <body>
    <div class="related">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../genindex.html" title="General Index"
             accesskey="I">index</a></li>
        <li class="right" >
          <a href="../py-modindex.html" title="Python Module Index"
             >modules</a> |</li>
        <li class="right" >
          <a href="struct.html" title="6.3. struct — Interpret bytes as packed binary data"
             accesskey="N">next</a> |</li>
        <li class="right" >
          <a href="string.html" title="6.1. string — Common string operations"
             accesskey="P">previous</a> |</li>
        <li><img src="../_static/py.png" alt=""
                 style="vertical-align: middle; margin-top: -1px"/></li>
        <li><a href="../index.html">Python v3.2.2 documentation</a> &raquo;</li>

          <li><a href="index.html" >The Python Standard Library</a> &raquo;</li>
          <li><a href="strings.html" accesskey="U">6. String Services</a> &raquo;</li> 
      </ul>
    </div>  

    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          <div class="body">
            
  <div class="section" id="module-re">
<span id="re-regular-expression-operations"></span><h1>6.2. <a class="reference internal" href="#module-re" title="re: Regular expression operations."><tt class="xref py py-mod docutils literal"><span class="pre">re</span></tt></a> &#8212; Regular expression operations<a class="headerlink" href="#module-re" title="Permalink to this headline">¶</a></h1>
<p>This module provides regular expression matching operations similar to
those found in Perl.</p>
<p>Both patterns and strings to be searched can be Unicode strings as well as
8-bit strings. However, Unicode strings and 8-bit strings cannot be mixed:
that is, you cannot match an Unicode string with a byte pattern or
vice-versa; similarly, when asking for a substitution, the replacement
string must be of the same type as both the pattern and the search string.</p>
<p>Regular expressions use the backslash character (<tt class="docutils literal"><span class="pre">'\'</span></tt>) to indicate
special forms or to allow special characters to be used without invoking
their special meaning.  This collides with Python&#8217;s usage of the same
character for the same purpose in string literals; for example, to match
a literal backslash, one might have to write <tt class="docutils literal"><span class="pre">'\\\\'</span></tt> as the pattern
string, because the regular expression must be <tt class="docutils literal"><span class="pre">\\</span></tt>, and each
backslash must be expressed as <tt class="docutils literal"><span class="pre">\\</span></tt> inside a regular Python string
literal.</p>
<p>The solution is to use Python&#8217;s raw string notation for regular expression
patterns; backslashes are not handled in any special way in a string literal
prefixed with <tt class="docutils literal"><span class="pre">'r'</span></tt>.  So <tt class="docutils literal"><span class="pre">r&quot;\n&quot;</span></tt> is a two-character string containing
<tt class="docutils literal"><span class="pre">'\'</span></tt> and <tt class="docutils literal"><span class="pre">'n'</span></tt>, while <tt class="docutils literal"><span class="pre">&quot;\n&quot;</span></tt> is a one-character string containing a
newline.  Usually patterns will be expressed in Python code using this raw
string notation.</p>
<p>It is important to note that most regular expression operations are available as
module-level functions and methods on
<a class="reference internal" href="#re-objects"><em>compiled regular expressions</em></a>.  The functions are shortcuts
that don&#8217;t require you to compile a regex object first, but miss some
fine-tuning parameters.</p>
<div class="admonition-see-also admonition seealso">
<p class="first admonition-title">See also</p>
<dl class="last docutils">
<dt>Mastering Regular Expressions</dt>
<dd>Book on regular expressions by Jeffrey Friedl, published by O&#8217;Reilly.  The
second edition of the book no longer covers Python at all, but the first
edition covered writing good regular expression patterns in great detail.</dd>
</dl>
</div>
<div class="section" id="regular-expression-syntax">
<span id="re-syntax"></span><h2>6.2.1. Regular Expression Syntax<a class="headerlink" href="#regular-expression-syntax" title="Permalink to this headline">¶</a></h2>
<p>A regular expression (or RE) specifies a set of strings that matches it; the
functions in this module let you check if a particular string matches a given
regular expression (or if a given regular expression matches a particular
string, which comes down to the same thing).</p>
<p>Regular expressions can be concatenated to form new regular expressions; if <em>A</em>
and <em>B</em> are both regular expressions, then <em>AB</em> is also a regular expression.
In general, if a string <em>p</em> matches <em>A</em> and another string <em>q</em> matches <em>B</em>, the
string <em>pq</em> will match AB.  This holds unless <em>A</em> or <em>B</em> contain low precedence
operations; boundary conditions between <em>A</em> and <em>B</em>; or have numbered group
references.  Thus, complex expressions can easily be constructed from simpler
primitive expressions like the ones described here.  For details of the theory
and implementation of regular expressions, consult the Friedl book referenced
above, or almost any textbook about compiler construction.</p>
<p>A brief explanation of the format of regular expressions follows.  For further
information and a gentler presentation, consult the <a class="reference internal" href="../howto/regex.html#regex-howto"><em>Regular Expression HOWTO</em></a>.</p>
<p>Regular expressions can contain both special and ordinary characters. Most
ordinary characters, like <tt class="docutils literal"><span class="pre">'A'</span></tt>, <tt class="docutils literal"><span class="pre">'a'</span></tt>, or <tt class="docutils literal"><span class="pre">'0'</span></tt>, are the simplest regular
expressions; they simply match themselves.  You can concatenate ordinary
characters, so <tt class="docutils literal"><span class="pre">last</span></tt> matches the string <tt class="docutils literal"><span class="pre">'last'</span></tt>.  (In the rest of this
section, we&#8217;ll write RE&#8217;s in <tt class="docutils literal"><span class="pre">this</span> <span class="pre">special</span> <span class="pre">style</span></tt>, usually without quotes, and
strings to be matched <tt class="docutils literal"><span class="pre">'in</span> <span class="pre">single</span> <span class="pre">quotes'</span></tt>.)</p>
<p>Some characters, like <tt class="docutils literal"><span class="pre">'|'</span></tt> or <tt class="docutils literal"><span class="pre">'('</span></tt>, are special. Special
characters either stand for classes of ordinary characters, or affect
how the regular expressions around them are interpreted. Regular
expression pattern strings may not contain null bytes, but can specify
the null byte using the <tt class="docutils literal"><span class="pre">\number</span></tt> notation, e.g., <tt class="docutils literal"><span class="pre">'\x00'</span></tt>.</p>
<p>The special characters are:</p>
<dl class="docutils">
<dt><tt class="docutils literal"><span class="pre">'.'</span></tt></dt>
<dd>(Dot.)  In the default mode, this matches any character except a newline.  If
the <a class="reference internal" href="#re.DOTALL" title="re.DOTALL"><tt class="xref py py-const docutils literal"><span class="pre">DOTALL</span></tt></a> flag has been specified, this matches any character
including a newline.</dd>
<dt><tt class="docutils literal"><span class="pre">'^'</span></tt></dt>
<dd>(Caret.)  Matches the start of the string, and in <a class="reference internal" href="#re.MULTILINE" title="re.MULTILINE"><tt class="xref py py-const docutils literal"><span class="pre">MULTILINE</span></tt></a> mode also
matches immediately after each newline.</dd>
<dt><tt class="docutils literal"><span class="pre">'$'</span></tt></dt>
<dd>Matches the end of the string or just before the newline at the end of the
string, and in <a class="reference internal" href="#re.MULTILINE" title="re.MULTILINE"><tt class="xref py py-const docutils literal"><span class="pre">MULTILINE</span></tt></a> mode also matches before a newline.  <tt class="docutils literal"><span class="pre">foo</span></tt>
matches both &#8216;foo&#8217; and &#8216;foobar&#8217;, while the regular expression <tt class="docutils literal"><span class="pre">foo$</span></tt> matches
only &#8216;foo&#8217;.  More interestingly, searching for <tt class="docutils literal"><span class="pre">foo.$</span></tt> in <tt class="docutils literal"><span class="pre">'foo1\nfoo2\n'</span></tt>
matches &#8216;foo2&#8217; normally, but &#8216;foo1&#8217; in <a class="reference internal" href="#re.MULTILINE" title="re.MULTILINE"><tt class="xref py py-const docutils literal"><span class="pre">MULTILINE</span></tt></a> mode; searching for
a single <tt class="docutils literal"><span class="pre">$</span></tt> in <tt class="docutils literal"><span class="pre">'foo\n'</span></tt> will find two (empty) matches: one just before
the newline, and one at the end of the string.</dd>
<dt><tt class="docutils literal"><span class="pre">'*'</span></tt></dt>
<dd>Causes the resulting RE to match 0 or more repetitions of the preceding RE, as
many repetitions as are possible.  <tt class="docutils literal"><span class="pre">ab*</span></tt> will match &#8216;a&#8217;, &#8216;ab&#8217;, or &#8216;a&#8217; followed
by any number of &#8216;b&#8217;s.</dd>
<dt><tt class="docutils literal"><span class="pre">'+'</span></tt></dt>
<dd>Causes the resulting RE to match 1 or more repetitions of the preceding RE.
<tt class="docutils literal"><span class="pre">ab+</span></tt> will match &#8216;a&#8217; followed by any non-zero number of &#8216;b&#8217;s; it will not
match just &#8216;a&#8217;.</dd>
<dt><tt class="docutils literal"><span class="pre">'?'</span></tt></dt>
<dd>Causes the resulting RE to match 0 or 1 repetitions of the preceding RE.
<tt class="docutils literal"><span class="pre">ab?</span></tt> will match either &#8216;a&#8217; or &#8216;ab&#8217;.</dd>
<dt><tt class="docutils literal"><span class="pre">*?</span></tt>, <tt class="docutils literal"><span class="pre">+?</span></tt>, <tt class="docutils literal"><span class="pre">??</span></tt></dt>
<dd>The <tt class="docutils literal"><span class="pre">'*'</span></tt>, <tt class="docutils literal"><span class="pre">'+'</span></tt>, and <tt class="docutils literal"><span class="pre">'?'</span></tt> qualifiers are all <em class="dfn">greedy</em>; they match
as much text as possible.  Sometimes this behaviour isn&#8217;t desired; if the RE
<tt class="docutils literal"><span class="pre">&lt;.*&gt;</span></tt> is matched against <tt class="docutils literal"><span class="pre">'&lt;H1&gt;title&lt;/H1&gt;'</span></tt>, it will match the entire
string, and not just <tt class="docutils literal"><span class="pre">'&lt;H1&gt;'</span></tt>.  Adding <tt class="docutils literal"><span class="pre">'?'</span></tt> after the qualifier makes it
perform the match in <em class="dfn">non-greedy</em> or <em class="dfn">minimal</em> fashion; as <em>few</em>
characters as possible will be matched.  Using <tt class="docutils literal"><span class="pre">.*?</span></tt> in the previous
expression will match only <tt class="docutils literal"><span class="pre">'&lt;H1&gt;'</span></tt>.</dd>
<dt><tt class="docutils literal"><span class="pre">{m}</span></tt></dt>
<dd>Specifies that exactly <em>m</em> copies of the previous RE should be matched; fewer
matches cause the entire RE not to match.  For example, <tt class="docutils literal"><span class="pre">a{6}</span></tt> will match
exactly six <tt class="docutils literal"><span class="pre">'a'</span></tt> characters, but not five.</dd>
<dt><tt class="docutils literal"><span class="pre">{m,n}</span></tt></dt>
<dd>Causes the resulting RE to match from <em>m</em> to <em>n</em> repetitions of the preceding
RE, attempting to match as many repetitions as possible.  For example,
<tt class="docutils literal"><span class="pre">a{3,5}</span></tt> will match from 3 to 5 <tt class="docutils literal"><span class="pre">'a'</span></tt> characters.  Omitting <em>m</em> specifies a
lower bound of zero,  and omitting <em>n</em> specifies an infinite upper bound.  As an
example, <tt class="docutils literal"><span class="pre">a{4,}b</span></tt> will match <tt class="docutils literal"><span class="pre">aaaab</span></tt> or a thousand <tt class="docutils literal"><span class="pre">'a'</span></tt> characters
followed by a <tt class="docutils literal"><span class="pre">b</span></tt>, but not <tt class="docutils literal"><span class="pre">aaab</span></tt>. The comma may not be omitted or the
modifier would be confused with the previously described form.</dd>
<dt><tt class="docutils literal"><span class="pre">{m,n}?</span></tt></dt>
<dd>Causes the resulting RE to match from <em>m</em> to <em>n</em> repetitions of the preceding
RE, attempting to match as <em>few</em> repetitions as possible.  This is the
non-greedy version of the previous qualifier.  For example, on the
6-character string <tt class="docutils literal"><span class="pre">'aaaaaa'</span></tt>, <tt class="docutils literal"><span class="pre">a{3,5}</span></tt> will match 5 <tt class="docutils literal"><span class="pre">'a'</span></tt> characters,
while <tt class="docutils literal"><span class="pre">a{3,5}?</span></tt> will only match 3 characters.</dd>
<dt><tt class="docutils literal"><span class="pre">'\'</span></tt></dt>
<dd><p class="first">Either escapes special characters (permitting you to match characters like
<tt class="docutils literal"><span class="pre">'*'</span></tt>, <tt class="docutils literal"><span class="pre">'?'</span></tt>, and so forth), or signals a special sequence; special
sequences are discussed below.</p>
<p class="last">If you&#8217;re not using a raw string to express the pattern, remember that Python
also uses the backslash as an escape sequence in string literals; if the escape
sequence isn&#8217;t recognized by Python&#8217;s parser, the backslash and subsequent
character are included in the resulting string.  However, if Python would
recognize the resulting sequence, the backslash should be repeated twice.  This
is complicated and hard to understand, so it&#8217;s highly recommended that you use
raw strings for all but the simplest expressions.</p>
</dd>
<dt><tt class="docutils literal"><span class="pre">[]</span></tt></dt>
<dd><p class="first">Used to indicate a set of characters.  Characters can be listed individually, or
a range of characters can be indicated by giving two characters and separating
them by a <tt class="docutils literal"><span class="pre">'-'</span></tt>.  Special characters are not active inside sets.  For example,
<tt class="docutils literal"><span class="pre">[akm$]</span></tt> will match any of the characters <tt class="docutils literal"><span class="pre">'a'</span></tt>, <tt class="docutils literal"><span class="pre">'k'</span></tt>,
<tt class="docutils literal"><span class="pre">'m'</span></tt>, or <tt class="docutils literal"><span class="pre">'$'</span></tt>; <tt class="docutils literal"><span class="pre">[a-z]</span></tt> will match any lowercase letter, and
<tt class="docutils literal"><span class="pre">[a-zA-Z0-9]</span></tt> matches any letter or digit.  Character classes such
as <tt class="docutils literal"><span class="pre">\w</span></tt> or <tt class="docutils literal"><span class="pre">\S</span></tt> (defined below) are also acceptable inside a
range, although the characters they match depends on whether
<a class="reference internal" href="#re.ASCII" title="re.ASCII"><tt class="xref py py-const docutils literal"><span class="pre">ASCII</span></tt></a> or  <a class="reference internal" href="#re.LOCALE" title="re.LOCALE"><tt class="xref py py-const docutils literal"><span class="pre">LOCALE</span></tt></a> mode is in force.  If you want to
include a <tt class="docutils literal"><span class="pre">']'</span></tt> or a <tt class="docutils literal"><span class="pre">'-'</span></tt> inside a set, precede it with a
backslash, or place it as the first character.  The pattern <tt class="docutils literal"><span class="pre">[]]</span></tt>
will match <tt class="docutils literal"><span class="pre">']'</span></tt>, for example.</p>
<p>You can match the characters not within a range by <em class="dfn">complementing</em> the set.
This is indicated by including a <tt class="docutils literal"><span class="pre">'^'</span></tt> as the first character of the set;
<tt class="docutils literal"><span class="pre">'^'</span></tt> elsewhere will simply match the <tt class="docutils literal"><span class="pre">'^'</span></tt> character.  For example,
<tt class="docutils literal"><span class="pre">[^5]</span></tt> will match any character except <tt class="docutils literal"><span class="pre">'5'</span></tt>, and <tt class="docutils literal"><span class="pre">[^^]</span></tt> will match any
character except <tt class="docutils literal"><span class="pre">'^'</span></tt>.</p>
<p class="last">Note that inside <tt class="docutils literal"><span class="pre">[]</span></tt> the special forms and special characters lose
their meanings and only the syntaxes described here are valid. For
example, <tt class="docutils literal"><span class="pre">+</span></tt>, <tt class="docutils literal"><span class="pre">*</span></tt>, <tt class="docutils literal"><span class="pre">(</span></tt>, <tt class="docutils literal"><span class="pre">)</span></tt>, and so on are treated as
literals inside <tt class="docutils literal"><span class="pre">[]</span></tt>, and backreferences cannot be used inside
<tt class="docutils literal"><span class="pre">[]</span></tt>.</p>
</dd>
<dt><tt class="docutils literal"><span class="pre">'|'</span></tt></dt>
<dd><tt class="docutils literal"><span class="pre">A|B</span></tt>, where A and B can be arbitrary REs, creates a regular expression that
will match either A or B.  An arbitrary number of REs can be separated by the
<tt class="docutils literal"><span class="pre">'|'</span></tt> in this way.  This can be used inside groups (see below) as well.  As
the target string is scanned, REs separated by <tt class="docutils literal"><span class="pre">'|'</span></tt> are tried from left to
right. When one pattern completely matches, that branch is accepted. This means
that once <tt class="docutils literal"><span class="pre">A</span></tt> matches, <tt class="docutils literal"><span class="pre">B</span></tt> will not be tested further, even if it would
produce a longer overall match.  In other words, the <tt class="docutils literal"><span class="pre">'|'</span></tt> operator is never
greedy.  To match a literal <tt class="docutils literal"><span class="pre">'|'</span></tt>, use <tt class="docutils literal"><span class="pre">\|</span></tt>, or enclose it inside a
character class, as in <tt class="docutils literal"><span class="pre">[|]</span></tt>.</dd>
<dt><tt class="docutils literal"><span class="pre">(...)</span></tt></dt>
<dd>Matches whatever regular expression is inside the parentheses, and indicates the
start and end of a group; the contents of a group can be retrieved after a match
has been performed, and can be matched later in the string with the <tt class="docutils literal"><span class="pre">\number</span></tt>
special sequence, described below.  To match the literals <tt class="docutils literal"><span class="pre">'('</span></tt> or <tt class="docutils literal"><span class="pre">')'</span></tt>,
use <tt class="docutils literal"><span class="pre">\(</span></tt> or <tt class="docutils literal"><span class="pre">\)</span></tt>, or enclose them inside a character class: <tt class="docutils literal"><span class="pre">[(]</span> <span class="pre">[)]</span></tt>.</dd>
<dt><tt class="docutils literal"><span class="pre">(?...)</span></tt></dt>
<dd>This is an extension notation (a <tt class="docutils literal"><span class="pre">'?'</span></tt> following a <tt class="docutils literal"><span class="pre">'('</span></tt> is not meaningful
otherwise).  The first character after the <tt class="docutils literal"><span class="pre">'?'</span></tt> determines what the meaning
and further syntax of the construct is. Extensions usually do not create a new
group; <tt class="docutils literal"><span class="pre">(?P&lt;name&gt;...)</span></tt> is the only exception to this rule. Following are the
currently supported extensions.</dd>
<dt><tt class="docutils literal"><span class="pre">(?aiLmsux)</span></tt></dt>
<dd><p class="first">(One or more letters from the set <tt class="docutils literal"><span class="pre">'a'</span></tt>, <tt class="docutils literal"><span class="pre">'i'</span></tt>, <tt class="docutils literal"><span class="pre">'L'</span></tt>, <tt class="docutils literal"><span class="pre">'m'</span></tt>,
<tt class="docutils literal"><span class="pre">'s'</span></tt>, <tt class="docutils literal"><span class="pre">'u'</span></tt>, <tt class="docutils literal"><span class="pre">'x'</span></tt>.)  The group matches the empty string; the
letters set the corresponding flags: <a class="reference internal" href="#re.A" title="re.A"><tt class="xref py py-const docutils literal"><span class="pre">re.A</span></tt></a> (ASCII-only matching),
<a class="reference internal" href="#re.I" title="re.I"><tt class="xref py py-const docutils literal"><span class="pre">re.I</span></tt></a> (ignore case), <a class="reference internal" href="#re.L" title="re.L"><tt class="xref py py-const docutils literal"><span class="pre">re.L</span></tt></a> (locale dependent),
<a class="reference internal" href="#re.M" title="re.M"><tt class="xref py py-const docutils literal"><span class="pre">re.M</span></tt></a> (multi-line), <a class="reference internal" href="#re.S" title="re.S"><tt class="xref py py-const docutils literal"><span class="pre">re.S</span></tt></a> (dot matches all),
and <a class="reference internal" href="#re.X" title="re.X"><tt class="xref py py-const docutils literal"><span class="pre">re.X</span></tt></a> (verbose), for the entire regular expression. (The
flags are described in <a class="reference internal" href="#contents-of-module-re"><em>Module Contents</em></a>.) This
is useful if you wish to include the flags as part of the regular
expression, instead of passing a <em>flag</em> argument to the
<a class="reference internal" href="#re.compile" title="re.compile"><tt class="xref py py-func docutils literal"><span class="pre">re.compile()</span></tt></a> function.</p>
<p class="last">Note that the <tt class="docutils literal"><span class="pre">(?x)</span></tt> flag changes how the expression is parsed. It should be
used first in the expression string, or after one or more whitespace characters.
If there are non-whitespace characters before the flag, the results are
undefined.</p>
</dd>
<dt><tt class="docutils literal"><span class="pre">(?:...)</span></tt></dt>
<dd>A non-capturing version of regular parentheses.  Matches whatever regular
expression is inside the parentheses, but the substring matched by the group
<em>cannot</em> be retrieved after performing a match or referenced later in the
pattern.</dd>
<dt><tt class="docutils literal"><span class="pre">(?P&lt;name&gt;...)</span></tt></dt>
<dd><p class="first">Similar to regular parentheses, but the substring matched by the group is
accessible within the rest of the regular expression via the symbolic group
name <em>name</em>.  Group names must be valid Python identifiers, and each group
name must be defined only once within a regular expression.  A symbolic group
is also a numbered group, just as if the group were not named.  So the group
named <tt class="docutils literal"><span class="pre">id</span></tt> in the example below can also be referenced as the numbered group
<tt class="docutils literal"><span class="pre">1</span></tt>.</p>
<p class="last">For example, if the pattern is <tt class="docutils literal"><span class="pre">(?P&lt;id&gt;[a-zA-Z_]\w*)</span></tt>, the group can be
referenced by its name in arguments to methods of match objects, such as
<tt class="docutils literal"><span class="pre">m.group('id')</span></tt> or <tt class="docutils literal"><span class="pre">m.end('id')</span></tt>, and also by name in the regular
expression itself (using <tt class="docutils literal"><span class="pre">(?P=id)</span></tt>) and replacement text given to
<tt class="docutils literal"><span class="pre">.sub()</span></tt> (using <tt class="docutils literal"><span class="pre">\g&lt;id&gt;</span></tt>).</p>
</dd>
<dt><tt class="docutils literal"><span class="pre">(?P=name)</span></tt></dt>
<dd>Matches whatever text was matched by the earlier group named <em>name</em>.</dd>
<dt><tt class="docutils literal"><span class="pre">(?#...)</span></tt></dt>
<dd>A comment; the contents of the parentheses are simply ignored.</dd>
<dt><tt class="docutils literal"><span class="pre">(?=...)</span></tt></dt>
<dd>Matches if <tt class="docutils literal"><span class="pre">...</span></tt> matches next, but doesn&#8217;t consume any of the string.  This is
called a lookahead assertion.  For example, <tt class="docutils literal"><span class="pre">Isaac</span> <span class="pre">(?=Asimov)</span></tt> will match
<tt class="docutils literal"><span class="pre">'Isaac</span> <span class="pre">'</span></tt> only if it&#8217;s followed by <tt class="docutils literal"><span class="pre">'Asimov'</span></tt>.</dd>
<dt><tt class="docutils literal"><span class="pre">(?!...)</span></tt></dt>
<dd>Matches if <tt class="docutils literal"><span class="pre">...</span></tt> doesn&#8217;t match next.  This is a negative lookahead assertion.
For example, <tt class="docutils literal"><span class="pre">Isaac</span> <span class="pre">(?!Asimov)</span></tt> will match <tt class="docutils literal"><span class="pre">'Isaac</span> <span class="pre">'</span></tt> only if it&#8217;s <em>not</em>
followed by <tt class="docutils literal"><span class="pre">'Asimov'</span></tt>.</dd>
<dt><tt class="docutils literal"><span class="pre">(?&lt;=...)</span></tt></dt>
<dd><p class="first">Matches if the current position in the string is preceded by a match for <tt class="docutils literal"><span class="pre">...</span></tt>
that ends at the current position.  This is called a <em class="dfn">positive lookbehind
assertion</em>. <tt class="docutils literal"><span class="pre">(?&lt;=abc)def</span></tt> will find a match in <tt class="docutils literal"><span class="pre">abcdef</span></tt>, since the
lookbehind will back up 3 characters and check if the contained pattern matches.
The contained pattern must only match strings of some fixed length, meaning that
<tt class="docutils literal"><span class="pre">abc</span></tt> or <tt class="docutils literal"><span class="pre">a|b</span></tt> are allowed, but <tt class="docutils literal"><span class="pre">a*</span></tt> and <tt class="docutils literal"><span class="pre">a{3,4}</span></tt> are not.  Note that
patterns which start with positive lookbehind assertions will never match at the
beginning of the string being searched; you will most likely want to use the
<a class="reference internal" href="#re.search" title="re.search"><tt class="xref py py-func docutils literal"><span class="pre">search()</span></tt></a> function rather than the <a class="reference internal" href="#re.match" title="re.match"><tt class="xref py py-func docutils literal"><span class="pre">match()</span></tt></a> function:</p>
<div class="highlight-python3"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="kn">import</span> <span class="nn">re</span>
<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="s">&#39;(?&lt;=abc)def&#39;</span><span class="p">,</span> <span class="s">&#39;abcdef&#39;</span><span class="p">)</span>
<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="go">&#39;def&#39;</span>
</pre></div>
</div>
<p>This example looks for a word following a hyphen:</p>
<div class="last highlight-python3"><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="s">&#39;(?&lt;=-)\w+&#39;</span><span class="p">,</span> <span class="s">&#39;spam-egg&#39;</span><span class="p">)</span>
<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="go">&#39;egg&#39;</span>
</pre></div>
</div>
</dd>
<dt><tt class="docutils literal"><span class="pre">(?&lt;!...)</span></tt></dt>
<dd>Matches if the current position in the string is not preceded by a match for
<tt class="docutils literal"><span class="pre">...</span></tt>.  This is called a <em class="dfn">negative lookbehind assertion</em>.  Similar to
positive lookbehind assertions, the contained pattern must only match strings of
some fixed length.  Patterns which start with negative lookbehind assertions may
match at the beginning of the string being searched.</dd>
<dt><tt class="docutils literal"><span class="pre">(?(id/name)yes-pattern|no-pattern)</span></tt></dt>
<dd>Will try to match with <tt class="docutils literal"><span class="pre">yes-pattern</span></tt> if the group with given <em>id</em> or
<em>name</em> exists, and with <tt class="docutils literal"><span class="pre">no-pattern</span></tt> if it doesn&#8217;t. <tt class="docutils literal"><span class="pre">no-pattern</span></tt> is
optional and can be omitted. For example,
<tt class="docutils literal"><span class="pre">(&lt;)?(\w+&#64;\w+(?:\.\w+)+)(?(1)&gt;|$)</span></tt> is a poor email matching pattern, which
will match with <tt class="docutils literal"><span class="pre">'&lt;user&#64;host.com&gt;'</span></tt> as well as <tt class="docutils literal"><span class="pre">'user&#64;host.com'</span></tt>, but
not with <tt class="docutils literal"><span class="pre">'&lt;user&#64;host.com'</span></tt> nor <tt class="docutils literal"><span class="pre">'user&#64;host.com&gt;'</span></tt> .</dd>
</dl>
<p>The special sequences consist of <tt class="docutils literal"><span class="pre">'\'</span></tt> and a character from the list below.
If the ordinary character is not on the list, then the resulting RE will match
the second character.  For example, <tt class="docutils literal"><span class="pre">\$</span></tt> matches the character <tt class="docutils literal"><span class="pre">'$'</span></tt>.</p>
<dl class="docutils">
<dt><tt class="docutils literal"><span class="pre">\number</span></tt></dt>
<dd>Matches the contents of the group of the same number.  Groups are numbered
starting from 1.  For example, <tt class="docutils literal"><span class="pre">(.+)</span> <span class="pre">\1</span></tt> matches <tt class="docutils literal"><span class="pre">'the</span> <span class="pre">the'</span></tt> or <tt class="docutils literal"><span class="pre">'55</span> <span class="pre">55'</span></tt>,
but not <tt class="docutils literal"><span class="pre">'the</span> <span class="pre">end'</span></tt> (note the space after the group).  This special sequence
can only be used to match one of the first 99 groups.  If the first digit of
<em>number</em> is 0, or <em>number</em> is 3 octal digits long, it will not be interpreted as
a group match, but as the character with octal value <em>number</em>. Inside the
<tt class="docutils literal"><span class="pre">'['</span></tt> and <tt class="docutils literal"><span class="pre">']'</span></tt> of a character class, all numeric escapes are treated as
characters.</dd>
<dt><tt class="docutils literal"><span class="pre">\A</span></tt></dt>
<dd>Matches only at the start of the string.</dd>
<dt><tt class="docutils literal"><span class="pre">\b</span></tt></dt>
<dd>Matches the empty string, but only at the beginning or end of a word.
A word is defined as a sequence of Unicode alphanumeric or underscore
characters, so the end of a word is indicated by whitespace or a
non-alphanumeric, non-underscore Unicode character. Note that
formally, <tt class="docutils literal"><span class="pre">\b</span></tt> is defined as the boundary between a <tt class="docutils literal"><span class="pre">\w</span></tt> and a
<tt class="docutils literal"><span class="pre">\W</span></tt> character (or vice versa). By default Unicode alphanumerics
are the ones used, but this can be changed by using the <a class="reference internal" href="#re.ASCII" title="re.ASCII"><tt class="xref py py-const docutils literal"><span class="pre">ASCII</span></tt></a>
flag.  Inside a character range, <tt class="docutils literal"><span class="pre">\b</span></tt> represents the backspace
character, for compatibility with Python&#8217;s string literals.</dd>
<dt><tt class="docutils literal"><span class="pre">\B</span></tt></dt>
<dd>Matches the empty string, but only when it is <em>not</em> at the beginning or end of a
word.  This is just the opposite of <tt class="docutils literal"><span class="pre">\b</span></tt>, so word characters are
Unicode alphanumerics or the underscore, although this can be changed
by using the <a class="reference internal" href="#re.ASCII" title="re.ASCII"><tt class="xref py py-const docutils literal"><span class="pre">ASCII</span></tt></a> flag.</dd>
<dt><tt class="docutils literal"><span class="pre">\d</span></tt></dt>
<dd><dl class="first last docutils">
<dt>For Unicode (str) patterns:</dt>
<dd>Matches any Unicode decimal digit (that is, any character in
Unicode character category [Nd]).  This includes <tt class="docutils literal"><span class="pre">[0-9]</span></tt>, and
also many other digit characters.  If the <a class="reference internal" href="#re.ASCII" title="re.ASCII"><tt class="xref py py-const docutils literal"><span class="pre">ASCII</span></tt></a> flag is
used only <tt class="docutils literal"><span class="pre">[0-9]</span></tt> is matched (but the flag affects the entire
regular expression, so in such cases using an explicit <tt class="docutils literal"><span class="pre">[0-9]</span></tt>
may be a better choice).</dd>
<dt>For 8-bit (bytes) patterns:</dt>
<dd>Matches any decimal digit; this is equivalent to <tt class="docutils literal"><span class="pre">[0-9]</span></tt>.</dd>
</dl>
</dd>
<dt><tt class="docutils literal"><span class="pre">\D</span></tt></dt>
<dd>Matches any character which is not a Unicode decimal digit. This is
the opposite of <tt class="docutils literal"><span class="pre">\d</span></tt>. If the <a class="reference internal" href="#re.ASCII" title="re.ASCII"><tt class="xref py py-const docutils literal"><span class="pre">ASCII</span></tt></a> flag is used this
becomes the equivalent of <tt class="docutils literal"><span class="pre">[^0-9]</span></tt> (but the flag affects the entire
regular expression, so in such cases using an explicit <tt class="docutils literal"><span class="pre">[^0-9]</span></tt> may
be a better choice).</dd>
<dt><tt class="docutils literal"><span class="pre">\s</span></tt></dt>
<dd><dl class="first last docutils">
<dt>For Unicode (str) patterns:</dt>
<dd>Matches Unicode whitespace characters (which includes
<tt class="docutils literal"><span class="pre">[</span> <span class="pre">\t\n\r\f\v]</span></tt>, and also many other characters, for example the
non-breaking spaces mandated by typography rules in many
languages). If the <a class="reference internal" href="#re.ASCII" title="re.ASCII"><tt class="xref py py-const docutils literal"><span class="pre">ASCII</span></tt></a> flag is used, only
<tt class="docutils literal"><span class="pre">[</span> <span class="pre">\t\n\r\f\v]</span></tt> is matched (but the flag affects the entire
regular expression, so in such cases using an explicit
<tt class="docutils literal"><span class="pre">[</span> <span class="pre">\t\n\r\f\v]</span></tt> may be a better choice).</dd>
<dt>For 8-bit (bytes) patterns:</dt>
<dd>Matches characters considered whitespace in the ASCII character set;
this is equivalent to <tt class="docutils literal"><span class="pre">[</span> <span class="pre">\t\n\r\f\v]</span></tt>.</dd>
</dl>
</dd>
<dt><tt class="docutils literal"><span class="pre">\S</span></tt></dt>
<dd>Matches any character which is not a Unicode whitespace character. This is
the opposite of <tt class="docutils literal"><span class="pre">\s</span></tt>. If the <a class="reference internal" href="#re.ASCII" title="re.ASCII"><tt class="xref py py-const docutils literal"><span class="pre">ASCII</span></tt></a> flag is used this
becomes the equivalent of <tt class="docutils literal"><span class="pre">[^</span> <span class="pre">\t\n\r\f\v]</span></tt> (but the flag affects the entire
regular expression, so in such cases using an explicit <tt class="docutils literal"><span class="pre">[^</span> <span class="pre">\t\n\r\f\v]</span></tt> may
be a better choice).</dd>
<dt><tt class="docutils literal"><span class="pre">\w</span></tt></dt>
<dd><dl class="first last docutils">
<dt>For Unicode (str) patterns:</dt>
<dd>Matches Unicode word characters; this includes most characters
that can be part of a word in any language, as well as numbers and
the underscore. If the <a class="reference internal" href="#re.ASCII" title="re.ASCII"><tt class="xref py py-const docutils literal"><span class="pre">ASCII</span></tt></a> flag is used, only
<tt class="docutils literal"><span class="pre">[a-zA-Z0-9_]</span></tt> is matched (but the flag affects the entire
regular expression, so in such cases using an explicit
<tt class="docutils literal"><span class="pre">[a-zA-Z0-9_]</span></tt> may be a better choice).</dd>
<dt>For 8-bit (bytes) patterns:</dt>
<dd>Matches characters considered alphanumeric in the ASCII character set;
this is equivalent to <tt class="docutils literal"><span class="pre">[a-zA-Z0-9_]</span></tt>.</dd>
</dl>
</dd>
<dt><tt class="docutils literal"><span class="pre">\W</span></tt></dt>
<dd>Matches any character which is not a Unicode word character. This is
the opposite of <tt class="docutils literal"><span class="pre">\w</span></tt>. If the <a class="reference internal" href="#re.ASCII" title="re.ASCII"><tt class="xref py py-const docutils literal"><span class="pre">ASCII</span></tt></a> flag is used this
becomes the equivalent of <tt class="docutils literal"><span class="pre">[^a-zA-Z0-9_]</span></tt> (but the flag affects the
entire regular expression, so in such cases using an explicit
<tt class="docutils literal"><span class="pre">[^a-zA-Z0-9_]</span></tt> may be a better choice).</dd>
<dt><tt class="docutils literal"><span class="pre">\Z</span></tt></dt>
<dd>Matches only at the end of the string.</dd>
</dl>
<p>Most of the standard escapes supported by Python string literals are also
accepted by the regular expression parser:</p>
<div class="highlight-python3"><div class="highlight"><pre>\<span class="n">a</span>      \<span class="n">b</span>      \<span class="n">f</span>      \<span class="n">n</span>
\<span class="n">r</span>      \<span class="n">t</span>      \<span class="n">v</span>      \<span class="n">x</span>
\\
</pre></div>
</div>
<p>Octal escapes are included in a limited form: If the first digit is a 0, or if
there are three octal digits, it is considered an octal escape. Otherwise, it is
a group reference.  As for string literals, octal escapes are always at most
three digits in length.</p>
</div>
<div class="section" id="matching-vs-searching">
<span id="matching-searching"></span><h2>6.2.2. Matching vs Searching<a class="headerlink" href="#matching-vs-searching" title="Permalink to this headline">¶</a></h2>
<p>Python offers two different primitive operations based on regular expressions:
<strong>match</strong> checks for a match only at the beginning of the string, while
<strong>search</strong> checks for a match anywhere in the string (this is what Perl does
by default).</p>
<p>Note that match may differ from search even when using a regular expression
beginning with <tt class="docutils literal"><span class="pre">'^'</span></tt>: <tt class="docutils literal"><span class="pre">'^'</span></tt> matches only at the start of the string, or in
<a class="reference internal" href="#re.MULTILINE" title="re.MULTILINE"><tt class="xref py py-const docutils literal"><span class="pre">MULTILINE</span></tt></a> mode also immediately following a newline.  The &#8220;match&#8221;
operation succeeds only if the pattern matches at the start of the string
regardless of mode, or at the starting position given by the optional <em>pos</em>
argument regardless of whether a newline precedes it.</p>
<div class="highlight-python3"><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="s">&quot;c&quot;</span><span class="p">,</span> <span class="s">&quot;abcdef&quot;</span><span class="p">)</span>  <span class="c"># No match</span>
<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="s">&quot;c&quot;</span><span class="p">,</span> <span class="s">&quot;abcdef&quot;</span><span class="p">)</span> <span class="c"># Match</span>
<span class="go">&lt;_sre.SRE_Match object at ...&gt;</span>
</pre></div>
</div>
</div>
<div class="section" id="module-contents">
<span id="contents-of-module-re"></span><h2>6.2.3. Module Contents<a class="headerlink" href="#module-contents" title="Permalink to this headline">¶</a></h2>
<p>The module defines several functions, constants, and an exception. Some of the
functions are simplified versions of the full featured methods for compiled
regular expressions.  Most non-trivial applications always use the compiled
form.</p>
<dl class="function">
<dt id="re.compile">
<tt class="descclassname">re.</tt><tt class="descname">compile</tt><big>(</big><em>pattern</em>, <em>flags=0</em><big>)</big><a class="headerlink" href="#re.compile" title="Permalink to this definition">¶</a></dt>
<dd><p>Compile a regular expression pattern into a regular expression object, which
can be used for matching using its <a class="reference internal" href="#re.match" title="re.match"><tt class="xref py py-func docutils literal"><span class="pre">match()</span></tt></a> and <a class="reference internal" href="#re.search" title="re.search"><tt class="xref py py-func docutils literal"><span class="pre">search()</span></tt></a> methods,
described below.</p>
<p>The expression&#8217;s behaviour can be modified by specifying a <em>flags</em> value.
Values can be any of the following variables, combined using bitwise OR (the
<tt class="docutils literal"><span class="pre">|</span></tt> operator).</p>
<p>The sequence</p>
<div class="highlight-python3"><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>
<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>
</pre></div>
</div>
<p>is equivalent to</p>
<div class="highlight-python3"><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>
</pre></div>
</div>
<p>but using <a class="reference internal" href="#re.compile" title="re.compile"><tt class="xref py py-func docutils literal"><span class="pre">re.compile()</span></tt></a> and saving the resulting regular expression
object for reuse is more efficient when the expression will be used several
times in a single program.</p>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last">The compiled versions of the most recent patterns passed to
<a class="reference internal" href="#re.match" title="re.match"><tt class="xref py py-func docutils literal"><span class="pre">re.match()</span></tt></a>, <a class="reference internal" href="#re.search" title="re.search"><tt class="xref py py-func docutils literal"><span class="pre">re.search()</span></tt></a> or <a class="reference internal" href="#re.compile" title="re.compile"><tt class="xref py py-func docutils literal"><span class="pre">re.compile()</span></tt></a> are cached, so
programs that use only a few regular expressions at a time needn&#8217;t worry
about compiling regular expressions.</p>
</div>
</dd></dl>

<dl class="data">
<dt id="re.A">
<tt class="descclassname">re.</tt><tt class="descname">A</tt><a class="headerlink" href="#re.A" title="Permalink to this definition">¶</a></dt>
<dt id="re.ASCII">
<tt class="descclassname">re.</tt><tt class="descname">ASCII</tt><a class="headerlink" href="#re.ASCII" title="Permalink to this definition">¶</a></dt>
<dd><p>Make <tt class="docutils literal"><span class="pre">\w</span></tt>, <tt class="docutils literal"><span class="pre">\W</span></tt>, <tt class="docutils literal"><span class="pre">\b</span></tt>, <tt class="docutils literal"><span class="pre">\B</span></tt>, <tt class="docutils literal"><span class="pre">\d</span></tt>, <tt class="docutils literal"><span class="pre">\D</span></tt>, <tt class="docutils literal"><span class="pre">\s</span></tt> and <tt class="docutils literal"><span class="pre">\S</span></tt>
perform ASCII-only matching instead of full Unicode matching.  This is only
meaningful for Unicode patterns, and is ignored for byte patterns.</p>
<p>Note that for backward compatibility, the <tt class="xref py py-const docutils literal"><span class="pre">re.U</span></tt> flag still
exists (as well as its synonym <tt class="xref py py-const docutils literal"><span class="pre">re.UNICODE</span></tt> and its embedded
counterpart <tt class="docutils literal"><span class="pre">(?u)</span></tt>), but these are redundant in Python 3 since
matches are Unicode by default for strings (and Unicode matching
isn&#8217;t allowed for bytes).</p>
</dd></dl>

<dl class="data">
<dt id="re.I">
<tt class="descclassname">re.</tt><tt class="descname">I</tt><a class="headerlink" href="#re.I" title="Permalink to this definition">¶</a></dt>
<dt id="re.IGNORECASE">
<tt class="descclassname">re.</tt><tt class="descname">IGNORECASE</tt><a class="headerlink" href="#re.IGNORECASE" title="Permalink to this definition">¶</a></dt>
<dd><p>Perform case-insensitive matching; expressions like <tt class="docutils literal"><span class="pre">[A-Z]</span></tt> will match
lowercase letters, too.  This is not affected by the current locale
and works for Unicode characters as expected.</p>
</dd></dl>

<dl class="data">
<dt id="re.L">
<tt class="descclassname">re.</tt><tt class="descname">L</tt><a class="headerlink" href="#re.L" title="Permalink to this definition">¶</a></dt>
<dt id="re.LOCALE">
<tt class="descclassname">re.</tt><tt class="descname">LOCALE</tt><a class="headerlink" href="#re.LOCALE" title="Permalink to this definition">¶</a></dt>
<dd><p>Make <tt class="docutils literal"><span class="pre">\w</span></tt>, <tt class="docutils literal"><span class="pre">\W</span></tt>, <tt class="docutils literal"><span class="pre">\b</span></tt>, <tt class="docutils literal"><span class="pre">\B</span></tt>, <tt class="docutils literal"><span class="pre">\s</span></tt> and <tt class="docutils literal"><span class="pre">\S</span></tt> dependent on the
current locale. The use of this flag is discouraged as the locale mechanism
is very unreliable, and it only handles one &#8220;culture&#8221; at a time anyway;
you should use Unicode matching instead, which is the default in Python 3
for Unicode (str) patterns.</p>
</dd></dl>

<dl class="data">
<dt id="re.M">
<tt class="descclassname">re.</tt><tt class="descname">M</tt><a class="headerlink" href="#re.M" title="Permalink to this definition">¶</a></dt>
<dt id="re.MULTILINE">
<tt class="descclassname">re.</tt><tt class="descname">MULTILINE</tt><a class="headerlink" href="#re.MULTILINE" title="Permalink to this definition">¶</a></dt>
<dd><p>When specified, the pattern character <tt class="docutils literal"><span class="pre">'^'</span></tt> matches at the beginning of the
string and at the beginning of each line (immediately following each newline);
and the pattern character <tt class="docutils literal"><span class="pre">'$'</span></tt> matches at the end of the string and at the
end of each line (immediately preceding each newline).  By default, <tt class="docutils literal"><span class="pre">'^'</span></tt>
matches only at the beginning of the string, and <tt class="docutils literal"><span class="pre">'$'</span></tt> only at the end of the
string and immediately before the newline (if any) at the end of the string.</p>
</dd></dl>

<dl class="data">
<dt id="re.S">
<tt class="descclassname">re.</tt><tt class="descname">S</tt><a class="headerlink" href="#re.S" title="Permalink to this definition">¶</a></dt>
<dt id="re.DOTALL">
<tt class="descclassname">re.</tt><tt class="descname">DOTALL</tt><a class="headerlink" href="#re.DOTALL" title="Permalink to this definition">¶</a></dt>
<dd><p>Make the <tt class="docutils literal"><span class="pre">'.'</span></tt> special character match any character at all, including a
newline; without this flag, <tt class="docutils literal"><span class="pre">'.'</span></tt> will match anything <em>except</em> a newline.</p>
</dd></dl>

<dl class="data">
<dt id="re.X">
<tt class="descclassname">re.</tt><tt class="descname">X</tt><a class="headerlink" href="#re.X" title="Permalink to this definition">¶</a></dt>
<dt id="re.VERBOSE">
<tt class="descclassname">re.</tt><tt class="descname">VERBOSE</tt><a class="headerlink" href="#re.VERBOSE" title="Permalink to this definition">¶</a></dt>
<dd><p>This flag allows you to write regular expressions that look nicer. Whitespace
within the pattern is ignored, except when in a character class or preceded by
an unescaped backslash, and, when a line contains a <tt class="docutils literal"><span class="pre">'#'</span></tt> neither in a
character class or preceded by an unescaped backslash, all characters from the
leftmost such <tt class="docutils literal"><span class="pre">'#'</span></tt> through the end of the line are ignored.</p>
<p>That means that the two following regular expression objects that match a
decimal number are functionally equal:</p>
<div class="highlight-python3"><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="s">r&quot;&quot;&quot;\d +  # the integral part</span>
<span class="s">                   \.    # the decimal point</span>
<span class="s">                   \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>
<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="s">r&quot;\d+\.\d*&quot;</span><span class="p">)</span>
</pre></div>
</div>
</dd></dl>

<dl class="function">
<dt id="re.search">
<tt class="descclassname">re.</tt><tt class="descname">search</tt><big>(</big><em>pattern</em>, <em>string</em>, <em>flags=0</em><big>)</big><a class="headerlink" href="#re.search" title="Permalink to this definition">¶</a></dt>
<dd><p>Scan through <em>string</em> looking for a location where the regular expression
<em>pattern</em> produces a match, and return a corresponding <a class="reference internal" href="#match-objects"><em>match object</em></a>.  Return <tt class="xref docutils literal"><span class="pre">None</span></tt> if no position in the string matches the
pattern; note that this is different from finding a zero-length match at some
point in the string.</p>
</dd></dl>

<dl class="function">
<dt id="re.match">
<tt class="descclassname">re.</tt><tt class="descname">match</tt><big>(</big><em>pattern</em>, <em>string</em>, <em>flags=0</em><big>)</big><a class="headerlink" href="#re.match" title="Permalink to this definition">¶</a></dt>
<dd><p>If zero or more characters at the beginning of <em>string</em> match the regular
expression <em>pattern</em>, return a corresponding <a class="reference internal" href="#match-objects"><em>match object</em></a>.  Return <tt class="xref docutils literal"><span class="pre">None</span></tt> if the string does not match the pattern;
note that this is different from a zero-length match.</p>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last">If you want to locate a match anywhere in <em>string</em>, use <a class="reference internal" href="#re.search" title="re.search"><tt class="xref py py-func docutils literal"><span class="pre">search()</span></tt></a>
instead.</p>
</div>
</dd></dl>

<dl class="function">
<dt id="re.split">
<tt class="descclassname">re.</tt><tt class="descname">split</tt><big>(</big><em>pattern</em>, <em>string</em>, <em>maxsplit=0</em>, <em>flags=0</em><big>)</big><a class="headerlink" href="#re.split" title="Permalink to this definition">¶</a></dt>
<dd><p>Split <em>string</em> by the occurrences of <em>pattern</em>.  If capturing parentheses are
used in <em>pattern</em>, then the text of all groups in the pattern are also returned
as part of the resulting list. If <em>maxsplit</em> is nonzero, at most <em>maxsplit</em>
splits occur, and the remainder of the string is returned as the final element
of the list.</p>
<div class="highlight-python3"><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="s">&#39;\W+&#39;</span><span class="p">,</span> <span class="s">&#39;Words, words, words.&#39;</span><span class="p">)</span>
<span class="go">[&#39;Words&#39;, &#39;words&#39;, &#39;words&#39;, &#39;&#39;]</span>
<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="s">&#39;(\W+)&#39;</span><span class="p">,</span> <span class="s">&#39;Words, words, words.&#39;</span><span class="p">)</span>
<span class="go">[&#39;Words&#39;, &#39;, &#39;, &#39;words&#39;, &#39;, &#39;, &#39;words&#39;, &#39;.&#39;, &#39;&#39;]</span>
<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="s">&#39;\W+&#39;</span><span class="p">,</span> <span class="s">&#39;Words, words, words.&#39;</span><span class="p">,</span> <span class="mi">1</span><span class="p">)</span>
<span class="go">[&#39;Words&#39;, &#39;words, words.&#39;]</span>
<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="s">&#39;[a-f]+&#39;</span><span class="p">,</span> <span class="s">&#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>
<span class="go">[&#39;0&#39;, &#39;3&#39;, &#39;9&#39;]</span>
</pre></div>
</div>
<p>If there are capturing groups in the separator and it matches at the start of
the string, the result will start with an empty string.  The same holds for
the end of the string:</p>
<div class="highlight-python3"><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="s">&#39;(\W+)&#39;</span><span class="p">,</span> <span class="s">&#39;...words, words...&#39;</span><span class="p">)</span>
<span class="go">[&#39;&#39;, &#39;...&#39;, &#39;words&#39;, &#39;, &#39;, &#39;words&#39;, &#39;...&#39;, &#39;&#39;]</span>
</pre></div>
</div>
<p>That way, separator components are always found at the same relative
indices within the result list (e.g., if there&#8217;s one capturing group
in the separator, the 0th, the 2nd and so forth).</p>
<p>Note that <em>split</em> will never split a string on an empty pattern match.
For example:</p>
<div class="highlight-python3"><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="s">&#39;x*&#39;</span><span class="p">,</span> <span class="s">&#39;foo&#39;</span><span class="p">)</span>
<span class="go">[&#39;foo&#39;]</span>
<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="s">&quot;(?m)^$&quot;</span><span class="p">,</span> <span class="s">&quot;foo</span><span class="se">\n\n</span><span class="s">bar</span><span class="se">\n</span><span class="s">&quot;</span><span class="p">)</span>
<span class="go">[&#39;foo\n\nbar\n&#39;]</span>
</pre></div>
</div>
<p class="versionchanged">
<span class="versionmodified">Changed in version 3.1: </span>Added the optional flags argument.</p>
</dd></dl>

<dl class="function">
<dt id="re.findall">
<tt class="descclassname">re.</tt><tt class="descname">findall</tt><big>(</big><em>pattern</em>, <em>string</em>, <em>flags=0</em><big>)</big><a class="headerlink" href="#re.findall" title="Permalink to this definition">¶</a></dt>
<dd><p>Return all non-overlapping matches of <em>pattern</em> in <em>string</em>, as a list of
strings.  The <em>string</em> is scanned left-to-right, and matches are returned in
the order found.  If one or more groups are present in the pattern, return a
list of groups; this will be a list of tuples if the pattern has more than
one group.  Empty matches are included in the result unless they touch the
beginning of another match.</p>
</dd></dl>

<dl class="function">
<dt id="re.finditer">
<tt class="descclassname">re.</tt><tt class="descname">finditer</tt><big>(</big><em>pattern</em>, <em>string</em>, <em>flags=0</em><big>)</big><a class="headerlink" href="#re.finditer" title="Permalink to this definition">¶</a></dt>
<dd><p>Return an <a class="reference internal" href="../glossary.html#term-iterator"><em class="xref std std-term">iterator</em></a> yielding <a class="reference internal" href="#match-objects"><em>match objects</em></a> over
all non-overlapping matches for the RE <em>pattern</em> in <em>string</em>.  The <em>string</em>
is scanned left-to-right, and matches are returned in the order found.  Empty
matches are included in the result unless they touch the beginning of another
match.</p>
</dd></dl>

<dl class="function">
<dt id="re.sub">
<tt class="descclassname">re.</tt><tt class="descname">sub</tt><big>(</big><em>pattern</em>, <em>repl</em>, <em>string</em>, <em>count=0</em>, <em>flags=0</em><big>)</big><a class="headerlink" href="#re.sub" title="Permalink to this definition">¶</a></dt>
<dd><p>Return the string obtained by replacing the leftmost non-overlapping occurrences
of <em>pattern</em> in <em>string</em> by the replacement <em>repl</em>.  If the pattern isn&#8217;t found,
<em>string</em> is returned unchanged.  <em>repl</em> can be a string or a function; if it is
a string, any backslash escapes in it are processed.  That is, <tt class="docutils literal"><span class="pre">\n</span></tt> is
converted to a single newline character, <tt class="docutils literal"><span class="pre">\r</span></tt> is converted to a carriage return, and
so forth.  Unknown escapes such as <tt class="docutils literal"><span class="pre">\j</span></tt> are left alone.  Backreferences, such
as <tt class="docutils literal"><span class="pre">\6</span></tt>, are replaced with the substring matched by group 6 in the pattern.
For example:</p>
<div class="highlight-python3"><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="s">r&#39;def\s+([a-zA-Z_][a-zA-Z_0-9]*)\s*\(\s*\):&#39;</span><span class="p">,</span>
<span class="gp">... </span>       <span class="s">r&#39;static PyObject*\npy_\1(void)\n{&#39;</span><span class="p">,</span>
<span class="gp">... </span>       <span class="s">&#39;def myfunc():&#39;</span><span class="p">)</span>
<span class="go">&#39;static PyObject*\npy_myfunc(void)\n{&#39;</span>
</pre></div>
</div>
<p>If <em>repl</em> is a function, it is called for every non-overlapping occurrence of
<em>pattern</em>.  The function takes a single match object argument, and returns the
replacement string.  For example:</p>
<div class="highlight-python3"><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>
<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="s">&#39;-&#39;</span><span class="p">:</span> <span class="k">return</span> <span class="s">&#39; &#39;</span>
<span class="gp">... </span>    <span class="k">else</span><span class="p">:</span> <span class="k">return</span> <span class="s">&#39;-&#39;</span>
<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="s">&#39;-{1,2}&#39;</span><span class="p">,</span> <span class="n">dashrepl</span><span class="p">,</span> <span class="s">&#39;pro----gram-files&#39;</span><span class="p">)</span>
<span class="go">&#39;pro--gram files&#39;</span>
<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="s">r&#39;\sAND\s&#39;</span><span class="p">,</span> <span class="s">&#39; &amp; &#39;</span><span class="p">,</span> <span class="s">&#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>
<span class="go">&#39;Baked Beans &amp; Spam&#39;</span>
</pre></div>
</div>
<p>The pattern may be a string or an RE object.</p>
<p>The optional argument <em>count</em> is the maximum number of pattern occurrences to be
replaced; <em>count</em> must be a non-negative integer.  If omitted or zero, all
occurrences will be replaced. Empty matches for the pattern are replaced only
when not adjacent to a previous match, so <tt class="docutils literal"><span class="pre">sub('x*',</span> <span class="pre">'-',</span> <span class="pre">'abc')</span></tt> returns
<tt class="docutils literal"><span class="pre">'-a-b-c-'</span></tt>.</p>
<p>In addition to character escapes and backreferences as described above,
<tt class="docutils literal"><span class="pre">\g&lt;name&gt;</span></tt> will use the substring matched by the group named <tt class="docutils literal"><span class="pre">name</span></tt>, as
defined by the <tt class="docutils literal"><span class="pre">(?P&lt;name&gt;...)</span></tt> syntax. <tt class="docutils literal"><span class="pre">\g&lt;number&gt;</span></tt> uses the corresponding
group number; <tt class="docutils literal"><span class="pre">\g&lt;2&gt;</span></tt> is therefore equivalent to <tt class="docutils literal"><span class="pre">\2</span></tt>, but isn&#8217;t ambiguous
in a replacement such as <tt class="docutils literal"><span class="pre">\g&lt;2&gt;0</span></tt>.  <tt class="docutils literal"><span class="pre">\20</span></tt> would be interpreted as a
reference to group 20, not a reference to group 2 followed by the literal
character <tt class="docutils literal"><span class="pre">'0'</span></tt>.  The backreference <tt class="docutils literal"><span class="pre">\g&lt;0&gt;</span></tt> substitutes in the entire
substring matched by the RE.</p>
<p class="versionchanged">
<span class="versionmodified">Changed in version 3.1: </span>Added the optional flags argument.</p>
</dd></dl>

<dl class="function">
<dt id="re.subn">
<tt class="descclassname">re.</tt><tt class="descname">subn</tt><big>(</big><em>pattern</em>, <em>repl</em>, <em>string</em>, <em>count=0</em>, <em>flags=0</em><big>)</big><a class="headerlink" href="#re.subn" title="Permalink to this definition">¶</a></dt>
<dd><p>Perform the same operation as <a class="reference internal" href="#re.sub" title="re.sub"><tt class="xref py py-func docutils literal"><span class="pre">sub()</span></tt></a>, but return a tuple <tt class="docutils literal"><span class="pre">(new_string,</span>
<span class="pre">number_of_subs_made)</span></tt>.</p>
<p class="versionchanged">
<span class="versionmodified">Changed in version 3.1: </span>Added the optional flags argument.</p>
</dd></dl>

<dl class="function">
<dt id="re.escape">
<tt class="descclassname">re.</tt><tt class="descname">escape</tt><big>(</big><em>string</em><big>)</big><a class="headerlink" href="#re.escape" title="Permalink to this definition">¶</a></dt>
<dd><p>Return <em>string</em> with all non-alphanumerics backslashed; this is useful if you
want to match an arbitrary literal string that may have regular expression
metacharacters in it.</p>
</dd></dl>

<dl class="function">
<dt id="re.purge">
<tt class="descclassname">re.</tt><tt class="descname">purge</tt><big>(</big><big>)</big><a class="headerlink" href="#re.purge" title="Permalink to this definition">¶</a></dt>
<dd><p>Clear the regular expression cache.</p>
</dd></dl>

<dl class="exception">
<dt id="re.error">
<em class="property">exception </em><tt class="descclassname">re.</tt><tt class="descname">error</tt><a class="headerlink" href="#re.error" title="Permalink to this definition">¶</a></dt>
<dd><p>Exception raised when a string passed to one of the functions here is not a
valid regular expression (for example, it might contain unmatched parentheses)
or when some other error occurs during compilation or matching.  It is never an
error if a string contains no match for a pattern.</p>
</dd></dl>

</div>
<div class="section" id="regular-expression-objects">
<span id="re-objects"></span><h2>6.2.4. Regular Expression Objects<a class="headerlink" href="#regular-expression-objects" title="Permalink to this headline">¶</a></h2>
<p>Compiled regular expression objects support the following methods and
attributes.</p>
<dl class="method">
<dt id="re.regex.search">
<tt class="descclassname">regex.</tt><tt class="descname">search</tt><big>(</big><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><big>)</big><a class="headerlink" href="#re.regex.search" title="Permalink to this definition">¶</a></dt>
<dd><p>Scan through <em>string</em> looking for a location where this regular expression
produces a match, and return a corresponding <a class="reference internal" href="#match-objects"><em>match object</em></a>.  Return <tt class="xref docutils literal"><span class="pre">None</span></tt> if no position in the string matches the
pattern; note that this is different from finding a zero-length match at some
point in the string.</p>
<p>The optional second parameter <em>pos</em> gives an index in the string where the
search is to start; it defaults to <tt class="docutils literal"><span class="pre">0</span></tt>.  This is not completely equivalent to
slicing the string; the <tt class="docutils literal"><span class="pre">'^'</span></tt> pattern character matches at the real beginning
of the string and at positions just after a newline, but not necessarily at the
index where the search is to start.</p>
<p>The optional parameter <em>endpos</em> limits how far the string will be searched; it
will be as if the string is <em>endpos</em> characters long, so only the characters
from <em>pos</em> to <tt class="docutils literal"><span class="pre">endpos</span> <span class="pre">-</span> <span class="pre">1</span></tt> will be searched for a match.  If <em>endpos</em> is less
than <em>pos</em>, no match will be found, otherwise, if <em>rx</em> is a compiled regular
expression object, <tt class="docutils literal"><span class="pre">rx.search(string,</span> <span class="pre">0,</span> <span class="pre">50)</span></tt> is equivalent to
<tt class="docutils literal"><span class="pre">rx.search(string[:50],</span> <span class="pre">0)</span></tt>.</p>
<div class="highlight-python3"><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="s">&quot;d&quot;</span><span class="p">)</span>
<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="s">&quot;dog&quot;</span><span class="p">)</span>     <span class="c"># Match at index 0</span>
<span class="go">&lt;_sre.SRE_Match object at ...&gt;</span>
<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="s">&quot;dog&quot;</span><span class="p">,</span> <span class="mi">1</span><span class="p">)</span>  <span class="c"># No match; search doesn&#39;t include the &quot;d&quot;</span>
</pre></div>
</div>
</dd></dl>

<dl class="method">
<dt id="re.regex.match">
<tt class="descclassname">regex.</tt><tt class="descname">match</tt><big>(</big><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><big>)</big><a class="headerlink" href="#re.regex.match" title="Permalink to this definition">¶</a></dt>
<dd><p>If zero or more characters at the <em>beginning</em> of <em>string</em> match this regular
expression, return a corresponding <a class="reference internal" href="#match-objects"><em>match object</em></a>.
Return <tt class="xref docutils literal"><span class="pre">None</span></tt> if the string does not match the pattern; note that this is
different from a zero-length match.</p>
<p>The optional <em>pos</em> and <em>endpos</em> parameters have the same meaning as for the
<a class="reference internal" href="#re.regex.search" title="re.regex.search"><tt class="xref py py-meth docutils literal"><span class="pre">search()</span></tt></a> method.</p>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last">If you want to locate a match anywhere in <em>string</em>, use
<a class="reference internal" href="#re.regex.search" title="re.regex.search"><tt class="xref py py-meth docutils literal"><span class="pre">search()</span></tt></a> instead.</p>
</div>
<div class="highlight-python3"><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="s">&quot;o&quot;</span><span class="p">)</span>
<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="s">&quot;dog&quot;</span><span class="p">)</span>      <span class="c"># No match as &quot;o&quot; is not at the start of &quot;dog&quot;.</span>
<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="s">&quot;dog&quot;</span><span class="p">,</span> <span class="mi">1</span><span class="p">)</span>   <span class="c"># Match as &quot;o&quot; is the 2nd character of &quot;dog&quot;.</span>
<span class="go">&lt;_sre.SRE_Match object at ...&gt;</span>
</pre></div>
</div>
</dd></dl>

<dl class="method">
<dt id="re.regex.split">
<tt class="descclassname">regex.</tt><tt class="descname">split</tt><big>(</big><em>string</em>, <em>maxsplit=0</em><big>)</big><a class="headerlink" href="#re.regex.split" title="Permalink to this definition">¶</a></dt>
<dd><p>Identical to the <a class="reference internal" href="#re.split" title="re.split"><tt class="xref py py-func docutils literal"><span class="pre">split()</span></tt></a> function, using the compiled pattern.</p>
</dd></dl>

<dl class="method">
<dt id="re.regex.findall">
<tt class="descclassname">regex.</tt><tt class="descname">findall</tt><big>(</big><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><big>)</big><a class="headerlink" href="#re.regex.findall" title="Permalink to this definition">¶</a></dt>
<dd><p>Similar to the <a class="reference internal" href="#re.findall" title="re.findall"><tt class="xref py py-func docutils literal"><span class="pre">findall()</span></tt></a> function, using the compiled pattern, but
also accepts optional <em>pos</em> and <em>endpos</em> parameters that limit the search
region like for <a class="reference internal" href="#re.match" title="re.match"><tt class="xref py py-meth docutils literal"><span class="pre">match()</span></tt></a>.</p>
</dd></dl>

<dl class="method">
<dt id="re.regex.finditer">
<tt class="descclassname">regex.</tt><tt class="descname">finditer</tt><big>(</big><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><big>)</big><a class="headerlink" href="#re.regex.finditer" title="Permalink to this definition">¶</a></dt>
<dd><p>Similar to the <a class="reference internal" href="#re.finditer" title="re.finditer"><tt class="xref py py-func docutils literal"><span class="pre">finditer()</span></tt></a> function, using the compiled pattern, but
also accepts optional <em>pos</em> and <em>endpos</em> parameters that limit the search
region like for <a class="reference internal" href="#re.match" title="re.match"><tt class="xref py py-meth docutils literal"><span class="pre">match()</span></tt></a>.</p>
</dd></dl>

<dl class="method">
<dt id="re.regex.sub">
<tt class="descclassname">regex.</tt><tt class="descname">sub</tt><big>(</big><em>repl</em>, <em>string</em>, <em>count=0</em><big>)</big><a class="headerlink" href="#re.regex.sub" title="Permalink to this definition">¶</a></dt>
<dd><p>Identical to the <a class="reference internal" href="#re.sub" title="re.sub"><tt class="xref py py-func docutils literal"><span class="pre">sub()</span></tt></a> function, using the compiled pattern.</p>
</dd></dl>

<dl class="method">
<dt id="re.regex.subn">
<tt class="descclassname">regex.</tt><tt class="descname">subn</tt><big>(</big><em>repl</em>, <em>string</em>, <em>count=0</em><big>)</big><a class="headerlink" href="#re.regex.subn" title="Permalink to this definition">¶</a></dt>
<dd><p>Identical to the <a class="reference internal" href="#re.subn" title="re.subn"><tt class="xref py py-func docutils literal"><span class="pre">subn()</span></tt></a> function, using the compiled pattern.</p>
</dd></dl>

<dl class="attribute">
<dt id="re.regex.flags">
<tt class="descclassname">regex.</tt><tt class="descname">flags</tt><a class="headerlink" href="#re.regex.flags" title="Permalink to this definition">¶</a></dt>
<dd><p>The flags argument used when the RE object was compiled, or <tt class="docutils literal"><span class="pre">0</span></tt> if no flags
were provided.</p>
</dd></dl>

<dl class="attribute">
<dt id="re.regex.groups">
<tt class="descclassname">regex.</tt><tt class="descname">groups</tt><a class="headerlink" href="#re.regex.groups" title="Permalink to this definition">¶</a></dt>
<dd><p>The number of capturing groups in the pattern.</p>
</dd></dl>

<dl class="attribute">
<dt id="re.regex.groupindex">
<tt class="descclassname">regex.</tt><tt class="descname">groupindex</tt><a class="headerlink" href="#re.regex.groupindex" title="Permalink to this definition">¶</a></dt>
<dd><p>A dictionary mapping any symbolic group names defined by <tt class="docutils literal"><span class="pre">(?P&lt;id&gt;)</span></tt> to group
numbers.  The dictionary is empty if no symbolic groups were used in the
pattern.</p>
</dd></dl>

<dl class="attribute">
<dt id="re.regex.pattern">
<tt class="descclassname">regex.</tt><tt class="descname">pattern</tt><a class="headerlink" href="#re.regex.pattern" title="Permalink to this definition">¶</a></dt>
<dd><p>The pattern string from which the RE object was compiled.</p>
</dd></dl>

</div>
<div class="section" id="match-objects">
<span id="id1"></span><h2>6.2.5. Match Objects<a class="headerlink" href="#match-objects" title="Permalink to this headline">¶</a></h2>
<p>Match objects always have a boolean value of <a class="reference internal" href="constants.html#True" title="True"><tt class="xref py py-const xref docutils literal"><span class="pre">True</span></tt></a>, so that you can test
whether e.g. <a class="reference internal" href="#re.match" title="re.match"><tt class="xref py py-func docutils literal"><span class="pre">match()</span></tt></a> resulted in a match with a simple if statement.  They
support the following methods and attributes:</p>
<dl class="method">
<dt id="re.match.expand">
<tt class="descclassname">match.</tt><tt class="descname">expand</tt><big>(</big><em>template</em><big>)</big><a class="headerlink" href="#re.match.expand" title="Permalink to this definition">¶</a></dt>
<dd><p>Return the string obtained by doing backslash substitution on the template
string <em>template</em>, as done by the <a class="reference internal" href="#re.regex.sub" title="re.regex.sub"><tt class="xref py py-meth docutils literal"><span class="pre">sub()</span></tt></a> method.
Escapes such as <tt class="docutils literal"><span class="pre">\n</span></tt> are converted to the appropriate characters,
and numeric backreferences (<tt class="docutils literal"><span class="pre">\1</span></tt>, <tt class="docutils literal"><span class="pre">\2</span></tt>) and named backreferences
(<tt class="docutils literal"><span class="pre">\g&lt;1&gt;</span></tt>, <tt class="docutils literal"><span class="pre">\g&lt;name&gt;</span></tt>) are replaced by the contents of the
corresponding group.</p>
</dd></dl>

<dl class="method">
<dt id="re.match.group">
<tt class="descclassname">match.</tt><tt class="descname">group</tt><big>(</big><span class="optional">[</span><em>group1</em>, <em>...</em><span class="optional">]</span><big>)</big><a class="headerlink" href="#re.match.group" title="Permalink to this definition">¶</a></dt>
<dd><p>Returns one or more subgroups of the match.  If there is a single argument, the
result is a single string; if there are multiple arguments, the result is a
tuple with one item per argument. Without arguments, <em>group1</em> defaults to zero
(the whole match is returned). If a <em>groupN</em> argument is zero, the corresponding
return value is the entire matching string; if it is in the inclusive range
[1..99], it is the string matching the corresponding parenthesized group.  If a
group number is negative or larger than the number of groups defined in the
pattern, an <a class="reference internal" href="exceptions.html#IndexError" title="IndexError"><tt class="xref py py-exc docutils literal"><span class="pre">IndexError</span></tt></a> exception is raised. If a group is contained in a
part of the pattern that did not match, the corresponding result is <tt class="xref docutils literal"><span class="pre">None</span></tt>.
If a group is contained in a part of the pattern that matched multiple times,
the last match is returned.</p>
<div class="highlight-python3"><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="s">r&quot;(\w+) (\w+)&quot;</span><span class="p">,</span> <span class="s">&quot;Isaac Newton, physicist&quot;</span><span class="p">)</span>
<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="c"># The entire match</span>
<span class="go">&#39;Isaac Newton&#39;</span>
<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="c"># The first parenthesized subgroup.</span>
<span class="go">&#39;Isaac&#39;</span>
<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="c"># The second parenthesized subgroup.</span>
<span class="go">&#39;Newton&#39;</span>
<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="c"># Multiple arguments give us a tuple.</span>
<span class="go">(&#39;Isaac&#39;, &#39;Newton&#39;)</span>
</pre></div>
</div>
<p>If the regular expression uses the <tt class="docutils literal"><span class="pre">(?P&lt;name&gt;...)</span></tt> syntax, the <em>groupN</em>
arguments may also be strings identifying groups by their group name.  If a
string argument is not used as a group name in the pattern, an <a class="reference internal" href="exceptions.html#IndexError" title="IndexError"><tt class="xref py py-exc docutils literal"><span class="pre">IndexError</span></tt></a>
exception is raised.</p>
<p>A moderately complicated example:</p>
<div class="highlight-python3"><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="s">r&quot;(?P&lt;first_name&gt;\w+) (?P&lt;last_name&gt;\w+)&quot;</span><span class="p">,</span> <span class="s">&quot;Malcolm Reynolds&quot;</span><span class="p">)</span>
<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="s">&#39;first_name&#39;</span><span class="p">)</span>
<span class="go">&#39;Malcolm&#39;</span>
<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="s">&#39;last_name&#39;</span><span class="p">)</span>
<span class="go">&#39;Reynolds&#39;</span>
</pre></div>
</div>
<p>Named groups can also be referred to by their index:</p>
<div class="highlight-python3"><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>
<span class="go">&#39;Malcolm&#39;</span>
<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="go">&#39;Reynolds&#39;</span>
</pre></div>
</div>
<p>If a group matches multiple times, only the last match is accessible:</p>
<div class="highlight-python3"><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="s">r&quot;(..)+&quot;</span><span class="p">,</span> <span class="s">&quot;a1b2c3&quot;</span><span class="p">)</span>  <span class="c"># Matches 3 times.</span>
<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="c"># Returns only the last match.</span>
<span class="go">&#39;c3&#39;</span>
</pre></div>
</div>
</dd></dl>

<dl class="method">
<dt id="re.match.groups">
<tt class="descclassname">match.</tt><tt class="descname">groups</tt><big>(</big><em>default=None</em><big>)</big><a class="headerlink" href="#re.match.groups" title="Permalink to this definition">¶</a></dt>
<dd><p>Return a tuple containing all the subgroups of the match, from 1 up to however
many groups are in the pattern.  The <em>default</em> argument is used for groups that
did not participate in the match; it defaults to <tt class="xref docutils literal"><span class="pre">None</span></tt>.</p>
<p>For example:</p>
<div class="highlight-python3"><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="s">r&quot;(\d+)\.(\d+)&quot;</span><span class="p">,</span> <span class="s">&quot;24.1632&quot;</span><span class="p">)</span>
<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="go">(&#39;24&#39;, &#39;1632&#39;)</span>
</pre></div>
</div>
<p>If we make the decimal place and everything after it optional, not all groups
might participate in the match.  These groups will default to <tt class="xref docutils literal"><span class="pre">None</span></tt> unless
the <em>default</em> argument is given:</p>
<div class="highlight-python3"><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="s">r&quot;(\d+)\.?(\d+)?&quot;</span><span class="p">,</span> <span class="s">&quot;24&quot;</span><span class="p">)</span>
<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="c"># Second group defaults to None.</span>
<span class="go">(&#39;24&#39;, None)</span>
<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="s">&#39;0&#39;</span><span class="p">)</span>   <span class="c"># Now, the second group defaults to &#39;0&#39;.</span>
<span class="go">(&#39;24&#39;, &#39;0&#39;)</span>
</pre></div>
</div>
</dd></dl>

<dl class="method">
<dt id="re.match.groupdict">
<tt class="descclassname">match.</tt><tt class="descname">groupdict</tt><big>(</big><em>default=None</em><big>)</big><a class="headerlink" href="#re.match.groupdict" title="Permalink to this definition">¶</a></dt>
<dd><p>Return a dictionary containing all the <em>named</em> subgroups of the match, keyed by
the subgroup name.  The <em>default</em> argument is used for groups that did not
participate in the match; it defaults to <tt class="xref docutils literal"><span class="pre">None</span></tt>.  For example:</p>
<div class="highlight-python3"><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="s">r&quot;(?P&lt;first_name&gt;\w+) (?P&lt;last_name&gt;\w+)&quot;</span><span class="p">,</span> <span class="s">&quot;Malcolm Reynolds&quot;</span><span class="p">)</span>
<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>
<span class="go">{&#39;first_name&#39;: &#39;Malcolm&#39;, &#39;last_name&#39;: &#39;Reynolds&#39;}</span>
</pre></div>
</div>
</dd></dl>

<dl class="method">
<dt id="re.match.start">
<tt class="descclassname">match.</tt><tt class="descname">start</tt><big>(</big><span class="optional">[</span><em>group</em><span class="optional">]</span><big>)</big><a class="headerlink" href="#re.match.start" title="Permalink to this definition">¶</a></dt>
<dt id="re.match.end">
<tt class="descclassname">match.</tt><tt class="descname">end</tt><big>(</big><span class="optional">[</span><em>group</em><span class="optional">]</span><big>)</big><a class="headerlink" href="#re.match.end" title="Permalink to this definition">¶</a></dt>
<dd><p>Return the indices of the start and end of the substring matched by <em>group</em>;
<em>group</em> defaults to zero (meaning the whole matched substring). Return <tt class="docutils literal"><span class="pre">-1</span></tt> if
<em>group</em> exists but did not contribute to the match.  For a match object <em>m</em>, and
a group <em>g</em> that did contribute to the match, the substring matched by group <em>g</em>
(equivalent to <tt class="docutils literal"><span class="pre">m.group(g)</span></tt>) is</p>
<div class="highlight-python3"><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>
</pre></div>
</div>
<p>Note that <tt class="docutils literal"><span class="pre">m.start(group)</span></tt> will equal <tt class="docutils literal"><span class="pre">m.end(group)</span></tt> if <em>group</em> matched a
null string.  For example, after <tt 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></tt>,
<tt class="docutils literal"><span class="pre">m.start(0)</span></tt> is 1, <tt class="docutils literal"><span class="pre">m.end(0)</span></tt> is 2, <tt class="docutils literal"><span class="pre">m.start(1)</span></tt> and <tt class="docutils literal"><span class="pre">m.end(1)</span></tt> are both
2, and <tt class="docutils literal"><span class="pre">m.start(2)</span></tt> raises an <a class="reference internal" href="exceptions.html#IndexError" title="IndexError"><tt class="xref py py-exc docutils literal"><span class="pre">IndexError</span></tt></a> exception.</p>
<p>An example that will remove <em>remove_this</em> from email addresses:</p>
<div class="highlight-python3"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">email</span> <span class="o">=</span> <span class="s">&quot;tony@tiremove_thisger.net&quot;</span>
<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="s">&quot;remove_this&quot;</span><span class="p">,</span> <span class="n">email</span><span class="p">)</span>
<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>
<span class="go">&#39;tony@tiger.net&#39;</span>
</pre></div>
</div>
</dd></dl>

<dl class="method">
<dt id="re.match.span">
<tt class="descclassname">match.</tt><tt class="descname">span</tt><big>(</big><span class="optional">[</span><em>group</em><span class="optional">]</span><big>)</big><a class="headerlink" href="#re.match.span" title="Permalink to this definition">¶</a></dt>
<dd><p>For a match <em>m</em>, return the 2-tuple <tt class="docutils literal"><span class="pre">(m.start(group),</span> <span class="pre">m.end(group))</span></tt>. Note
that if <em>group</em> did not contribute to the match, this is <tt class="docutils literal"><span class="pre">(-1,</span> <span class="pre">-1)</span></tt>.
<em>group</em> defaults to zero, the entire match.</p>
</dd></dl>

<dl class="attribute">
<dt id="re.match.pos">
<tt class="descclassname">match.</tt><tt class="descname">pos</tt><a class="headerlink" href="#re.match.pos" title="Permalink to this definition">¶</a></dt>
<dd><p>The value of <em>pos</em> which was passed to the <a class="reference internal" href="#re.regex.search" title="re.regex.search"><tt class="xref py py-meth docutils literal"><span class="pre">search()</span></tt></a> or
<a class="reference internal" href="#re.regex.match" title="re.regex.match"><tt class="xref py py-meth docutils literal"><span class="pre">match()</span></tt></a> method of a <a class="reference internal" href="#match-objects"><em>match object</em></a>.  This
is the index into the string at which the RE engine started looking for a
match.</p>
</dd></dl>

<dl class="attribute">
<dt id="re.match.endpos">
<tt class="descclassname">match.</tt><tt class="descname">endpos</tt><a class="headerlink" href="#re.match.endpos" title="Permalink to this definition">¶</a></dt>
<dd><p>The value of <em>endpos</em> which was passed to the <a class="reference internal" href="#re.regex.search" title="re.regex.search"><tt class="xref py py-meth docutils literal"><span class="pre">search()</span></tt></a> or
<a class="reference internal" href="#re.regex.match" title="re.regex.match"><tt class="xref py py-meth docutils literal"><span class="pre">match()</span></tt></a> method of a <a class="reference internal" href="#match-objects"><em>match object</em></a>.  This
is the index into the string beyond which the RE engine will not go.</p>
</dd></dl>

<dl class="attribute">
<dt id="re.match.lastindex">
<tt class="descclassname">match.</tt><tt class="descname">lastindex</tt><a class="headerlink" href="#re.match.lastindex" title="Permalink to this definition">¶</a></dt>
<dd><p>The integer index of the last matched capturing group, or <tt class="xref docutils literal"><span class="pre">None</span></tt> if no group
was matched at all. For example, the expressions <tt class="docutils literal"><span class="pre">(a)b</span></tt>, <tt class="docutils literal"><span class="pre">((a)(b))</span></tt>, and
<tt class="docutils literal"><span class="pre">((ab))</span></tt> will have <tt class="docutils literal"><span class="pre">lastindex</span> <span class="pre">==</span> <span class="pre">1</span></tt> if applied to the string <tt class="docutils literal"><span class="pre">'ab'</span></tt>, while
the expression <tt class="docutils literal"><span class="pre">(a)(b)</span></tt> will have <tt class="docutils literal"><span class="pre">lastindex</span> <span class="pre">==</span> <span class="pre">2</span></tt>, if applied to the same
string.</p>
</dd></dl>

<dl class="attribute">
<dt id="re.match.lastgroup">
<tt class="descclassname">match.</tt><tt class="descname">lastgroup</tt><a class="headerlink" href="#re.match.lastgroup" title="Permalink to this definition">¶</a></dt>
<dd><p>The name of the last matched capturing group, or <tt class="xref docutils literal"><span class="pre">None</span></tt> if the group didn&#8217;t
have a name, or if no group was matched at all.</p>
</dd></dl>

<dl class="attribute">
<dt id="re.match.re">
<tt class="descclassname">match.</tt><tt class="descname">re</tt><a class="headerlink" href="#re.match.re" title="Permalink to this definition">¶</a></dt>
<dd><p>The regular expression object whose <a class="reference internal" href="#re.regex.match" title="re.regex.match"><tt class="xref py py-meth docutils literal"><span class="pre">match()</span></tt></a> or
<a class="reference internal" href="#re.regex.search" title="re.regex.search"><tt class="xref py py-meth docutils literal"><span class="pre">search()</span></tt></a> method produced this match instance.</p>
</dd></dl>

<dl class="attribute">
<dt id="re.match.string">
<tt class="descclassname">match.</tt><tt class="descname">string</tt><a class="headerlink" href="#re.match.string" title="Permalink to this definition">¶</a></dt>
<dd><p>The string passed to <a class="reference internal" href="#re.regex.match" title="re.regex.match"><tt class="xref py py-meth docutils literal"><span class="pre">match()</span></tt></a> or <a class="reference internal" href="#re.regex.search" title="re.regex.search"><tt class="xref py py-meth docutils literal"><span class="pre">search()</span></tt></a>.</p>
</dd></dl>

</div>
<div class="section" id="regular-expression-examples">
<span id="re-examples"></span><h2>6.2.6. Regular Expression Examples<a class="headerlink" href="#regular-expression-examples" title="Permalink to this headline">¶</a></h2>
<div class="section" id="checking-for-a-pair">
<h3>6.2.6.1. Checking For a Pair<a class="headerlink" href="#checking-for-a-pair" title="Permalink to this headline">¶</a></h3>
<p>In this example, we&#8217;ll use the following helper function to display match
objects a little more gracefully:</p>
<div class="highlight-python3"><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>
    <span class="k">if</span> <span class="n">match</span> <span class="ow">is</span> <span class="k">None</span><span class="p">:</span>
        <span class="k">return</span> <span class="k">None</span>
    <span class="k">return</span> <span class="s">&#39;&lt;Match: %r, groups=%r&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>
</pre></div>
</div>
<p>Suppose you are writing a poker program where a player&#8217;s hand is represented as
a 5-character string with each character representing a card, &#8220;a&#8221; for ace, &#8220;k&#8221;
for king, &#8220;q&#8221; for queen, j for jack, &#8220;0&#8221; for 10, and &#8220;1&#8221; through &#8220;9&#8221;
representing the card with that value.</p>
<p>To see if a given string is a valid hand, one could do the following:</p>
<div class="highlight-python3"><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="s">r&quot;[0-9akqj]{5}$&quot;</span><span class="p">)</span>
<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="s">&quot;ak05q&quot;</span><span class="p">))</span>  <span class="c"># Valid.</span>
<span class="go">&quot;&lt;Match: &#39;ak05q&#39;, groups=()&gt;&quot;</span>
<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="s">&quot;ak05e&quot;</span><span class="p">))</span>  <span class="c"># Invalid.</span>
<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="s">&quot;ak0&quot;</span><span class="p">))</span>    <span class="c"># Invalid.</span>
<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="s">&quot;727ak&quot;</span><span class="p">))</span>  <span class="c"># Valid.</span>
<span class="go">&quot;&lt;Match: &#39;727ak&#39;, groups=()&gt;&quot;</span>
</pre></div>
</div>
<p>That last hand, <tt class="docutils literal"><span class="pre">&quot;727ak&quot;</span></tt>, contained a pair, or two of the same valued cards.
To match this with a regular expression, one could use backreferences as such:</p>
<div class="highlight-python3"><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="s">r&quot;.*(.).*\1&quot;</span><span class="p">)</span>
<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="s">&quot;717ak&quot;</span><span class="p">))</span>     <span class="c"># Pair of 7s.</span>
<span class="go">&quot;&lt;Match: &#39;717&#39;, groups=(&#39;7&#39;,)&gt;&quot;</span>
<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="s">&quot;718ak&quot;</span><span class="p">))</span>     <span class="c"># No pairs.</span>
<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="s">&quot;354aa&quot;</span><span class="p">))</span>     <span class="c"># Pair of aces.</span>
<span class="go">&quot;&lt;Match: &#39;354aa&#39;, groups=(&#39;a&#39;,)&gt;&quot;</span>
</pre></div>
</div>
<p>To find out what card the pair consists of, one could use the
<a class="reference internal" href="#re.match.group" title="re.match.group"><tt class="xref py py-meth docutils literal"><span class="pre">group()</span></tt></a> method of the match object in the following manner:</p>
<div class="highlight-python3"><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="s">&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>
<span class="go">&#39;7&#39;</span>

<span class="go"># Error because re.match() returns None, which doesn&#39;t have a group() method:</span>
<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="s">&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>
<span class="gt">Traceback (most recent call last):</span>
  File <span class="nb">&quot;&lt;pyshell#23&gt;&quot;</span>, line <span class="m">1</span>, in <span class="n-Identifier">&lt;module&gt;</span>
    <span class="n">re</span><span class="o">.</span><span class="n">match</span><span class="p">(</span><span class="s">r&quot;.*(.).*\1&quot;</span><span class="p">,</span> <span class="s">&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>
<span class="nc">AttributeError</span>: <span class="n-Identifier">&#39;NoneType&#39; object has no attribute &#39;group&#39;</span>

<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="s">&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>
<span class="go">&#39;a&#39;</span>
</pre></div>
</div>
</div>
<div class="section" id="simulating-scanf">
<h3>6.2.6.2. Simulating scanf()<a class="headerlink" href="#simulating-scanf" title="Permalink to this headline">¶</a></h3>
<p id="index-0">Python does not currently have an equivalent to <tt class="xref c c-func docutils literal"><span class="pre">scanf()</span></tt>.  Regular
expressions are generally more powerful, though also more verbose, than
<tt class="xref c c-func docutils literal"><span class="pre">scanf()</span></tt> format strings.  The table below offers some more-or-less
equivalent mappings between <tt class="xref c c-func docutils literal"><span class="pre">scanf()</span></tt> format tokens and regular
expressions.</p>
<table border="1" class="docutils">
<colgroup>
<col width="42%" />
<col width="58%" />
</colgroup>
<thead valign="bottom">
<tr><th class="head"><tt class="xref c c-func docutils literal"><span class="pre">scanf()</span></tt> Token</th>
<th class="head">Regular Expression</th>
</tr>
</thead>
<tbody valign="top">
<tr><td><tt class="docutils literal"><span class="pre">%c</span></tt></td>
<td><tt class="docutils literal"><span class="pre">.</span></tt></td>
</tr>
<tr><td><tt class="docutils literal"><span class="pre">%5c</span></tt></td>
<td><tt class="docutils literal"><span class="pre">.{5}</span></tt></td>
</tr>
<tr><td><tt class="docutils literal"><span class="pre">%d</span></tt></td>
<td><tt class="docutils literal"><span class="pre">[-+]?\d+</span></tt></td>
</tr>
<tr><td><tt class="docutils literal"><span class="pre">%e</span></tt>, <tt class="docutils literal"><span class="pre">%E</span></tt>, <tt class="docutils literal"><span class="pre">%f</span></tt>, <tt class="docutils literal"><span class="pre">%g</span></tt></td>
<td><tt class="docutils literal"><span class="pre">[-+]?(\d+(\.\d*)?|\.\d+)([eE][-+]?\d+)?</span></tt></td>
</tr>
<tr><td><tt class="docutils literal"><span class="pre">%i</span></tt></td>
<td><tt class="docutils literal"><span class="pre">[-+]?(0[xX][\dA-Fa-f]+|0[0-7]*|\d+)</span></tt></td>
</tr>
<tr><td><tt class="docutils literal"><span class="pre">%o</span></tt></td>
<td><tt class="docutils literal"><span class="pre">0[0-7]*</span></tt></td>
</tr>
<tr><td><tt class="docutils literal"><span class="pre">%s</span></tt></td>
<td><tt class="docutils literal"><span class="pre">\S+</span></tt></td>
</tr>
<tr><td><tt class="docutils literal"><span class="pre">%u</span></tt></td>
<td><tt class="docutils literal"><span class="pre">\d+</span></tt></td>
</tr>
<tr><td><tt class="docutils literal"><span class="pre">%x</span></tt>, <tt class="docutils literal"><span class="pre">%X</span></tt></td>
<td><tt class="docutils literal"><span class="pre">0[xX][\dA-Fa-f]+</span></tt></td>
</tr>
</tbody>
</table>
<p>To extract the filename and numbers from a string like</p>
<div class="highlight-python3"><div class="highlight"><pre><span class="o">/</span><span class="n">usr</span><span class="o">/</span><span class="n">sbin</span><span class="o">/</span><span class="n">sendmail</span> <span class="o">-</span> <span class="mi">0</span> <span class="n">errors</span><span class="p">,</span> <span class="mi">4</span> <span class="n">warnings</span>
</pre></div>
</div>
<p>you would use a <tt class="xref c c-func docutils literal"><span class="pre">scanf()</span></tt> format like</p>
<div class="highlight-python3"><div class="highlight"><pre><span class="o">%</span><span class="n">s</span> <span class="o">-</span> <span class="o">%</span><span class="n">d</span> <span class="n">errors</span><span class="p">,</span> <span class="o">%</span><span class="n">d</span> <span class="n">warnings</span>
</pre></div>
</div>
<p>The equivalent regular expression would be</p>
<div class="highlight-python3"><div class="highlight"><pre><span class="p">(</span>\<span class="n">S</span><span class="o">+</span><span class="p">)</span> <span class="o">-</span> <span class="p">(</span>\<span class="n">d</span><span class="o">+</span><span class="p">)</span> <span class="n">errors</span><span class="p">,</span> <span class="p">(</span>\<span class="n">d</span><span class="o">+</span><span class="p">)</span> <span class="n">warnings</span>
</pre></div>
</div>
</div>
<div class="section" id="avoiding-recursion">
<h3>6.2.6.3. Avoiding recursion<a class="headerlink" href="#avoiding-recursion" title="Permalink to this headline">¶</a></h3>
<p>If you create regular expressions that require the engine to perform a lot of
recursion, you may encounter a <a class="reference internal" href="exceptions.html#RuntimeError" title="RuntimeError"><tt class="xref py py-exc docutils literal"><span class="pre">RuntimeError</span></tt></a> exception with the message
<tt class="docutils literal"><span class="pre">maximum</span> <span class="pre">recursion</span> <span class="pre">limit</span></tt> exceeded. For example,</p>
<div class="highlight-python3"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">s</span> <span class="o">=</span> <span class="s">&#39;Begin &#39;</span> <span class="o">+</span> <span class="mi">1000</span><span class="o">*</span><span class="s">&#39;a very long string &#39;</span> <span class="o">+</span> <span class="s">&#39;end&#39;</span>
<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="s">&#39;Begin (\w| )*? end&#39;</span><span class="p">,</span> <span class="n">s</span><span class="p">)</span><span class="o">.</span><span class="n">end</span><span class="p">()</span>
<span class="gt">Traceback (most recent call last):</span>
  File <span class="nb">&quot;&lt;stdin&gt;&quot;</span>, line <span class="m">1</span>, in <span class="n-Identifier">?</span>
  File <span class="nb">&quot;/usr/local/lib/python3.2/re.py&quot;</span>, line <span class="m">132</span>, in <span class="n-Identifier">match</span>
    <span class="k">return</span> <span class="n">_compile</span><span class="p">(</span><span class="n">pattern</span><span class="p">,</span> <span class="n">flags</span><span class="p">)</span><span class="o">.</span><span class="n">match</span><span class="p">(</span><span class="n">string</span><span class="p">)</span>
<span class="nc">RuntimeError</span>: <span class="n-Identifier">maximum recursion limit exceeded</span>
</pre></div>
</div>
<p>You can often restructure your regular expression to avoid recursion.</p>
<p>Simple uses of the <tt class="docutils literal"><span class="pre">*?</span></tt> pattern are special-cased to avoid recursion.  Thus,
the above regular expression can avoid recursion by being recast as <tt class="docutils literal"><span class="pre">Begin</span>
<span class="pre">[a-zA-Z0-9_</span> <span class="pre">]*?end</span></tt>.  As a further benefit, such regular expressions will run
faster than their recursive equivalents.</p>
</div>
<div class="section" id="search-vs-match">
<h3>6.2.6.4. search() vs. match()<a class="headerlink" href="#search-vs-match" title="Permalink to this headline">¶</a></h3>
<p>In a nutshell, <a class="reference internal" href="#re.match" title="re.match"><tt class="xref py py-func docutils literal"><span class="pre">match()</span></tt></a> only attempts to match a pattern at the beginning
of a string where <a class="reference internal" href="#re.search" title="re.search"><tt class="xref py py-func docutils literal"><span class="pre">search()</span></tt></a> will match a pattern anywhere in a string.
For example:</p>
<div class="highlight-python3"><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="s">&quot;o&quot;</span><span class="p">,</span> <span class="s">&quot;dog&quot;</span><span class="p">)</span>  <span class="c"># No match as &quot;o&quot; is not the first letter of &quot;dog&quot;.</span>
<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="s">&quot;o&quot;</span><span class="p">,</span> <span class="s">&quot;dog&quot;</span><span class="p">)</span> <span class="c"># Match as search() looks everywhere in the string.</span>
<span class="go">&lt;_sre.SRE_Match object at ...&gt;</span>
</pre></div>
</div>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last">The following applies only to regular expression objects like those created
with <tt class="docutils literal"><span class="pre">re.compile(&quot;pattern&quot;)</span></tt>, not the primitives <tt class="docutils literal"><span class="pre">re.match(pattern,</span>
<span class="pre">string)</span></tt> or <tt class="docutils literal"><span class="pre">re.search(pattern,</span> <span class="pre">string)</span></tt>.</p>
</div>
<p><a class="reference internal" href="#re.match" title="re.match"><tt class="xref py py-func docutils literal"><span class="pre">match()</span></tt></a> has an optional second parameter that gives an index in the string
where the search is to start:</p>
<div class="highlight-python3"><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="s">&quot;o&quot;</span><span class="p">)</span>
<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="s">&quot;dog&quot;</span><span class="p">)</span>      <span class="c"># No match as &quot;o&quot; is not at the start of &quot;dog.&quot;</span>

<span class="go"># Equivalent to the above expression as 0 is the default starting index:</span>
<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="s">&quot;dog&quot;</span><span class="p">,</span> <span class="mi">0</span><span class="p">)</span>

<span class="go"># Match as &quot;o&quot; is the 2nd character of &quot;dog&quot; (index 0 is the first):</span>
<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="s">&quot;dog&quot;</span><span class="p">,</span> <span class="mi">1</span><span class="p">)</span>
<span class="go">&lt;_sre.SRE_Match object at ...&gt;</span>
<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="s">&quot;dog&quot;</span><span class="p">,</span> <span class="mi">2</span><span class="p">)</span>   <span class="c"># No match as &quot;o&quot; is not the 3rd character of &quot;dog.&quot;</span>
</pre></div>
</div>
</div>
<div class="section" id="making-a-phonebook">
<h3>6.2.6.5. Making a Phonebook<a class="headerlink" href="#making-a-phonebook" title="Permalink to this headline">¶</a></h3>
<p><a class="reference internal" href="#re.split" title="re.split"><tt class="xref py py-func docutils literal"><span class="pre">split()</span></tt></a> splits a string into a list delimited by the passed pattern.  The
method is invaluable for converting textual data into data structures that can be
easily read and modified by Python as demonstrated in the following example that
creates a phonebook.</p>
<p>First, here is the input.  Normally it may come from a file, here we are using
triple-quoted string syntax:</p>
<div class="highlight-python3"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="nb">input</span> <span class="o">=</span> <span class="s">&quot;&quot;&quot;Ross McFluff: 834.345.1254 155 Elm Street</span>
<span class="gp">...</span><span class="s"></span>
<span class="gp">... </span><span class="s">Ronald Heathmore: 892.345.3428 436 Finley Avenue</span>
<span class="gp">... </span><span class="s">Frank Burger: 925.541.7625 662 South Dogwood Way</span>
<span class="gp">...</span><span class="s"></span>
<span class="gp">...</span><span class="s"></span>
<span class="gp">... </span><span class="s">Heather Albrecht: 548.326.4584 919 Park Place&quot;&quot;&quot;</span>
</pre></div>
</div>
<p>The entries are separated by one or more newlines. Now we convert the string
into a list with each nonempty line having its own entry:</p>
<div class="highlight-python3"><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="s">&quot;</span><span class="se">\n</span><span class="s">+&quot;</span><span class="p">,</span> <span class="nb">input</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">entries</span>
<span class="go">[&#39;Ross McFluff: 834.345.1254 155 Elm Street&#39;,</span>
<span class="go">&#39;Ronald Heathmore: 892.345.3428 436 Finley Avenue&#39;,</span>
<span class="go">&#39;Frank Burger: 925.541.7625 662 South Dogwood Way&#39;,</span>
<span class="go">&#39;Heather Albrecht: 548.326.4584 919 Park Place&#39;]</span>
</pre></div>
</div>
<p>Finally, split each entry into a list with first name, last name, telephone
number, and address.  We use the <tt class="docutils literal"><span class="pre">maxsplit</span></tt> parameter of <a class="reference internal" href="#re.split" title="re.split"><tt class="xref py py-func docutils literal"><span class="pre">split()</span></tt></a>
because the address has spaces, our splitting pattern, in it:</p>
<div class="highlight-python3"><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="s">&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>
<span class="go">[[&#39;Ross&#39;, &#39;McFluff&#39;, &#39;834.345.1254&#39;, &#39;155 Elm Street&#39;],</span>
<span class="go">[&#39;Ronald&#39;, &#39;Heathmore&#39;, &#39;892.345.3428&#39;, &#39;436 Finley Avenue&#39;],</span>
<span class="go">[&#39;Frank&#39;, &#39;Burger&#39;, &#39;925.541.7625&#39;, &#39;662 South Dogwood Way&#39;],</span>
<span class="go">[&#39;Heather&#39;, &#39;Albrecht&#39;, &#39;548.326.4584&#39;, &#39;919 Park Place&#39;]]</span>
</pre></div>
</div>
<p>The <tt class="docutils literal"><span class="pre">:?</span></tt> pattern matches the colon after the last name, so that it does not
occur in the result list.  With a <tt class="docutils literal"><span class="pre">maxsplit</span></tt> of <tt class="docutils literal"><span class="pre">4</span></tt>, we could separate the
house number from the street name:</p>
<div class="highlight-python3"><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="s">&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>
<span class="go">[[&#39;Ross&#39;, &#39;McFluff&#39;, &#39;834.345.1254&#39;, &#39;155&#39;, &#39;Elm Street&#39;],</span>
<span class="go">[&#39;Ronald&#39;, &#39;Heathmore&#39;, &#39;892.345.3428&#39;, &#39;436&#39;, &#39;Finley Avenue&#39;],</span>
<span class="go">[&#39;Frank&#39;, &#39;Burger&#39;, &#39;925.541.7625&#39;, &#39;662&#39;, &#39;South Dogwood Way&#39;],</span>
<span class="go">[&#39;Heather&#39;, &#39;Albrecht&#39;, &#39;548.326.4584&#39;, &#39;919&#39;, &#39;Park Place&#39;]]</span>
</pre></div>
</div>
</div>
<div class="section" id="text-munging">
<h3>6.2.6.6. Text Munging<a class="headerlink" href="#text-munging" title="Permalink to this headline">¶</a></h3>
<p><a class="reference internal" href="#re.sub" title="re.sub"><tt class="xref py py-func docutils literal"><span class="pre">sub()</span></tt></a> replaces every occurrence of a pattern with a string or the
result of a function.  This example demonstrates using <a class="reference internal" href="#re.sub" title="re.sub"><tt class="xref py py-func docutils literal"><span class="pre">sub()</span></tt></a> with
a function to &#8220;munge&#8221; text, or randomize the order of all the characters
in each word of a sentence except for the first and last characters:</p>
<div class="highlight-python3"><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>
<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>
<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>
<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="s">&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>
<span class="gp">&gt;&gt;&gt; </span><span class="n">text</span> <span class="o">=</span> <span class="s">&quot;Professor Abdolmalek, please report your absences promptly.&quot;</span>
<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="s">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>
<span class="go">&#39;Poefsrosr Aealmlobdk, pslaee reorpt your abnseces plmrptoy.&#39;</span>
<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="s">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>
<span class="go">&#39;Pofsroser Aodlambelk, plasee reoprt yuor asnebces potlmrpy.&#39;</span>
</pre></div>
</div>
</div>
<div class="section" id="finding-all-adverbs">
<h3>6.2.6.7. Finding all Adverbs<a class="headerlink" href="#finding-all-adverbs" title="Permalink to this headline">¶</a></h3>
<p><a class="reference internal" href="#re.findall" title="re.findall"><tt class="xref py py-func docutils literal"><span class="pre">findall()</span></tt></a> matches <em>all</em> occurrences of a pattern, not just the first
one as <a class="reference internal" href="#re.search" title="re.search"><tt class="xref py py-func docutils literal"><span class="pre">search()</span></tt></a> does.  For example, if one was a writer and wanted to
find all of the adverbs in some text, he or she might use <a class="reference internal" href="#re.findall" title="re.findall"><tt class="xref py py-func docutils literal"><span class="pre">findall()</span></tt></a> in
the following manner:</p>
<div class="highlight-python3"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">text</span> <span class="o">=</span> <span class="s">&quot;He was carefully disguised but captured quickly by police.&quot;</span>
<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="s">r&quot;\w+ly&quot;</span><span class="p">,</span> <span class="n">text</span><span class="p">)</span>
<span class="go">[&#39;carefully&#39;, &#39;quickly&#39;]</span>
</pre></div>
</div>
</div>
<div class="section" id="finding-all-adverbs-and-their-positions">
<h3>6.2.6.8. Finding all Adverbs and their Positions<a class="headerlink" href="#finding-all-adverbs-and-their-positions" title="Permalink to this headline">¶</a></h3>
<p>If one wants more information about all matches of a pattern than the matched
text, <a class="reference internal" href="#re.finditer" title="re.finditer"><tt class="xref py py-func docutils literal"><span class="pre">finditer()</span></tt></a> is useful as it provides <a class="reference internal" href="#match-objects"><em>match objects</em></a> instead of strings.  Continuing with the previous example, if
one was a writer who wanted to find all of the adverbs <em>and their positions</em> in
some text, he or she would use <a class="reference internal" href="#re.finditer" title="re.finditer"><tt class="xref py py-func docutils literal"><span class="pre">finditer()</span></tt></a> in the following manner:</p>
<div class="highlight-python3"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">text</span> <span class="o">=</span> <span class="s">&quot;He was carefully disguised but captured quickly by police.&quot;</span>
<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="s">r&quot;\w+ly&quot;</span><span class="p">,</span> <span class="n">text</span><span class="p">):</span>
<span class="gp">... </span>    <span class="nb">print</span><span class="p">(</span><span class="s">&#39;%02d-%02d: %s&#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>
<span class="go">07-16: carefully</span>
<span class="go">40-47: quickly</span>
</pre></div>
</div>
</div>
<div class="section" id="raw-string-notation">
<h3>6.2.6.9. Raw String Notation<a class="headerlink" href="#raw-string-notation" title="Permalink to this headline">¶</a></h3>
<p>Raw string notation (<tt class="docutils literal"><span class="pre">r&quot;text&quot;</span></tt>) keeps regular expressions sane.  Without it,
every backslash (<tt class="docutils literal"><span class="pre">'\'</span></tt>) in a regular expression would have to be prefixed with
another one to escape it.  For example, the two following lines of code are
functionally identical:</p>
<div class="highlight-python3"><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="s">r&quot;\W(.)\1\W&quot;</span><span class="p">,</span> <span class="s">&quot; ff &quot;</span><span class="p">)</span>
<span class="go">&lt;_sre.SRE_Match object at ...&gt;</span>
<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="s">&quot;</span><span class="se">\\</span><span class="s">W(.)</span><span class="se">\\</span><span class="s">1</span><span class="se">\\</span><span class="s">W&quot;</span><span class="p">,</span> <span class="s">&quot; ff &quot;</span><span class="p">)</span>
<span class="go">&lt;_sre.SRE_Match object at ...&gt;</span>
</pre></div>
</div>
<p>When one wants to match a literal backslash, it must be escaped in the regular
expression.  With raw string notation, this means <tt class="docutils literal"><span class="pre">r&quot;\\&quot;</span></tt>.  Without raw string
notation, one must use <tt class="docutils literal"><span class="pre">&quot;\\\\&quot;</span></tt>, making the following lines of code
functionally identical:</p>
<div class="highlight-python3"><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="s">r&quot;</span><span class="se">\\</span><span class="s">&quot;</span><span class="p">,</span> <span class="s">r&quot;</span><span class="se">\\</span><span class="s">&quot;</span><span class="p">)</span>
<span class="go">&lt;_sre.SRE_Match object at ...&gt;</span>
<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="s">&quot;</span><span class="se">\\\\</span><span class="s">&quot;</span><span class="p">,</span> <span class="s">r&quot;</span><span class="se">\\</span><span class="s">&quot;</span><span class="p">)</span>
<span class="go">&lt;_sre.SRE_Match object at ...&gt;</span>
</pre></div>
</div>
</div>
<div class="section" id="writing-a-tokenizer">
<h3>6.2.6.10. Writing a Tokenizer<a class="headerlink" href="#writing-a-tokenizer" title="Permalink to this headline">¶</a></h3>
<p>A <a class="reference external" href="http://en.wikipedia.org/wiki/Lexical_analysis">tokenizer or scanner</a>
analyzes a string to categorize groups of characters.  This is a useful first
step in writing a compiler or interpreter.</p>
<p>The text categories are specified with regular expressions.  The technique is
to combine those into a single master regular expression and to loop over
successive matches:</p>
<div class="highlight-python3"><div class="highlight"><pre><span class="kn">import</span> <span class="nn">collections</span>
<span class="kn">import</span> <span class="nn">re</span>

<span class="n">Token</span> <span class="o">=</span> <span class="n">collections</span><span class="o">.</span><span class="n">namedtuple</span><span class="p">(</span><span class="s">&#39;Token&#39;</span><span class="p">,</span> <span class="p">[</span><span class="s">&#39;typ&#39;</span><span class="p">,</span> <span class="s">&#39;value&#39;</span><span class="p">,</span> <span class="s">&#39;line&#39;</span><span class="p">,</span> <span class="s">&#39;column&#39;</span><span class="p">])</span>

<span class="k">def</span> <span class="nf">tokenize</span><span class="p">(</span><span class="n">s</span><span class="p">):</span>
    <span class="n">keywords</span> <span class="o">=</span> <span class="p">{</span><span class="s">&#39;IF&#39;</span><span class="p">,</span> <span class="s">&#39;THEN&#39;</span><span class="p">,</span> <span class="s">&#39;ENDIF&#39;</span><span class="p">,</span> <span class="s">&#39;FOR&#39;</span><span class="p">,</span> <span class="s">&#39;NEXT&#39;</span><span class="p">,</span> <span class="s">&#39;GOSUB&#39;</span><span class="p">,</span> <span class="s">&#39;RETURN&#39;</span><span class="p">}</span>
    <span class="n">token_specification</span> <span class="o">=</span> <span class="p">[</span>
        <span class="p">(</span><span class="s">&#39;NUMBER&#39;</span><span class="p">,</span>  <span class="s">r&#39;\d+(\.\d*)?&#39;</span><span class="p">),</span> <span class="c"># Integer or decimal number</span>
        <span class="p">(</span><span class="s">&#39;ASSIGN&#39;</span><span class="p">,</span>  <span class="s">r&#39;:=&#39;</span><span class="p">),</span>          <span class="c"># Assignment operator</span>
        <span class="p">(</span><span class="s">&#39;END&#39;</span><span class="p">,</span>     <span class="s">r&#39;;&#39;</span><span class="p">),</span>           <span class="c"># Statement terminator</span>
        <span class="p">(</span><span class="s">&#39;ID&#39;</span><span class="p">,</span>      <span class="s">r&#39;[A-Za-z]+&#39;</span><span class="p">),</span>   <span class="c"># Identifiers</span>
        <span class="p">(</span><span class="s">&#39;OP&#39;</span><span class="p">,</span>      <span class="s">r&#39;[+*\/\-]&#39;</span><span class="p">),</span>    <span class="c"># Arithmetic operators</span>
        <span class="p">(</span><span class="s">&#39;NEWLINE&#39;</span><span class="p">,</span> <span class="s">r&#39;\n&#39;</span><span class="p">),</span>          <span class="c"># Line endings</span>
        <span class="p">(</span><span class="s">&#39;SKIP&#39;</span><span class="p">,</span>    <span class="s">r&#39;[ \t]&#39;</span><span class="p">),</span>       <span class="c"># Skip over spaces and tabs</span>
    <span class="p">]</span>
    <span class="n">tok_regex</span> <span class="o">=</span> <span class="s">&#39;|&#39;</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="s">&#39;(?P&lt;%s&gt;%s)&#39;</span> <span class="o">%</span> <span class="n">pair</span> <span class="k">for</span> <span class="n">pair</span> <span class="ow">in</span> <span class="n">token_specification</span><span class="p">)</span>
    <span class="n">get_token</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">tok_regex</span><span class="p">)</span><span class="o">.</span><span class="n">match</span>
    <span class="n">line</span> <span class="o">=</span> <span class="mi">1</span>
    <span class="n">pos</span> <span class="o">=</span> <span class="n">line_start</span> <span class="o">=</span> <span class="mi">0</span>
    <span class="n">mo</span> <span class="o">=</span> <span class="n">get_token</span><span class="p">(</span><span class="n">s</span><span class="p">)</span>
    <span class="k">while</span> <span class="n">mo</span> <span class="ow">is</span> <span class="ow">not</span> <span class="k">None</span><span class="p">:</span>
        <span class="n">typ</span> <span class="o">=</span> <span class="n">mo</span><span class="o">.</span><span class="n">lastgroup</span>
        <span class="k">if</span> <span class="n">typ</span> <span class="o">==</span> <span class="s">&#39;NEWLINE&#39;</span><span class="p">:</span>
            <span class="n">line_start</span> <span class="o">=</span> <span class="n">pos</span>
            <span class="n">line</span> <span class="o">+=</span> <span class="mi">1</span>
        <span class="k">elif</span> <span class="n">typ</span> <span class="o">!=</span> <span class="s">&#39;SKIP&#39;</span><span class="p">:</span>
            <span class="n">val</span> <span class="o">=</span> <span class="n">mo</span><span class="o">.</span><span class="n">group</span><span class="p">(</span><span class="n">typ</span><span class="p">)</span>
            <span class="k">if</span> <span class="n">typ</span> <span class="o">==</span> <span class="s">&#39;ID&#39;</span> <span class="ow">and</span> <span class="n">val</span> <span class="ow">in</span> <span class="n">keywords</span><span class="p">:</span>
                <span class="n">typ</span> <span class="o">=</span> <span class="n">val</span>
            <span class="k">yield</span> <span class="n">Token</span><span class="p">(</span><span class="n">typ</span><span class="p">,</span> <span class="n">val</span><span class="p">,</span> <span class="n">line</span><span class="p">,</span> <span class="n">mo</span><span class="o">.</span><span class="n">start</span><span class="p">()</span><span class="o">-</span><span class="n">line_start</span><span class="p">)</span>
        <span class="n">pos</span> <span class="o">=</span> <span class="n">mo</span><span class="o">.</span><span class="n">end</span><span class="p">()</span>
        <span class="n">mo</span> <span class="o">=</span> <span class="n">get_token</span><span class="p">(</span><span class="n">s</span><span class="p">,</span> <span class="n">pos</span><span class="p">)</span>
    <span class="k">if</span> <span class="n">pos</span> <span class="o">!=</span> <span class="nb">len</span><span class="p">(</span><span class="n">s</span><span class="p">):</span>
        <span class="k">raise</span> <span class="ne">RuntimeError</span><span class="p">(</span><span class="s">&#39;Unexpected character %r on line %d&#39;</span> <span class="o">%</span><span class="p">(</span><span class="n">s</span><span class="p">[</span><span class="n">pos</span><span class="p">],</span> <span class="n">line</span><span class="p">))</span>

<span class="n">statements</span> <span class="o">=</span> <span class="s">&#39;&#39;&#39;</span>
<span class="s">    IF quantity THEN</span>
<span class="s">        total := total + price * quantity;</span>
<span class="s">        tax := price * 0.05;</span>
<span class="s">    ENDIF;</span>
<span class="s">&#39;&#39;&#39;</span>

<span class="k">for</span> <span class="n">token</span> <span class="ow">in</span> <span class="n">tokenize</span><span class="p">(</span><span class="n">statements</span><span class="p">):</span>
    <span class="nb">print</span><span class="p">(</span><span class="n">token</span><span class="p">)</span>
</pre></div>
</div>
<p>The tokenizer produces the following output:</p>
<div class="highlight-python3"><div class="highlight"><pre><span class="n">Token</span><span class="p">(</span><span class="n">typ</span><span class="o">=</span><span class="s">&#39;IF&#39;</span><span class="p">,</span> <span class="n">value</span><span class="o">=</span><span class="s">&#39;IF&#39;</span><span class="p">,</span> <span class="n">line</span><span class="o">=</span><span class="mi">2</span><span class="p">,</span> <span class="n">column</span><span class="o">=</span><span class="mi">5</span><span class="p">)</span>
<span class="n">Token</span><span class="p">(</span><span class="n">typ</span><span class="o">=</span><span class="s">&#39;ID&#39;</span><span class="p">,</span> <span class="n">value</span><span class="o">=</span><span class="s">&#39;quantity&#39;</span><span class="p">,</span> <span class="n">line</span><span class="o">=</span><span class="mi">2</span><span class="p">,</span> <span class="n">column</span><span class="o">=</span><span class="mi">8</span><span class="p">)</span>
<span class="n">Token</span><span class="p">(</span><span class="n">typ</span><span class="o">=</span><span class="s">&#39;THEN&#39;</span><span class="p">,</span> <span class="n">value</span><span class="o">=</span><span class="s">&#39;THEN&#39;</span><span class="p">,</span> <span class="n">line</span><span class="o">=</span><span class="mi">2</span><span class="p">,</span> <span class="n">column</span><span class="o">=</span><span class="mi">17</span><span class="p">)</span>
<span class="n">Token</span><span class="p">(</span><span class="n">typ</span><span class="o">=</span><span class="s">&#39;ID&#39;</span><span class="p">,</span> <span class="n">value</span><span class="o">=</span><span class="s">&#39;total&#39;</span><span class="p">,</span> <span class="n">line</span><span class="o">=</span><span class="mi">3</span><span class="p">,</span> <span class="n">column</span><span class="o">=</span><span class="mi">9</span><span class="p">)</span>
<span class="n">Token</span><span class="p">(</span><span class="n">typ</span><span class="o">=</span><span class="s">&#39;ASSIGN&#39;</span><span class="p">,</span> <span class="n">value</span><span class="o">=</span><span class="s">&#39;:=&#39;</span><span class="p">,</span> <span class="n">line</span><span class="o">=</span><span class="mi">3</span><span class="p">,</span> <span class="n">column</span><span class="o">=</span><span class="mi">15</span><span class="p">)</span>
<span class="n">Token</span><span class="p">(</span><span class="n">typ</span><span class="o">=</span><span class="s">&#39;ID&#39;</span><span class="p">,</span> <span class="n">value</span><span class="o">=</span><span class="s">&#39;total&#39;</span><span class="p">,</span> <span class="n">line</span><span class="o">=</span><span class="mi">3</span><span class="p">,</span> <span class="n">column</span><span class="o">=</span><span class="mi">18</span><span class="p">)</span>
<span class="n">Token</span><span class="p">(</span><span class="n">typ</span><span class="o">=</span><span class="s">&#39;OP&#39;</span><span class="p">,</span> <span class="n">value</span><span class="o">=</span><span class="s">&#39;+&#39;</span><span class="p">,</span> <span class="n">line</span><span class="o">=</span><span class="mi">3</span><span class="p">,</span> <span class="n">column</span><span class="o">=</span><span class="mi">24</span><span class="p">)</span>
<span class="n">Token</span><span class="p">(</span><span class="n">typ</span><span class="o">=</span><span class="s">&#39;ID&#39;</span><span class="p">,</span> <span class="n">value</span><span class="o">=</span><span class="s">&#39;price&#39;</span><span class="p">,</span> <span class="n">line</span><span class="o">=</span><span class="mi">3</span><span class="p">,</span> <span class="n">column</span><span class="o">=</span><span class="mi">26</span><span class="p">)</span>
<span class="n">Token</span><span class="p">(</span><span class="n">typ</span><span class="o">=</span><span class="s">&#39;OP&#39;</span><span class="p">,</span> <span class="n">value</span><span class="o">=</span><span class="s">&#39;*&#39;</span><span class="p">,</span> <span class="n">line</span><span class="o">=</span><span class="mi">3</span><span class="p">,</span> <span class="n">column</span><span class="o">=</span><span class="mi">32</span><span class="p">)</span>
<span class="n">Token</span><span class="p">(</span><span class="n">typ</span><span class="o">=</span><span class="s">&#39;ID&#39;</span><span class="p">,</span> <span class="n">value</span><span class="o">=</span><span class="s">&#39;quantity&#39;</span><span class="p">,</span> <span class="n">line</span><span class="o">=</span><span class="mi">3</span><span class="p">,</span> <span class="n">column</span><span class="o">=</span><span class="mi">34</span><span class="p">)</span>
<span class="n">Token</span><span class="p">(</span><span class="n">typ</span><span class="o">=</span><span class="s">&#39;END&#39;</span><span class="p">,</span> <span class="n">value</span><span class="o">=</span><span class="s">&#39;;&#39;</span><span class="p">,</span> <span class="n">line</span><span class="o">=</span><span class="mi">3</span><span class="p">,</span> <span class="n">column</span><span class="o">=</span><span class="mi">42</span><span class="p">)</span>
<span class="n">Token</span><span class="p">(</span><span class="n">typ</span><span class="o">=</span><span class="s">&#39;ID&#39;</span><span class="p">,</span> <span class="n">value</span><span class="o">=</span><span class="s">&#39;tax&#39;</span><span class="p">,</span> <span class="n">line</span><span class="o">=</span><span class="mi">4</span><span class="p">,</span> <span class="n">column</span><span class="o">=</span><span class="mi">9</span><span class="p">)</span>
<span class="n">Token</span><span class="p">(</span><span class="n">typ</span><span class="o">=</span><span class="s">&#39;ASSIGN&#39;</span><span class="p">,</span> <span class="n">value</span><span class="o">=</span><span class="s">&#39;:=&#39;</span><span class="p">,</span> <span class="n">line</span><span class="o">=</span><span class="mi">4</span><span class="p">,</span> <span class="n">column</span><span class="o">=</span><span class="mi">13</span><span class="p">)</span>
<span class="n">Token</span><span class="p">(</span><span class="n">typ</span><span class="o">=</span><span class="s">&#39;ID&#39;</span><span class="p">,</span> <span class="n">value</span><span class="o">=</span><span class="s">&#39;price&#39;</span><span class="p">,</span> <span class="n">line</span><span class="o">=</span><span class="mi">4</span><span class="p">,</span> <span class="n">column</span><span class="o">=</span><span class="mi">16</span><span class="p">)</span>
<span class="n">Token</span><span class="p">(</span><span class="n">typ</span><span class="o">=</span><span class="s">&#39;OP&#39;</span><span class="p">,</span> <span class="n">value</span><span class="o">=</span><span class="s">&#39;*&#39;</span><span class="p">,</span> <span class="n">line</span><span class="o">=</span><span class="mi">4</span><span class="p">,</span> <span class="n">column</span><span class="o">=</span><span class="mi">22</span><span class="p">)</span>
<span class="n">Token</span><span class="p">(</span><span class="n">typ</span><span class="o">=</span><span class="s">&#39;NUMBER&#39;</span><span class="p">,</span> <span class="n">value</span><span class="o">=</span><span class="s">&#39;0.05&#39;</span><span class="p">,</span> <span class="n">line</span><span class="o">=</span><span class="mi">4</span><span class="p">,</span> <span class="n">column</span><span class="o">=</span><span class="mi">24</span><span class="p">)</span>
<span class="n">Token</span><span class="p">(</span><span class="n">typ</span><span class="o">=</span><span class="s">&#39;END&#39;</span><span class="p">,</span> <span class="n">value</span><span class="o">=</span><span class="s">&#39;;&#39;</span><span class="p">,</span> <span class="n">line</span><span class="o">=</span><span class="mi">4</span><span class="p">,</span> <span class="n">column</span><span class="o">=</span><span class="mi">28</span><span class="p">)</span>
<span class="n">Token</span><span class="p">(</span><span class="n">typ</span><span class="o">=</span><span class="s">&#39;ENDIF&#39;</span><span class="p">,</span> <span class="n">value</span><span class="o">=</span><span class="s">&#39;ENDIF&#39;</span><span class="p">,</span> <span class="n">line</span><span class="o">=</span><span class="mi">5</span><span class="p">,</span> <span class="n">column</span><span class="o">=</span><span class="mi">5</span><span class="p">)</span>
<span class="n">Token</span><span class="p">(</span><span class="n">typ</span><span class="o">=</span><span class="s">&#39;END&#39;</span><span class="p">,</span> <span class="n">value</span><span class="o">=</span><span class="s">&#39;;&#39;</span><span class="p">,</span> <span class="n">line</span><span class="o">=</span><span class="mi">5</span><span class="p">,</span> <span class="n">column</span><span class="o">=</span><span class="mi">10</span><span class="p">)</span>
</pre></div>
</div>
</div>
</div>
</div>


          </div>
        </div>
      </div>
      <div class="sphinxsidebar">
        <div class="sphinxsidebarwrapper">
  <h3><a href="../contents.html">Table Of Contents</a></h3>
  <ul>
<li><a class="reference internal" href="#">6.2. <tt class="docutils literal"><span class="pre">re</span></tt> &#8212; Regular expression operations</a><ul>
<li><a class="reference internal" href="#regular-expression-syntax">6.2.1. Regular Expression Syntax</a></li>
<li><a class="reference internal" href="#matching-vs-searching">6.2.2. Matching vs Searching</a></li>
<li><a class="reference internal" href="#module-contents">6.2.3. Module Contents</a></li>
<li><a class="reference internal" href="#regular-expression-objects">6.2.4. Regular Expression Objects</a></li>
<li><a class="reference internal" href="#match-objects">6.2.5. Match Objects</a></li>
<li><a class="reference internal" href="#regular-expression-examples">6.2.6. Regular Expression Examples</a><ul>
<li><a class="reference internal" href="#checking-for-a-pair">6.2.6.1. Checking For a Pair</a></li>
<li><a class="reference internal" href="#simulating-scanf">6.2.6.2. Simulating scanf()</a></li>
<li><a class="reference internal" href="#avoiding-recursion">6.2.6.3. Avoiding recursion</a></li>
<li><a class="reference internal" href="#search-vs-match">6.2.6.4. search() vs. match()</a></li>
<li><a class="reference internal" href="#making-a-phonebook">6.2.6.5. Making a Phonebook</a></li>
<li><a class="reference internal" href="#text-munging">6.2.6.6. Text Munging</a></li>
<li><a class="reference internal" href="#finding-all-adverbs">6.2.6.7. Finding all Adverbs</a></li>
<li><a class="reference internal" href="#finding-all-adverbs-and-their-positions">6.2.6.8. Finding all Adverbs and their Positions</a></li>
<li><a class="reference internal" href="#raw-string-notation">6.2.6.9. Raw String Notation</a></li>
<li><a class="reference internal" href="#writing-a-tokenizer">6.2.6.10. Writing a Tokenizer</a></li>
</ul>
</li>
</ul>
</li>
</ul>

  <h4>Previous topic</h4>
  <p class="topless"><a href="string.html"
                        title="previous chapter">6.1. <tt class="docutils literal"><span class="pre">string</span></tt> &#8212; Common string operations</a></p>
  <h4>Next topic</h4>
  <p class="topless"><a href="struct.html"
                        title="next chapter">6.3. <tt class="docutils literal docutils literal"><span class="pre">struct</span></tt> &#8212; Interpret bytes as packed binary data</a></p>
<h3>This Page</h3>
<ul class="this-page-menu">
  <li><a href="../bugs.html">Report a Bug</a></li>
  <li><a href="../_sources/library/re.txt"
         rel="nofollow">Show Source</a></li>
</ul>

<div id="searchbox" style="display: none">
  <h3>Quick search</h3>
    <form class="search" action="../search.html" method="get">
      <input type="text" name="q" size="18" />
      <input type="submit" value="Go" />
      <input type="hidden" name="check_keywords" value="yes" />
      <input type="hidden" name="area" value="default" />
    </form>
    <p class="searchtip" style="font-size: 90%">
    Enter search terms or a module, class or function name.
    </p>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>
      <div class="clearer"></div>
    </div>
    <div class="related">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../genindex.html" title="General Index"
             >index</a></li>
        <li class="right" >
          <a href="../py-modindex.html" title="Python Module Index"
             >modules</a> |</li>
        <li class="right" >
          <a href="struct.html" title="6.3. struct — Interpret bytes as packed binary data"
             >next</a> |</li>
        <li class="right" >
          <a href="string.html" title="6.1. string — Common string operations"
             >previous</a> |</li>
        <li><img src="../_static/py.png" alt=""
                 style="vertical-align: middle; margin-top: -1px"/></li>
        <li><a href="../index.html">Python v3.2.2 documentation</a> &raquo;</li>

          <li><a href="index.html" >The Python Standard Library</a> &raquo;</li>
          <li><a href="strings.html" >6. String Services</a> &raquo;</li> 
      </ul>
    </div>
    <div class="footer">
    &copy; <a href="../copyright.html">Copyright</a> 1990-2011, Python Software Foundation.
    <br />
    The Python Software Foundation is a non-profit corporation.  
    <a href="http://www.python.org/psf/donations/">Please donate.</a>
    <br />
    Last updated on Sep 04, 2011.
    <a href="../bugs.html">Found a bug</a>?
    <br />
    Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 1.0.7.
    </div>

  </body>
</html>