Changeset 250

Show
Ignore:
Timestamp:
Mon Dec 12 09:42:09 2005
Author:
Zoomq
Message:

re done!

Files:

Legend:

Unmodified
Added
Removed
Modified
  • zh-translations/branches/diveintopython-zh-5.4/zh-cn/xml/re.xml

    r201 r250  
    1   <?xml version="1.0" encoding="utf-8"?>  
    2   <chapter id="re">  
    3   <?dbhtml filename="regular_expressions/index.html"?>  
    4   <title>Regular Expressions</title>  
    5   <titleabbrev id="re.numberonly">Chapter 7</titleabbrev>  
    6   <abstract>  
    7   <title/>  
    8   <para>Regular expressions are a powerful and standardized way of searching, replacing, and parsing text with complex patterns of characters.  If you've used regular expressions in other languages (like &perl;), the syntax will be very familiar, and you get by just reading the summary of the &remodule_link; to get an overview of the available functions and their arguments.</para>  
    9   </abstract>  
    10   <section id="re.intro">  
    11   <title>Diving In</title>  
    12   <para>Strings have methods for searching (<function>index</function>, <function>find</function>, and <function>count</function>), replacing (<function>replace</function>), and parsing (<function>split</function>), but they are limited to the simplest of cases.  The search methods look for a single, hard-coded substring, and they are always case-sensitive.  To do case-insensitive searches of a string <varname>s</varname>, you must call <function>s.lower()</function> or <function>s.upper()</function> and make sure your search strings are the appropriate case to match.  The <function>replace</function> and <function>split</function> methods have the same limitations.</para>  
    13   <abstract>  
    14   <title/>  
    15   <para>If what you're trying to do can be accomplished with string functions, you should use them.  They're fast and simple and easy to read, and there's a lot to be said for fast, simple, readable code.  But if you find yourself using a lot of different string functions with &if; statements to handle special cases, or if you're combining them with &split; and &join; and list comprehensions in weird unreadable ways, you may need to move up to regular expressions.</para>  
    16   </abstract>  
    17   <para>Although the regular expression syntax is tight and unlike normal code, the result can end up being <emphasis>more</emphasis> readable than a hand-rolled solution that uses a long chain of string functions.  There are even ways of embedding comments within regular expressions to make them practically self-documenting.</para>  
    18   </section>  
    19   <section id="re.matching">  
    20   <?dbhtml filename="regular_expressions/street_addresses.html"?>  
    21   <title>Case Study: Street Addresses</title>  
    22   <abstract>  
    23   <title/>  
    24   <para>This series of examples was inspired by a real-life problem I had in my day job several years ago, when I needed to scrub and standardize street addresses exported from a legacy system before importing them into a newer system.  (See, I don't just make this stuff up; it's actually useful.)  This example shows how I approached the problem.</para>  
    25   </abstract>  
    26   <example>  
    27   <title>Matching at the End of a String</title>  
    28   <screen>  
    29   &prompt;<userinput>s = '100 NORTH MAIN ROAD'</userinput>  
    30   &prompt;<userinput>s.replace('ROAD', 'RD.')</userinput>               <co id="re.matching.1.1"/>  
    31   <computeroutput>'100 NORTH MAIN RD.'</computeroutput>  
    32   &prompt;<userinput>s = '100 NORTH BROAD ROAD'</userinput>  
    33   &prompt;<userinput>s.replace('ROAD', 'RD.')</userinput>               <co id="re.matching.1.2"/>  
    34   <computeroutput>'100 NORTH BRD. RD.'</computeroutput>  
    35   &prompt;<userinput>s[:-4] + s[-4:].replace('ROAD', 'RD.')</userinput> <co id="re.matching.1.3"/>  
    36   <computeroutput>'100 NORTH BROAD RD.'</computeroutput>  
    37   &prompt;<userinput>import re</userinput>                              <co id="re.matching.1.4"/>  
    38   &prompt;<userinput>re.sub('ROAD$', 'RD.', s)</userinput>              <co id="re.matching.1.5"/> <co id="re.matching.1.6"/>  
    39   <computeroutput>'100 NORTH BROAD RD.'</computeroutput></screen>  
    40   <calloutlist>  
    41   <callout arearefs="re.matching.1.1">  
    42   <para>My goal is to standardize a street address so that <literal>'ROAD'</literal> is always abbreviated as <literal>'RD.'</literal>.  At first glance, I thought this was simple enough that I could just use the string method <function>replace</function>.  After all, all the data was already uppercase, so case mismatches would not be a problem.  And the search string, <literal>'ROAD'</literal>, was a constant.  And in this deceptively simple example, <function>s.replace</function> does indeed work.</para>  
    43   </callout>  
    44   <callout arearefs="re.matching.1.2">  
    45   <para>Life, unfortunately, is full of counterexamples, and I quickly discovered this one.  The problem here is that <literal>'ROAD'</literal> appears twice in the address, once as part of the street name <literal>'BROAD'</literal> and once as its own word.  The <function>replace</function> method sees these two occurrences and blindly replaces both of them; meanwhile, I see my addresses getting destroyed.</para>  
    46   </callout>  
    47   <callout arearefs="re.matching.1.3">  
    48   <para>To solve the problem of addresses with more than one <literal>'ROAD'</literal> substring, you could resort to something like this: only search and replace <literal>'ROAD'</literal> in the last four characters of the address (<literal>s[-4:]</literal>), and leave the string alone (<literal>s[:-4]</literal>).  But you can see that this is already getting unwieldy.  For example, the pattern is dependent on the length of the string you're replacing (if you were replacing <literal>'STREET'</literal> with <literal>'ST.'</literal>, you would need to use <literal>s[:-6]</literal> and <literal>s[-6:].replace(...)</literal>).  Would you like to come back in six months and debug this?  I know I wouldn't.</para>  
    49   </callout>  
    50   <callout arearefs="re.matching.1.4">  
    51   <para>It's time to move up to regular expressions.  In &python;, all functionality related to regular expressions is contained in the &re; module.</para>  
    52   </callout>  
    53   <callout arearefs="re.matching.1.5">  
    54   <para>Take a look at the first parameter: <literal>'ROAD$'</literal>.  This is a simple regular expression that matches <literal>'ROAD'</literal> only when it occurs at the end of a string.  The <literal>$</literal> means <quote>end of the string</quote>.  (There is a corresponding character, the caret <literal>^</literal>, which means <quote>beginning of the string</quote>.)</para>  
    55   </callout>  
    56   <callout arearefs="re.matching.1.6">  
    57   <para>Using the <function>re.sub</function> function, you search the string <varname>s</varname> for the regular expression <literal>'ROAD$'</literal> and replace it with <literal>'RD.'</literal>.  This matches the <literal>ROAD</literal> at the end of the string <varname>s</varname>, but does <emphasis>not</emphasis> match the <literal>ROAD</literal> that's part of the word <literal>BROAD</literal>, because that's in the middle of <varname>s</varname>.</para>  
    58   </callout>  
    59   </calloutlist>  
    60   </example>  
    61   <para>Continuing with my story of scrubbing addresses, I soon discovered that the previous example, matching <literal>'ROAD'</literal> at the end of the address, was not good enough, because not all addresses included a street designation at all; some just ended with the street name.  Most of the time, I got away with it, but if the street name was <literal>'BROAD'</literal>, then the regular expression would match <literal>'ROAD'</literal> at the end of the string as part of the word <literal>'BROAD'</literal>, which is not what I wanted.</para>  
    62   <example>  
    63   <title>Matching Whole Words</title>  
    64   <screen>  
    65   &prompt;<userinput>s = '100 BROAD'</userinput>  
    66   &prompt;<userinput>re.sub('ROAD$', 'RD.', s)</userinput>  
    67   <computeroutput>'100 BRD.'</computeroutput>  
    68   &prompt;<userinput>re.sub('\\bROAD$', 'RD.', s)</userinput>  <co id="re.matching.2.2"/>  
    69   <computeroutput>'100 BROAD'</computeroutput>  
    70   &prompt;<userinput>re.sub(r'\bROAD$', 'RD.', s)</userinput>  <co id="re.matching.2.3"/>  
    71   <computeroutput>'100 BROAD'</computeroutput>  
    72   &prompt;<userinput>s = '100 BROAD ROAD APT. 3'</userinput>  
    73   &prompt;<userinput>re.sub(r'\bROAD$', 'RD.', s)</userinput>  <co id="re.matching.2.4"/>  
    74   <computeroutput>'100 BROAD ROAD APT. 3'</computeroutput>  
    75   &prompt;<userinput>re.sub(r'\bROAD\b', 'RD.', s)</userinput> <co id="re.matching.2.5"/>  
    76   <computeroutput>'100 BROAD RD. APT 3'</computeroutput></screen>  
    77   <calloutlist>  
    78   <callout arearefs="re.matching.2.2">  
    79   <para>What I <emphasis>really</emphasis> wanted was to match <literal>'ROAD'</literal> when it was at the end of the string <emphasis>and</emphasis> it was its own whole word, not a part of some larger word.  To express this in a regular expression, you use <literal>\b</literal>, which means <quote>a word boundary must occur right here</quote>.  In &python;, this is complicated by the fact that the <literal>'\'</literal> character in a string must itself be escaped.  This is sometimes referred to as the backslash plague, and it is one reason why regular expressions are easier in &perl; than in &python;.  On the down side, &perl; mixes regular expressions with other syntax, so if you have a bug, it may be hard to tell whether it's a bug in syntax or a bug in your regular expression.</para>  
    80   </callout>  
    81   <callout arearefs="re.matching.2.3">  
    82   <para>To work around the backslash plague, you can use what is called a raw string, by prefixing the string with the letter <literal>r</literal>.  This tells &python; that nothing in this string should be escaped; <literal>'\t'</literal> is a tab character, but <literal>r'\t'</literal> is really the backslash character <literal>\</literal> followed by the letter <literal>t</literal>.  I recommend always using raw strings when dealing with regular expressions; otherwise, things get too confusing too quickly (and regular expressions get confusing quickly enough all by themselves).</para>  
    83   </callout>  
    84   <callout arearefs="re.matching.2.4">  
    85   <para><emphasis>*sigh*</emphasis>  Unfortunately, I soon found more cases that contradicted my logic.  In this case, the street address contained the word <literal>'ROAD'</literal> as a whole word by itself, but it wasn't at the end, because the address had an apartment number after the street designation.  Because <literal>'ROAD'</literal> isn't at the very end of the string, it doesn't match, so the entire call to <function>re.sub</function> ends up replacing nothing at all, and you get the original string back, which is not what you want.</para>  
    86   </callout>  
    87   <callout arearefs="re.matching.2.5">  
    88   <para>To solve this problem, I removed the <literal>$</literal> character and added another <literal>\b</literal>.  Now the regular expression reads <quote>match <literal>'ROAD'</literal> when it's a whole word by itself anywhere in the string,</quote> whether at the end, the beginning, or somewhere in the middle.</para>  
    89   </callout>  
    90   </calloutlist>  
    91   </example>  
    92   </section>  
    93   <section id="re.roman">  
    94   <?dbhtml filename="regular_expressions/roman_numerals.html"?>  
    95   <title>Case Study: Roman Numerals</title>  
    96   <abstract>  
    97   <title/>  
    98   <para>You've most likely seen Roman numerals, even if you didn't recognize them.  You may have seen them in copyrights of old movies and television shows (<quote>Copyright <literal>MCMXLVI</literal></quote> instead of <quote>Copyright <literal>1946</literal></quote>), or on the dedication walls of libraries or universities (<quote>established <literal>MDCCCLXXXVIII</literal></quote> instead of <quote>established <literal>1888</literal></quote>).  You may also have seen them in outlines and bibliographical references.  It's a system of representing numbers that really does date back to the ancient Roman empire (hence the name).</para>  
    99   </abstract>  
    100   <para>In Roman numerals, there are seven characters that are repeated and combined in various ways to represent numbers.</para>  
    101   <itemizedlist>  
    102   <listitem><para><literal>I</literal> = <literal>1</literal></para></listitem>  
    103   <listitem><para><literal>V</literal> = <literal>5</literal></para></listitem>  
    104   <listitem><para><literal>X</literal> = <literal>10</literal></para></listitem>  
    105   <listitem><para><literal>L</literal> = <literal>50</literal></para></listitem>  
    106   <listitem><para><literal>C</literal> = <literal>100</literal></para></listitem>  
    107   <listitem><para><literal>D</literal> = <literal>500</literal></para></listitem>  
    108   <listitem><para><literal>M</literal> = <literal>1000</literal></para></listitem>  
    109   </itemizedlist>  
    110   <para>The following are some general rules for constructing Roman numerals:</para>  
    111   <itemizedlist>  
    112   <listitem><para>Characters are additive.  <literal>I</literal> is &one;, <literal>II</literal> is <literal>2</literal>, and <literal>III</literal> is <literal>3</literal>.  <literal>VI</literal> is <literal>6</literal> (literally, <quote><literal>5</literal> and <literal>1</literal></quote>), <literal>VII</literal> is <literal>7</literal>, and <literal>VIII</literal> is <literal>8</literal>.</para></listitem>  
    113   <listitem><para>The tens characters (<literal>I</literal>, <literal>X</literal>, <literal>C</literal>, and <literal>M</literal>) can be repeated up to three times.  At <literal>4</literal>, you need to subtract from the next highest fives character.  You can't represent <literal>4</literal> as <literal>IIII</literal>; instead, it is represented as <literal>IV</literal> (<quote><literal>1</literal> less than <literal>5</literal></quote>).  The number <literal>40</literal> is written as <literal>XL</literal> (<literal>10</literal> less than <literal>50</literal>), <literal>41</literal> as <literal>XLI</literal>, <literal>42</literal> as <literal>XLII</literal>, <literal>43</literal> as <literal>XLIII</literal>, and then <literal>44</literal> as <literal>XLIV</literal> (<literal>10</literal> less than <literal>50</literal>, then <literal>1</literal> less than <literal>5</literal>).</para></listitem>  
    114   <listitem><para>Similarly, at <literal>9</literal>, you need to subtract from the next highest tens character: <literal>8</literal> is <literal>VIII</literal>, but <literal>9</literal> is <literal>IX</literal> (<literal>1</literal> less than <literal>10</literal>), not <literal>VIIII</literal> (since the <literal>I</literal> character can not be repeated four times).  The number <literal>90</literal> is <literal>XC</literal>, <literal>900</literal> is <literal>CM</literal>.</para></listitem>  
    115   <listitem><para>The fives characters can not be repeated.  The number <literal>10</literal> is always represented as <literal>X</literal>, never as <literal>VV</literal>.  The number <literal>100</literal> is always <literal>C</literal>, never <literal>LL</literal>.</para></listitem>  
    116   <listitem><para>Roman numerals are always written highest to lowest, and read left to right, so the order the of characters matters very much.  <literal>DC</literal> is <literal>600</literal>; <literal>CD</literal> is a completely different number (<literal>400</literal>, <literal>100</literal> less than <literal>500</literal>).  <literal>CI</literal> is <literal>101</literal>; <literal>IC</literal> is not even a valid Roman numeral (because you can't subtract <literal>1</literal> directly from <literal>100</literal>; you would need to write it as <literal>XCIX</literal>, for <literal>10</literal> less than <literal>100</literal>, then <literal>1</literal> less than <literal>10</literal>).</para></listitem>  
    117   </itemizedlist>  
    118   <section>  
    119   <title>Checking for Thousands</title>  
    120   <para>What would it take to validate that an arbitrary string is a valid Roman numeral?  Let's take it one digit at a time.  Since Roman numerals are always written highest to lowest, let's start with the highest: the thousands place.  For numbers 1000 and higher, the thousands are represented by a series of <literal>M</literal> characters.</para>  
    121   <example>  
    122   <title>Checking for Thousands</title>  
    123   <screen>  
    124   &prompt;<userinput>import re</userinput>  
    125   &prompt;<userinput>pattern = '^M?M?M?$'</userinput>       <co id="re.roman.1.1"/>  
    126   &prompt;<userinput>re.search(pattern, 'M')</userinput>    <co id="re.roman.1.2"/>  
    127   <computeroutput>&lt;SRE_Match object at 0106FB58></computeroutput>  
    128   &prompt;<userinput>re.search(pattern, 'MM')</userinput>   <co id="re.roman.1.3"/>  
    129   <computeroutput>&lt;SRE_Match object at 0106C290></computeroutput>  
    130   &prompt;<userinput>re.search(pattern, 'MMM')</userinput>  <co id="re.roman.1.4"/>  
    131   <computeroutput>&lt;SRE_Match object at 0106AA38></computeroutput>  
    132   &prompt;<userinput>re.search(pattern, 'MMMM')</userinput> <co id="re.roman.1.5"/>  
    133   &prompt;<userinput>re.search(pattern, '')</userinput>     <co id="re.roman.1.6"/>  
    134   <computeroutput>&lt;SRE_Match object at 0106F4A8></computeroutput></screen>  
    135   <calloutlist>  
    136   <callout arearefs="re.roman.1.1">  
    137   <para>This pattern has three parts:</para>  
    138   <itemizedlist>  
    139   <listitem><para><literal>^</literal> to match what follows only at the beginning of the string.  If this were not specified, the pattern would match no matter where the <literal>M</literal> characters were, which is not what you want.  You want to make sure that the <literal>M</literal> characters, if they're there, are at the beginning of the string.</para></listitem>  
    140   <listitem><para><literal>M?</literal> to optionally match a single <literal>M</literal> character.  Since this is repeated three times, you're matching anywhere from zero to three <literal>M</literal> characters in a row.</para></listitem>  
    141   <listitem><para><literal>$</literal> to match what precedes only at the end of the string.  When combined with the <literal>^</literal> character at the beginning, this means that the pattern must match the entire string, with no other characters before or after the <literal>M</literal> characters.</para></listitem>  
    142   </itemizedlist>  
    143   </callout>  
    144   <callout arearefs="re.roman.1.2">  
    145   <para>The essence of the &re; module is the &search; function, that takes a regular expression (<varname>pattern</varname>) and a string (<literal>'M'</literal>) to try to match against the regular expression.  If a match is found, &search; returns an object which has various methods to describe the match; if no match is found, &search; returns &none;, the &python; null value.  All you care about at the moment is whether the pattern matches, which you can tell by just looking at the return value of &search;.  <literal>'M'</literal> matches this regular expression, because the first optional <literal>M</literal> matches and the second and third optional <literal>M</literal> characters are ignored.</para>  
    146   </callout>  
    147   <callout arearefs="re.roman.1.3">  
    148   <para><literal>'MM'</literal> matches because the first and second optional <literal>M</literal> characters match and the third <literal>M</literal> is ignored.</para>  
    149   </callout>  
    150   <callout arearefs="re.roman.1.4">  
    151   <para><literal>'MMM'</literal> matches because all three <literal>M</literal> characters match.</para>  
    152   </callout>  
    153   <callout arearefs="re.roman.1.5">  
    154   <para><literal>'MMMM'</literal> does not match.  All three <literal>M</literal> characters match, but then the regular expression insists on the string ending (because of the <literal>$</literal> character), and the string doesn't end yet (because of the fourth <literal>M</literal>).  So &search; returns &none;.</para>  
    155   </callout>  
    156   <callout arearefs="re.roman.1.6">  
    157   <para>Interestingly, an empty string also matches this regular expression, since all the <literal>M</literal> characters are optional.</para>  
    158   </callout>  
    159   </calloutlist>  
    160   </example>  
    161   </section>  
    162   <section>  
    163   <title>Checking for Hundreds</title>  
    164   <para>The hundreds place is more difficult than the thousands, because there are several mutually exclusive ways it could be expressed, depending on its value.</para>  
    165   <itemizedlist>  
    166   <listitem><para><literal>100</literal> = <literal>C</literal></para></listitem>  
    167   <listitem><para><literal>200</literal> = <literal>CC</literal></para></listitem>  
    168   <listitem><para><literal>300</literal> = <literal>CCC</literal></para></listitem>  
    169   <listitem><para><literal>400</literal> = <literal>CD</literal></para></listitem>  
    170   <listitem><para><literal>500</literal> = <literal>D</literal></para></listitem>  
    171   <listitem><para><literal>600</literal> = <literal>DC</literal></para></listitem>  
    172   <listitem><para><literal>700</literal> = <literal>DCC</literal></para></listitem>  
    173   <listitem><para><literal>800</literal> = <literal>DCCC</literal></para></listitem>  
    174   <listitem><para><literal>900</literal> = <literal>CM</literal></para></listitem>  
    175   </itemizedlist>  
    176   <para>So there are four possible patterns:</para>  
    177   <itemizedlist>  
    178   <listitem><para><literal>CM</literal></para></listitem>  
    179   <listitem><para><literal>CD</literal></para></listitem>  
    180   <listitem><para>Zero to three <literal>C</literal> characters (zero if the hundreds place is 0)</para></listitem>  
    181   <listitem><para><literal>D</literal>, followed by zero to three <literal>C</literal> characters</para></listitem>  
    182   </itemizedlist>  
    183   <para>The last two patterns can be combined:</para>  
    184   <itemizedlist>  
    185   <listitem><para>an optional <literal>D</literal>, followed by zero to three <literal>C</literal> characters</para></listitem>  
    186   </itemizedlist>  
    187   <para>This example shows how to validate the hundreds place of a Roman numeral.</para>  
    188   <example id="re.roman.hundreds">  
    189   <title>Checking for Hundreds</title>  
    190   <screen>  
    191   &prompt;<userinput>import re</userinput>  
    192   &prompt;<userinput>pattern = '^M?M?M?(CM|CD|D?C?C?C?)$'</userinput> <co id="re.roman.2.1"/>  
    193   &prompt;<userinput>re.search(pattern, 'MCM')</userinput>            <co id="re.roman.2.2"/>  
    194   <computeroutput>&lt;SRE_Match object at 01070390></computeroutput>  
    195   &prompt;<userinput>re.search(pattern, 'MD')</userinput>             <co id="re.roman.2.3"/>  
    196   <computeroutput>&lt;SRE_Match object at 01073A50></computeroutput>  
    197   &prompt;<userinput>re.search(pattern, 'MMMCCC')</userinput>         <co id="re.roman.2.4"/>  
    198   <computeroutput>&lt;SRE_Match object at 010748A8></computeroutput>  
    199   &prompt;<userinput>re.search(pattern, 'MCMC')</userinput>           <co id="re.roman.2.5"/>  
    200   &prompt;<userinput>re.search(pattern, '')</userinput>               <co id="re.roman.2.6"/>  
    201   <computeroutput>&lt;SRE_Match object at 01071D98></computeroutput></screen>  
    202   <calloutlist>  
    203   <callout arearefs="re.roman.2.1">  
    204   <para>This pattern starts out the same as the previous one, checking for the beginning of the string (<literal>^</literal>), then the thousands place (<literal>M?M?M?</literal>).  Then it has the new part, in parentheses, which defines a set of three mutually exclusive patterns, separated by vertical bars: <literal>CM</literal>, <literal>CD</literal>, and <literal>D?C?C?C?</literal> (which is an optional <literal>D</literal> followed by zero to three optional <literal>C</literal> characters).  The regular expression parser checks for each of these patterns in order (from left to right), takes the first one that matches, and ignores the rest.</para>  
    205   </callout>  
    206   <callout arearefs="re.roman.2.2">  
    207   <para><literal>'MCM'</literal> matches because the first <literal>M</literal> matches, the second and third <literal>M</literal> characters are ignored, and the <literal>CM</literal> matches (so the <literal>CD</literal> and <literal>D?C?C?C?</literal> patterns are never even considered).  <literal>MCM</literal> is the Roman numeral representation of <literal>1900</literal>.</para>  
    208   </callout>  
    209   <callout arearefs="re.roman.2.3">  
    210   <para><literal>'MD'</literal> matches because the first <literal>M</literal> matches, the second and third <literal>M</literal> characters are ignored, and the <literal>D?C?C?C?</literal> pattern matches <literal>D</literal> (each of the three <literal>C</literal> characters are optional and are ignored).  <literal>MD</literal> is the Roman numeral representation of <literal>1500</literal>.</para>  
    211   </callout>  
    212   <callout arearefs="re.roman.2.4">  
    213   <para><literal>'MMMCCC'</literal> matches because all three <literal>M</literal> characters match, and the <literal>D?C?C?C?</literal> pattern matches <literal>CCC</literal> (the <literal>D</literal> is optional and is ignored).  <literal>MMMCCC</literal> is the Roman numeral representation of <literal>3300</literal>.</para>  
    214   </callout>  
    215   <callout arearefs="re.roman.2.5">  
    216   <para><literal>'MCMC'</literal> does not match.  The first <literal>M</literal> matches, the second and third <literal>M</literal> characters are ignored, and the <literal>CM</literal> matches, but then the <literal>$</literal> does not match because you're not at the end of the string yet (you still have an unmatched <literal>C</literal> character).  The <literal>C</literal> does <emphasis>not</emphasis> match as part of the <literal>D?C?C?C?</literal> pattern, because the mutually exclusive <literal>CM</literal> pattern has already matched.</para>  
    217   </callout>  
    218   <callout arearefs="re.roman.2.6">  
    219   <para>Interestingly, an empty string still matches this pattern, because all the <literal>M</literal> characters are optional and ignored, and the empty string matches the <literal>D?C?C?C?</literal> pattern where all the characters are optional and ignored.</para>  
    220   </callout>  
    221   </calloutlist>  
    222   </example>  
    223   <para>Whew!  See how quickly regular expressions can get nasty?  And you've only covered the thousands and hundreds places of Roman numerals.  But if you followed all that, the tens and ones places are easy, because they're exactly the same pattern.  But let's look at another way to express the pattern.</para>  
    224   </section>  
    225   </section>  
    226   <section id="re.nm">  
    227   <?dbhtml filename="regular_expressions/n_m_syntax.html"?>  
    228   <title>Using the <literal>{n,m}</literal> Syntax</title>  
    229   <abstract>  
    230   <title/>  
    231   <para>In <link linkend="re.roman">the previous section</link>, you were dealing with a pattern where the same character could be repeated up to three times.  There is another way to express this in regular expressions, which some people find more readable.  First look at the method we already used in the previous example.</para>  
    232   </abstract>  
    233   <example>  
    234   <title>The Old Way: Every Character Optional</title>  
    235   <screen>  
    236   &prompt;<userinput>import re</userinput>  
    237   &prompt;<userinput>pattern = '^M?M?M?$'</userinput>  
    238   &prompt;<userinput>re.search(pattern, 'M')</userinput>    <co id="re.nm.1.1"/>  
    239   <computeroutput>&lt;_sre.SRE_Match object at 0x008EE090></computeroutput>  
    240   &prompt;<userinput>pattern = '^M?M?M?$'</userinput>  
    241   &prompt;<userinput>re.search(pattern, 'MM')</userinput>   <co id="re.nm.1.2"/>  
    242   <computeroutput>&lt;_sre.SRE_Match object at 0x008EEB48></computeroutput>  
    243   &prompt;<userinput>pattern = '^M?M?M?$'</userinput>  
    244   &prompt;<userinput>re.search(pattern, 'MMM')</userinput>  <co id="re.nm.1.3"/>  
    245   <computeroutput>&lt;_sre.SRE_Match object at 0x008EE090></computeroutput>  
    246   &prompt;<userinput>re.search(pattern, 'MMMM')</userinput> <co id="re.nm.1.4"/>  
    247   &prompt;  
    248   </screen>  
    249   <calloutlist>  
    250   <callout arearefs="re.nm.1.1">  
    251   <para>This matches the start of the string, and then the first optional <literal>M</literal>, but not the second and third <literal>M</literal> (but that's okay because they're optional), and then the end of the string.</para>  
    252   </callout>  
    253   <callout arearefs="re.nm.1.2">  
    254   <para>This matches the start of the string, and then the first and second optional <literal>M</literal>, but not the third <literal>M</literal> (but that's okay because it's optional), and then the end of the string.</para>  
    255   </callout>  
    256   <callout arearefs="re.nm.1.3">  
    257   <para>This matches the start of the string, and then all three optional <literal>M</literal>, and then the end of the string.</para>  
    258   </callout>  
    259   <callout arearefs="re.nm.1.4">  
    260   <para>This matches the start of the string, and then all three optional <literal>M</literal>, but then does not match the the end of the string (because there is still one unmatched <literal>M</literal>), so the pattern does not match and returns &none;.</para>  
    261   </callout>  
    262   </calloutlist>  
    263   </example>  
    264   <example>  
    265   <title>The New Way: From <literal>n</literal> o <literal>m</literal></title>  
    266   <screen>  
    267   &prompt;<userinput>pattern = '^M{0,3}$'</userinput>       <co id="re.nm.2.0"/>  
    268   &prompt;<userinput>re.search(pattern, 'M')</userinput>    <co id="re.nm.2.1"/>  
    269   <computeroutput>&lt;_sre.SRE_Match object at 0x008EEB48></computeroutput>  
    270   &prompt;<userinput>re.search(pattern, 'MM')</userinput>   <co id="re.nm.2.2"/>  
    271   <computeroutput>&lt;_sre.SRE_Match object at 0x008EE090></computeroutput>  
    272   &prompt;<userinput>re.search(pattern, 'MMM')</userinput>  <co id="re.nm.2.3"/>  
    273   <computeroutput>&lt;_sre.SRE_Match object at 0x008EEDA8></computeroutput>  
    274   &prompt;<userinput>re.search(pattern, 'MMMM')</userinput> <co id="re.nm.2.4"/>  
    275   &prompt;  
    276   </screen>  
    277   <calloutlist>  
    278   <callout arearefs="re.nm.2.0">  
    279   <para>This pattern says: <quote>Match the start of the string, then anywhere from zero to three <literal>M</literal> characters, then the end of the string.</quote>  The 0 and 3 can be any numbers; if you want to match at least one but no more than three <literal>M</literal> characters, you could say <literal>M{1,3}</literal>.</para>  
    280   </callout>  
    281   <callout arearefs="re.nm.2.1">  
    282   <para>This matches the start of the string, then one <literal>M</literal> out of a possible three, then the end of the string.</para>  
    283   </callout>  
    284   <callout arearefs="re.nm.2.2">  
    285   <para>This matches the start of the string, then two <literal>M</literal> out of a possible three, then the end of the string.</para>  
    286   </callout>  
    287   <callout arearefs="re.nm.2.3">  
    288   <para>This matches the start of the string, then three <literal>M</literal> out of a possible three, then the end of the string.</para>  
    289   </callout>  
    290   <callout arearefs="re.nm.2.4">  
    291   <para>This matches the start of the string, then three <literal>M</literal> out of a possible three, but then <emphasis>does not match</emphasis> the end of the string.  The regular expression allows for up to only three <literal>M</literal> characters before the end of the string, but you have four, so the pattern does not match and returns &none;.</para>  
    292   </callout>  
    293   </calloutlist>  
    294   </example>  
    295   <note>  
    296   <!--<title>Comparing regular expressions</title>-->  
    297   <title/>  
    298   <para>There is no way to programmatically determine that two regular expressions are equivalent.  The best you can do is write a lot of test cases to make sure they behave the same way on all relevant inputs.  You'll talk more about writing test cases later in this book.</para>  
    299   </note>  
    300   <section>  
    301   <title>Checking for Tens and Ones</title>  
    302   <para>Now let's expand the Roman numeral regular expression to cover the tens and ones place.  This example shows the check for tens.</para>  
    303   <example id="re.tens.example">  
    304   <title>Checking for Tens</title>  
    305   <screen>  
    306   &prompt;<userinput>pattern = '^M?M?M?M?(CM|CD|D?C?C?C?)(XC|XL|L?X?X?X?)$'</userinput>  
    307   &prompt;<userinput>re.search(pattern, 'MCMXL')</userinput>    <co id="re.nm.3.3"/>  
    308   <computeroutput>&lt;_sre.SRE_Match object at 0x008EEB48></computeroutput>  
    309   &prompt;<userinput>re.search(pattern, 'MCML')</userinput>     <co id="re.nm.3.4"/>  
    310   <computeroutput>&lt;_sre.SRE_Match object at 0x008EEB48></computeroutput>  
    311   &prompt;<userinput>re.search(pattern, 'MCMLX')</userinput>    <co id="re.nm.3.5"/>  
    312   <computeroutput>&lt;_sre.SRE_Match object at 0x008EEB48></computeroutput>  
    313   &prompt;<userinput>re.search(pattern, 'MCMLXXX')</userinput>  <co id="re.nm.3.7"/>  
    314   <computeroutput>&lt;_sre.SRE_Match object at 0x008EEB48></computeroutput>  
    315   &prompt;<userinput>re.search(pattern, 'MCMLXXXX')</userinput> <co id="re.nm.3.8"/>  
    316   &prompt;  
    317   </screen>  
    318   <calloutlist>  
    319   <callout arearefs="re.nm.3.3">  
    320   <para>This matches the start of the string, then the first optional <literal>M</literal>, then <literal>CM</literal>, then <literal>XL</literal>, then the end of the string.  Remember, the <literal>(A|B|C)</literal> syntax means <quote>match exactly one of A, B, or C</quote>.  You match <literal>XL</literal>, so you ignore the <literal>XC</literal> and <literal>L?X?X?X?</literal> choices, and then move on to the end of the string.  <literal>MCML</literal> is the Roman numeral representation of <literal>1940</literal>.</para>  
    321   </callout>  
    322   <callout arearefs="re.nm.3.4">  
    323   <para>This matches the start of the string, then the first optional <literal>M</literal>, then <literal>CM</literal>, then <literal>L?X?X?X?</literal>.  Of the <literal>L?X?X?X?</literal>, it matches the <literal>L</literal> and skips all three optional <literal>X</literal> characters.  Then you move to the end of the string.  <literal>MCML</literal> is the Roman numeral representation of <literal>1950</literal>.</para>  
    324   </callout>  
    325   <callout arearefs="re.nm.3.5">  
    326   <para>This matches the start of the string, then the first optional <literal>M</literal>, then <literal>CM</literal>, then the optional <literal>L</literal> and the first optional <literal>X</literal>, skips the second and third optional <literal>X</literal>, then the end of the string.  <literal>MCMLX</literal> is the Roman numeral representation of <literal>1960</literal>.</para>  
    327   </callout>  
    328   <callout arearefs="re.nm.3.7">  
    329   <para>This matches the start of the string, then the first optional <literal>M</literal>, then <literal>CM</literal>, then the optional <literal>L</literal> and all three optional <literal>X</literal> characters, then the end of the string.  <literal>MCMLXXX</literal> is the Roman numeral representation of <literal>1980</literal>.</para>  
    330   </callout>  
    331   <callout arearefs="re.nm.3.8">  
    332   <para>This matches the start of the string, then the first optional <literal>M</literal>, then <literal>CM</literal>, then the optional <literal>L</literal> and all three optional <literal>X</literal> characters, then <emphasis>fails to match</emphasis> the end of the string because there is still one more <literal>X</literal> unaccounted for.  So the entire pattern fails to match, and returns &none;.  <literal>MCMLXXXX</literal> is not a valid Roman numeral.</para>  
    333   </callout>  
    334   </calloutlist>  
    335   </example>  
    336   <para>The expression for the ones place follows the same pattern.  I'll spare you the details and show you the end result.</para>  
    337   <informalexample>  
    338   <!--<title>The Ones Place</title>-->  
    339   <screen>  
    340   &prompt;<userinput>pattern = '^M?M?M?M?(CM|CD|D?C?C?C?)(XC|XL|L?X?X?X?)(IX|IV|V?I?I?I?)$'</userinput>  
    341   </screen>  
    342   </informalexample>  
    343   <para>So what does that look like using this alternate <literal>{n,m}</literal> syntax?  This example shows the new syntax.</para>  
    344   <example id="re.nm.example">  
    345   <title>Validating Roman Numerals with <literal>{n,m}</literal></title>  
    346   <screen>  
    347   &prompt;<userinput>pattern = '^M{0,4}(CM|CD|D?C{0,3})(XC|XL|L?X{0,3})(IX|IV|V?I{0,3})$'</userinput>  
    348   &prompt;<userinput>re.search(pattern, 'MDLV')</userinput>             <co id="re.nm.4.1"/>  
    349   <computeroutput>&lt;_sre.SRE_Match object at 0x008EEB48></computeroutput>  
    350   &prompt;<userinput>re.search(pattern, 'MMDCLXVI')</userinput>         <co id="re.nm.4.2"/>  
    351   <computeroutput>&lt;_sre.SRE_Match object at 0x008EEB48></computeroutput>  
    352   &prompt;<userinput>re.search(pattern, 'MMMMDCCCLXXXVIII')</userinput> <co id="re.nm.4.3"/>  
    353   <computeroutput>&lt;_sre.SRE_Match object at 0x008EEB48></computeroutput>  
    354   &prompt;<userinput>re.search(pattern, 'I')</userinput>                <co id="re.nm.4.4"/>  
    355   <computeroutput>&lt;_sre.SRE_Match object at 0x008EEB48></computeroutput>  
    356   </screen>  
    357   <calloutlist>  
    358   <callout arearefs="re.nm.4.1">  
    359   <para>This matches the start of the string, then one of a possible four <literal>M</literal> characters, then <literal>D?C{0,3}</literal>.  Of that, it matches the optional <literal>D</literal> and zero of three possible <literal>C</literal> characters.  Moving on, it matches <literal>L?X{0,3}</literal> by matching the optional <literal>L</literal> and zero of three possible <literal>X</literal> characters.  Then it matches <literal>V?I{0,3}</literal> by matching the optional V and zero of three possible <literal>I</literal> characters, and finally the end of the string.  <literal>MDLV</literal> is the Roman numeral representation of <literal>1555</literal>.</para>  
    360   </callout>  
    361   <callout arearefs="re.nm.4.2">  
    362   <para>This matches the start of the string, then two of a possible four <literal>M</literal> characters, then the <literal>D?C{0,3}</literal> with a <literal>D</literal> and one of three possible <literal>C</literal> characters; then <literal>L?X{0,3}</literal> with an <literal>L</literal> and one of three possible <literal>X</literal> characters; then <literal>V?I{0,3}</literal> with a <literal>V</literal> and one of three possible <literal>I</literal> characters; then the end of the string.  <literal>MMDCLXVI</literal> is the Roman numeral representation of <literal>2666</literal>.</para>  
    363   </callout>  
    364   <callout arearefs="re.nm.4.3">  
    365   <para>This matches the start of the string, then four out of four <literal>M</literal> characters, then <literal>D?C{0,3}</literal> with a <literal>D</literal> and three out of three <literal>C</literal> characters; then <literal>L?X{0,3}</literal> with an <literal>L</literal> and three out of three <literal>X</literal> characters; then <literal>V?I{0,3}</literal> with a <literal>V</literal> and three out of three <literal>I</literal> characters; then the end of the string.  <literal>MMMMDCCCLXXXVIII</literal> is the Roman numeral representation of <literal>3888</literal>, and it's the longest Roman numeral you can write without extended syntax.</para>  
    366   </callout>  
    367   <callout arearefs="re.nm.4.4">  
    368   <para>Watch closely.  (I feel like a magician.  <quote>Watch closely, kids, I'm going to pull a rabbit out of my hat.</quote>)  This matches the start of the string, then zero out of four <literal>M</literal>, then matches <literal>D?C{0,3}</literal> by skipping the optional <literal>D</literal> and matching zero out of three <literal>C</literal>, then matches <literal>L?X{0,3}</literal> by skipping the optional <literal>L</literal> and matching zero out of three <literal>X</literal>, then matches <literal>V?I{0,3}</literal> by skipping the optional <literal>V</literal> and matching one out of three <literal>I</literal>.  Then the end of the string.  Whoa.</para>  
    369   </callout>  
    370   </calloutlist>  
    371   </example>  
    372   <para>If you followed all that and understood it on the first try, you're doing better than I did.  Now imagine trying to understand someone else's regular expressions, in the middle of a critical function of a large program.  Or even imagine coming back to your own regular expressions a few months later.  I've done it, and it's not a pretty sight.</para>  
    373   <para>In the next section you'll explore an alternate syntax that can help keep your expressions maintainable.</para>  
    374   </section>  
    375   </section>  
    376   <section id="re.verbose">  
    377   <?dbhtml filename="regular_expressions/verbose.html"?>  
    378   <title>Verbose Regular Expressions</title>  
    379   <abstract>  
    380   <title/>  
    381   <para>So far you've just been dealing with what I'll call <quote>compact</quote> regular expressions.  As you've seen, they are difficult to read, and even if you figure out what one does, that's no guarantee that you'll be able to understand it six months later.  What you really need is inline documentation.</para>  
    382   </abstract>  
    383   <para>&python; allows you to do this with something called <emphasis>verbose regular expressions</emphasis>.  A verbose regular expression is different from a compact regular expression in two ways:</para>  
    384   <itemizedlist>  
    385   <listitem><para>Whitespace is ignored.  Spaces, tabs, and carriage returns are not matched as spaces, tabs, and carriage returns.  They're not matched at all.  (If you want to match a space in a verbose regular expression, you'll need to escape it by putting a backslash in front of it.)</para></listitem>  
    386   <listitem><para>Comments are ignored.  A comment in a verbose regular expression is just like a comment in Python code: it starts with a <literal>#</literal> character and goes until the end of the line.  In this case it's a comment within a multi-line string instead of within your source code, but it works the same way.</para></listitem>  
    387   </itemizedlist>  
    388   <para>This will be more clear with an example.  Let's revisit the compact regular expression you've been working with, and make it a verbose regular expression.  This example shows how.</para>  
    389   <example>  
    390   <title>Regular Expressions with Inline Comments</title>  
    391   <screen>  
    392   &prompt;<userinput>pattern = """  
    393       ^                   # beginning of string  
    394       M{0,4}              # thousands - 0 to 4 M's  
    395       (CM|CD|D?C{0,3})    # hundreds - 900 (CM), 400 (CD), 0-300 (0 to 3 C's),  
    396                           #            or 500-800 (D, followed by 0 to 3 C's)  
    397       (XC|XL|L?X{0,3})    # tens - 90 (XC), 40 (XL), 0-30 (0 to 3 X's),  
    398                           #        or 50-80 (L, followed by 0 to 3 X's)  
    399       (IX|IV|V?I{0,3})    # ones - 9 (IX), 4 (IV), 0-3 (0 to 3 I's),  
    400                           #        or 5-8 (V, followed by 0 to 3 I's)  
    401       $                   # end of string  
    402       """</userinput>  
    403   &prompt;<userinput>re.search(pattern, 'M', re.VERBOSE)</userinput>                <co id="re.verbose.1.1"/>  
    404   <computeroutput>&lt;_sre.SRE_Match object at 0x008EEB48></computeroutput>  
    405   &prompt;<userinput>re.search(pattern, 'MCMLXXXIX', re.VERBOSE)</userinput>        <co id="re.verbose.1.2"/>  
    406   <computeroutput>&lt;_sre.SRE_Match object at 0x008EEB48></computeroutput>  
    407   &prompt;<userinput>re.search(pattern, 'MMMMDCCCLXXXVIII', re.VERBOSE)</userinput> <co id="re.verbose.1.3"/>  
    408   <computeroutput>&lt;_sre.SRE_Match object at 0x008EEB48></computeroutput>  
    409   &prompt;<userinput>re.search(pattern, 'M')</userinput>                            <co id="re.verbose.1.4"/>  
    410   </screen>  
    411   <calloutlist>  
    412   <callout arearefs="re.verbose.1.1">  
    413   <para>The most important thing to remember when using verbose regular expressions is that you need to pass an extra argument when working with them: <literal>re.VERBOSE</literal> is a constant defined in the &re; module that signals that the pattern should be treated as a verbose regular expression.  As you can see, this pattern has quite a bit of whitespace (all of which is ignored), and several comments (all of which are ignored).  Once you ignore the whitespace and the comments, this is exactly the same regular expression as you saw in <link linkend="re.nm">the previous section</link>, but it's a lot more readable.</para>  
    414   </callout>  
    415   <callout arearefs="re.verbose.1.2">  
    416   <para>This matches the start of the string, then one of a possible four <literal>M</literal>, then <literal>CM</literal>, then <literal>L</literal> and three of a possible three <literal>X</literal>, then <literal>IX</literal>, then the end of the string.</para>  
    417   </callout>  
    418   <callout arearefs="re.verbose.1.3">  
    419   <para>This matches the start of the string, then four of a possible four <literal>M</literal>, then <literal>D</literal> and three of a possible three <literal>C</literal>, then <literal>L</literal> and three of a possible three <literal>X</literal>, then <literal>V</literal> and three of a possible three <literal>I</literal>, then the end of the string.</para>  
    420   </callout>  
    421   <callout arearefs="re.verbose.1.4">  
    422   <para>This does not match.  Why?  Because it doesn't have the <literal>re.VERBOSE</literal> flag, so the <function>re.search</function> function is treating the pattern as a compact regular expression, with significant whitespace and literal hash marks.  &python; can't auto-detect whether a regular expression is verbose or not.  &python; assumes every regular expression is compact unless you explicitly state that it is verbose.</para>  
    423   </callout>  
    424   </calloutlist>  
    425   </example>  
    426   </section>  
    427   <section id="re.phone">  
    428   <?dbhtml filename="regular_expressions/phone_numbers.html"?>  
    429   <title>Case study: Parsing Phone Numbers</title>  
    430   <abstract>  
    431   <title/>  
    432   <para>So far you've concentrated on matching whole patterns.  Either the pattern matches, or it doesn't.  But regular expressions are much more powerful than that.  When a regular expression <emphasis>does</emphasis> match, you can pick out specific pieces of it.  You can find out what matched where.</para>  
    433   </abstract>  
    434   <para>This example came from another real-world problem I encountered, again from a previous day job.  The problem: parsing an American phone number.  The client wanted to be able to enter the number free-form (in a single field), but then wanted to store the area code, trunk, number, and optionally an extension separately in the company's database.  I scoured the Web and found many examples of regular expressions that purported to do this, but none of them were permissive enough.</para>  
    435   <para>Here are the phone numbers I needed to be able to accept:</para>  
    436   <itemizedlist>  
    437   <listitem><para><literal>800-555-1212</literal></para></listitem>  
    438   <listitem><para><literal>800 555 1212</literal></para></listitem>  
    439   <listitem><para><literal>800.555.1212</literal></para></listitem>  
    440   <listitem><para><literal>(800) 555-1212</literal></para></listitem>  
    441   <listitem><para><literal>1-800-555-1212</literal></para></listitem>  
    442   <listitem><para><literal>800-555-1212-1234</literal></para></listitem>  
    443   <listitem><para><literal>800-555-1212x1234</literal></para></listitem>  
    444   <listitem><para><literal>800-555-1212 ext. 1234</literal></para></listitem>  
    445   <listitem><para><literal>work 1-(800) 555.1212 #1234</literal></para></listitem>  
    446   </itemizedlist>  
    447   <para>Quite a variety!  In each of these cases, I need to know that the area code was <literal>800</literal>, the trunk was <literal>555</literal>, and the rest of the phone number was <literal>1212</literal>.  For those with an extension, I need to know that the extension was <literal>1234</literal>.</para>  
    448   <para>Let's work through developing a solution for phone number parsing.  This example shows the first step.</para>  
    449   <example id="re.phone.example">  
    450   <title>Finding Numbers</title>  
    451   <screen>  
    452   &prompt;<userinput>phonePattern = re.compile(r'^(\d{3})-(\d{3})-(\d{4})$')</userinput> <co id="re.phone.1.1"/>  
    453   &prompt;<userinput>phonePattern.search('800-555-1212').groups()</userinput>            <co id="re.phone.1.2"/>  
    454   <computeroutput>('800', '555', '1212')</computeroutput>  
    455   &prompt;<userinput>phonePattern.search('800-555-1212-1234')</userinput>                <co id="re.phone.1.3"/>  
    456   &prompt;  
    457   </screen>  
    458   <calloutlist>  
    459   <callout arearefs="re.phone.1.1">  
    460   <para>Always read regular expressions from left to right.  This one matches the beginning of the string, and then <literal>(\d{3})</literal>.  What's <literal>\d{3}</literal>?  Well, the <literal>{3}</literal> means <quote>match exactly three numeric digits</quote>; it's a variation on the <link linkend="re.nm"><literal>{n,m} syntax</literal></link> you saw earlier.  <literal>\d</literal> means <quote>any numeric digit</quote> (<literal>0</literal> through <literal>9</literal>).  Putting it in parentheses means <quote>match exactly three numeric digits, <emphasis>and then remember them as a group that I can ask for later</emphasis></quote>.  Then match a literal hyphen.  Then match another group of exactly three digits.  Then another literal hyphen.  Then another group of exactly four digits.  Then match the end of the string.</para>  
    461   </callout>  
    462   <callout arearefs="re.phone.1.2">  
    463   <para>To get access to the groups that the regular expression parser remembered along the way, use the <function>groups()</function> method on the object that the <function>search</function> function returns.  It will return a tuple of however many groups were defined in the regular expression.  In this case, you defined three groups, one with three digits, one with three digits, and one with four digits.</para>  
    464   </callout>  
    465   <callout arearefs="re.phone.1.3">  
    466   <para>This regular expression is not the final answer, because it doesn't handle a phone number with an extension on the end.  For that, you'll need to expand the regular expression.</para>  
    467   </callout>  
    468   </calloutlist>  
    469   </example>  
    470   <example>  
    471   <title>Finding the Extension</title>  
    472   <screen>  
    473   &prompt;<userinput>phonePattern = re.compile(r'^(\d{3})-(\d{3})-(\d{4})-(\d+)$')</userinput> <co id="re.phone.2.1"/>  
    474   &prompt;<userinput>phonePattern.search('800-555-1212-1234').groups()</userinput>             <co id="re.phone.2.2"/>  
    475   <computeroutput>('800', '555', '1212', '1234')</computeroutput>  
    476   &prompt;<userinput>phonePattern.search('800 555 1212 1234')</userinput>                      <co id="re.phone.2.3"/>  
    477   &prompt;  
    478   &prompt;<userinput>phonePattern.search('800-555-1212')</userinput>                           <co id="re.phone.2.4"/>  
    479   &prompt;  
    480   </screen>  
    481   <calloutlist>  
    482   <callout arearefs="re.phone.2.1">  
    483   <para>This regular expression is almost identical to the previous one.  Just as before, you match the beginning of the string, then a remembered group of three digits, then a hyphen, then a remembered group of three digits, then a hyphen, then a remembered group of four digits.  What's new is that you then match another hyphen, and a remembered group of one or more digits, then the end of the string.</para>  
    484   </callout>  
    485   <callout arearefs="re.phone.2.2">  
    486   <para>The <function>groups()</function> method now returns a tuple of four elements, since the regular expression now defines four groups to remember.</para>  
    487   </callout>  
    488   <callout arearefs="re.phone.2.3">  
    489   <para>Unfortunately, this regular expression is not the final answer either, because it assumes that the different parts of the phone number are separated by hyphens.  What if they're separated by spaces, or commas, or dots?  You need a more general solution to match several different types of separators.</para>  
    490   </callout>  
    491   <callout arearefs="re.phone.2.4">  
    492   <para>Oops!  Not only does this regular expression not do everything you want, it's actually a step backwards, because now you can't parse phone numbers <emphasis>without</emphasis> an extension.  That's not what you wanted at all; if the extension is there, you want to know what it is, but if it's not there, you still want to know what the different parts of the main number are.</para>  
    493   </callout>  
    494   </calloutlist>  
    495   </example>  
    496   <para>The next example shows the regular expression to handle separators between the different parts of the phone number.</para>  
    497   <example>  
    498   <title>Handling Different Separators</title>  
    499   <screen>  
    500   &prompt;<userinput>phonePattern = re.compile(r'^(\d{3})\D+(\d{3})\D+(\d{4})\D+(\d+)$')</userinput> <co id="re.phone.3.1"/>  
    501   &prompt;<userinput>phonePattern.search('800 555 1212 1234').groups()</userinput>                   <co id="re.phone.3.2"/>  
    502   <computeroutput>('800', '555', '1212', '1234')</computeroutput>  
    503   &prompt;<userinput>phonePattern.search('800-555-1212-1234').groups()</userinput>                   <co id="re.phone.3.3"/>  
    504   <computeroutput>('800', '555', '1212', '1234')</computeroutput>  
    505   &prompt;<userinput>phonePattern.search('80055512121234')</userinput>                               <co id="re.phone.3.4"/>  
    506   &prompt;  
    507   &prompt;<userinput>phonePattern.search('800-555-1212')</userinput>                                 <co id="re.phone.3.5"/>  
    508   &prompt;  
    509   </screen>  
    510   <calloutlist>  
    511   <callout arearefs="re.phone.3.1">  
    512   <para>Hang on to your hat.  You're matching the beginning of the string, then a group of three digits, then <literal>\D+</literal>.  What the heck is that?  Well, <literal>\D</literal> matches any character <emphasis>except</emphasis> a numeric digit, and <literal>+</literal> means <quote>1 or more</quote>.  So <literal>\D+</literal> matches one or more characters that are not digits.  This is what you're using instead of a literal hyphen, to try to match different separators.</para>  
    513   </callout>  
    514   <callout arearefs="re.phone.3.2">  
    515   <para>Using <literal>\D+</literal> instead of <literal>-</literal> means you can now match phone numbers where the parts are separated by spaces instead of hyphens.</para>  
    516   </callout>  
    517   <callout arearefs="re.phone.3.3">  
    518   <para>Of course, phone numbers separated by hyphens still work too.</para>  
    519   </callout>  
    520   <callout arearefs="re.phone.3.4">  
    521   <para>Unfortunately, this is still not the final answer, because it assumes that there is a separator at all.  What if the phone number is entered without any spaces or hyphens at all?</para>  
    522   </callout>  
    523   <callout arearefs="re.phone.3.4">  
    524   <para>Oops!  This still hasn't fixed the problem of requiring extensions.  Now you have two problems, but you can solve both of them with the same technique.</para>  
    525   </callout>  
    526   </calloutlist>  
    527   </example>  
    528   <para>The next example shows the regular expression for handling phone numbers <emphasis>without</emphasis> separators.</para>  
    529   <example>  
    530   <title>Handling Numbers Without Separators</title>  
    531   <screen>  
    532   &prompt;<userinput>phonePattern = re.compile(r'^(\d{3})\D*(\d{3})\D*(\d{4})\D*(\d*)$')</userinput> <co id="re.phone.4.1"/>  
    533   &prompt;<userinput>phonePattern.search('80055512121234').groups()</userinput>                      <co id="re.phone.4.2"/>  
    534   <computeroutput>('800', '555', '1212', '1234')</computeroutput>  
    535   &prompt;<userinput>phonePattern.search('800.555.1212 x1234').groups()</userinput>                  <co id="re.phone.4.3"/>  
    536   <computeroutput>('800', '555', '1212', '1234')</computeroutput>  
    537   &prompt;<userinput>phonePattern.search('800-555-1212').groups()</userinput>                        <co id="re.phone.4.4"/>  
    538   <computeroutput>('800', '555', '1212', '')</computeroutput>  
    539   &prompt;<userinput>phonePattern.search('(800)5551212 x1234')</userinput>                           <co id="re.phone.4.5"/>  
    540   &prompt;  
    541   </screen>  
    542   <calloutlist>  
    543   <callout arearefs="re.phone.4.1">  
    544   <para>The only change you've made since that last step is changing all the <literal>+</literal> to <literal>*</literal>.  Instead of <literal>\D+</literal> between the parts of the phone number, you now match on <literal>\D*</literal>.  Remember that <literal>+</literal> means <quote>1 or more</quote>?  Well, <literal>*</literal> means <quote>zero or more</quote>.  So now you should be able to parse phone numbers even when there is no separator character at all.</para>  
    545   </callout>  
    546   <callout arearefs="re.phone.4.2">  
    547   <para>Lo and behold, it actually works.  Why?  You matched the beginning of the string, then a remembered group of three digits (<literal>800</literal>), then zero non-numeric characters, then a remembered group of three digits (<literal>555</literal>), then zero non-numeric characters, then a remembered group of four digits (<literal>1212</literal>), then zero non-numeric characters, then a remembered group of an arbitrary number of digits (<literal>1234</literal>), then the end of the string.</para>  
    548   </callout>  
    549   <callout arearefs="re.phone.4.3">  
    550   <para>Other variations work now too: dots instead of hyphens, and both a space and an <literal>x</literal> before the extension.</para>  
    551   </callout>  
    552   <callout arearefs="re.phone.4.4">  
    553   <para>Finally, you've solved the other long-standing problem: extensions are optional again.  If no extension is found, the <function>groups()</function> method still returns a tuple of four elements, but the fourth element is just an empty string.</para>  
    554   </callout>  
    555   <callout arearefs="re.phone.4.5">  
    556   <para>I hate to be the bearer of bad news, but you're not finished yet.  What's the problem here?  There's an extra character before the area code, but the regular expression assumes that the area code is the first thing at the beginning of the string.  No problem, you can use the same technique of <quote>zero or more non-numeric characters</quote> to skip over the leading characters before the area code.</para>  
    557   </callout>  
    558   </calloutlist>  
    559   </example>  
    560   <para>The next example shows how to handle leading characters in phone numbers.</para>  
    561   <example>  
    562   <title>Handling Leading Characters</title>  
    563   <screen>  
    564   &prompt;<userinput>phonePattern = re.compile(r'^\D*(\d{3})\D*(\d{3})\D*(\d{4})\D*(\d*)$')</userinput> <co id="re.phone.5.1"/>  
    565   &prompt;<userinput>phonePattern.search('(800)5551212 ext. 1234').groups()</userinput>                 <co id="re.phone.5.2"/>  
    566   <computeroutput>('800', '555', '1212', '1234')</computeroutput>  
    567   &prompt;<userinput>phonePattern.search('800-555-1212').groups()</userinput>                           <co id="re.phone.5.3"/>  
    568   <computeroutput>('800', '555', '1212', '')</computeroutput>  
    569   &prompt;<userinput>phonePattern.search('work 1-(800) 555.1212 #1234')</userinput>                     <co id="re.phone.5.4"/>  
    570   &prompt;  
    571   </screen>  
    572   <calloutlist>  
    573   <callout arearefs="re.phone.5.1">  
    574   <para>This is the same as in the previous example, except now you're matching <literal>\D*</literal>, zero or more non-numeric characters, before the first remembered group (the area code).  Notice that you're not remembering these non-numeric characters (they're not in parentheses).  If you find them, you'll just skip over them and then start remembering the area code whenever you get to it.</para>  
    575   </callout>  
    576   <callout arearefs="re.phone.5.2">  
    577   <para>You can successfully parse the phone number, even with the leading left parenthesis before the area code.  (The right parenthesis after the area code is already handled; it's treated as a non-numeric separator and matched by the <literal>\D*</literal> after the first remembered group.)</para>  
    578   </callout>  
    579   <callout arearefs="re.phone.5.3">  
    580   <para>Just a sanity check to make sure you haven't broken anything that used to work.  Since the leading characters are entirely optional, this matches the beginning of the string, then zero non-numeric characters, then a remembered group of three digits (<literal>800</literal>), then one non-numeric character (the hyphen), then a remembered group of three digits (<literal>555</literal>), then one non-numeric character (the hyphen), then a remembered group of four digits (<literal>1212</literal>), then zero non-numeric characters, then a remembered group of zero digits, then the end of the string.</para>  
    581   </callout>  
    582   <callout arearefs="re.phone.5.4">  
    583   <para>This is where regular expressions make me want to gouge my eyes out with a blunt object.  Why doesn't this phone number match?  Because there's a <literal>1</literal> before the area code, but you assumed that all the leading characters before the area code were non-numeric characters (<literal>\D*</literal>).  Aargh.</para>  
    584   </callout>  
    585   </calloutlist>  
    586   </example>  
    587   <para>Let's back up for a second.  So far the regular expressions have all matched from the beginning of the string.  But now you see that there may be an indeterminate amount of stuff at the beginning of the string that you want to ignore.  Rather than trying to match it all just so you can skip over it, let's take a different approach: don't explicitly match the beginning of the string at all.  This approach is shown in the next example.</para>  
    588   <example>  
    589   <title>Phone Number, Wherever I May Find Ye</title>  
    590   <screen>  
    591   &prompt;<userinput>phonePattern = re.compile(r'(\d{3})\D*(\d{3})\D*(\d{4})\D*(\d*)$')</userinput> <co id="re.phone.6.1"/>  
    592   &prompt;<userinput>phonePattern.search('work 1-(800) 555.1212 #1234').groups()</userinput>        <co id="re.phone.6.2"/>  
    593   <computeroutput>('800', '555', '1212', '1234')</computeroutput>  
    594   &prompt;<userinput>phonePattern.search('800-555-1212')</userinput>                                <co id="re.phone.6.3"/>  
    595   <computeroutput>('800', '555', '1212', '')</computeroutput>  
    596   &prompt;<userinput>phonePattern.search('80055512121234')</userinput>                              <co id="re.phone.6.4"/>  
    597   <computeroutput>('800', '555', '1212', '1234')</computeroutput>  
    598   </screen>  
    599   <calloutlist>  
    600   <callout arearefs="re.phone.6.1">  
    601   <para>Note the lack of <literal>^</literal> in this regular expression.  You are not matching the beginning of the string anymore.  There's nothing that says you need to match the entire input with your regular expression.  The regular expression engine will do the hard work of figuring out where the input string starts to match, and go from there.</para>  
    602   </callout>  
    603   <callout arearefs="re.phone.6.2">  
    604   <para>Now you can successfully parse a phone number that includes leading characters and a leading digit, plus any number of any kind of separators around each part of the phone number.</para>  
    605   </callout>  
    606   <callout arearefs="re.phone.6.3">  
    607   <para>Sanity check.  this still works.</para>  
    608   </callout>  
    609   <callout arearefs="re.phone.6.4">  
    610   <para>That still works too.</para>  
    611   </callout>  
    612   </calloutlist>  
    613   </example>  
    614   <para>See how quickly a regular expression can get out of control?  Take a quick glance at any of the previous iterations.  Can you tell the difference between one and the next?</para>  
    615   <para>While you still understand the final answer (and it is the final answer; if you've discovered a case it doesn't handle, I don't want to know about it), let's write it out as a verbose regular expression, before you forget why you made the choices you made.</para>  
    616   <example>  
    617   <title>Parsing Phone Numbers (Final Version)</title>  
    618   <screen>  
    619   &prompt;<userinput>phonePattern = re.compile(r'''  
    620                   # don't match beginning of string, number can start anywhere  
    621       (\d{3})     # area code is 3 digits (e.g. '800')  
    622       \D*         # optional separator is any number of non-digits  
    623       (\d{3})     # trunk is 3 digits (e.g. '555')  
    624       \D*         # optional separator  
    625       (\d{4})     # rest of number is 4 digits (e.g. '1212')  
    626       \D*         # optional separator  
    627       (\d*)       # extension is optional and can be any number of digits  
    628       $           # end of string  
    629       ''', re.VERBOSE)</userinput>  
    630   &prompt;<userinput>phonePattern.search('work 1-(800) 555.1212 #1234').groups()</userinput>        <co id="re.phone.7.1"/>  
    631   <computeroutput>('800', '555', '1212', '1234')</computeroutput>  
    632   &prompt;<userinput>phonePattern.search('800-555-1212')</userinput>                                <co id="re.phone.7.2"/>  
    633   <computeroutput>('800', '555', '1212', '')</computeroutput>  
    634   </screen>  
    635   <calloutlist>  
    636   <callout arearefs="re.phone.7.1">  
    637   <para>Other than being spread out over multiple lines, this is exactly the same regular expression as the last step, so it's no surprise that it parses the same inputs.</para>  
    638   </callout>  
    639   <callout arearefs="re.phone.7.2">  
    640   <para>Final sanity check.  Yes, this still works.  You're done.</para>  
    641   </callout>  
    642   </calloutlist>  
    643   </example>  
    644   <itemizedlist role="furtherreading">  
    645   <title>Further Reading on Regular Expressions</title>  
    646   <listitem><para>&rehowto; teaches about regular expressions and how to use them in &python;.</para></listitem>  
    647   <listitem><para>&pythonlibraryreference; summarizes the &remodule_link;.</para></listitem>  
    648   </itemizedlist>  
    649   </section>  
    650   <section id="re.summary">  
    651   <?dbhtml filename="regular_expressions/summary.html"?>  
    652   <title>Summary</title>  
    653   <abstract>  
    654   <title/>  
    655   <para>This is just the tiniest tip of the iceberg of what regular expressions can do.  In other words, even though you're completely overwhelmed by them now, believe me, you ain't seen nothing yet.</para>  
    656   </abstract>  
    657   <para>You should now be familiar with the following techniques:</para>  
    658   <itemizedlist>  
    659   <listitem><para><literal>^</literal> matches the beginning of a string.</para></listitem>  
    660   <listitem><para><literal>$</literal> matches the end of a string.</para></listitem>  
    661   <listitem><para><literal>\b</literal> matches a word boundary.</para></listitem>  
    662   <listitem><para><literal>\d</literal> matches any numeric digit.</para></listitem>  
    663   <listitem><para><literal>\D</literal> matches any non-numeric character.</para></listitem>  
    664   <listitem><para><literal>x?</literal> matches an optional <literal>x</literal> character (in other words, it matches an <literal>x</literal> zero or one times).</para></listitem>  
    665   <listitem><para><literal>x*</literal> matches <literal>x</literal> zero or more times.</para></listitem>  
    666   <listitem><para><literal>x+</literal> matches <literal>x</literal> one or more times.</para></listitem>  
    667   <listitem><para><literal>x{n,m}</literal> matches an <literal>x</literal> character at least <literal>n</literal> times, but not more than <literal>m</literal> times.</para></listitem>  
    668   <listitem><para><literal>(a|b|c)</literal> matches either <literal>a</literal> or <literal>b</literal> or <literal>c</literal>.</para></listitem>  
    669   <listitem><para><literal>(x)</literal> in general is a <emphasis>remembered group</emphasis>.  You can get the value of what matched by using the <function>groups()</function> method of the object returned by <function>re.search</function>.</para></listitem>  
    670   </itemizedlist>  
    671   <para>Regular expressions are extremely powerful, but they are not the correct solution for every problem.  You should learn enough about them to know when they are appropriate, when they will solve your problems, and when they will cause more problems than they solve.</para>  
    672   <blockquote><attribution>Jamie Zawinski, <ulink url="http://groups.google.com/groups?selm=33F0C496.370D7C45%40netscape.com">in comp.emacs.xemacs</ulink></attribution><para>Some people, when confronted with a problem, think <quote>I know, I'll use regular expressions.</quote>  Now they have two problems.</para></blockquote>  
    673   </section>  
    674   </chapter>  
    675    
    676   <!--  
    677   * Intro/Diving in  
    678     * This chapter is for Python programmers who have read the first three chapters of this book, but have never used regular expressions.  If you have used regular expressions in some other language (such as Perl), this chapter is not for you; go read some other document [link, find "Python RE for Perl programmers", or write it] and get on with your life.  
    679     * Jamie Zawinski (comp.lang.emacs): "Some people, when confronted with a problem, think 'I know, I'll use regular expressions.'  Now they have two problems."  
    680   * Case study: normalizing addresses  
    681     * Use dialect.re section  
    682     * Patterns:  
    683       $  Match at end  
    684       \b Match at word boundary  
    685       ^  Match at beginning (add mention)  
    686   - Case study: validating Roman numerals  
    687     * Roman numeral pattern (storyboard)  
    688     * Storyboard:  
    689       steal most of roman.stage5  
    690       '^M?M?M?(CM|CD|D?C?C?C?)(XC|XL|L?X?X?X?)(IX|IV|V?I?I?I?)$'  
    691       '^M{0,3}(CM|CD|D?C{0,3})(XC|XL|L?X{0,3})(IX|IV|V?I{0,3})$')  
    692    
    693     * Patterns:  
    694       .   Match any character  
    695       ?   Match previous 0 or 1 times (optional)  
    696       |   or  
    697       {n,m} Match previous anywhere from n to m times  
    698    
    699     * Verbose regular expressions  
    700    
    701   - Case study: extracting parts of an American phone number  
    702     - Use phone number validation example  
    703     - "grouping"  
    704     - re.search(r'\D*(\d{3})\D*(\d{3})\D*(\d{4})\D*(\d*)$', s).groups()  
    705     - Patterns:  
    706       () define group which can be accessed later (really saw it in last section, but didn't fully utilize it)  
    707       \d Match any digit  
    708       \D Match any non-digit  
    709       *  Match previous as many times as possible, 0 or more times  
    710       +  Match previous as many times as possible, at least once  
    711       *? Match previous as few times as possible, 0 or more times  
    712       +? Match previous as few times as possible, at least once  
    713   -->  
      1 <?xml version="1.0" encoding="utf-8"?>  
      2 <chapter id="re">  
      3 <?dbhtml filename="regular_expressions/index.html"?>  
      4 <title>正则表达式</title>  
      5 <titleabbrev id="re.numberonly">Chapter 7</titleabbrev>  
      6 <abstract>  
      7 <title/>  
      8 <para>正则表达式是搜索、替换和解析复杂字符模式的一种强大而标准的方法。如果你曾经在其他语言(如&perl;)中使用过它,他们的语法非常相似,那么你仅仅阅读一下re模块的摘要,大致了解其中可用的函数和参数就可以了。</para>  
      9 </abstract>  
      10 <section id="re.intro">  
      11 <title>深入</title>  
      12 <para>字符串也有很多方法,可以进行搜索(<function>index</function>, <function>find</function>, 和 <function>count</function>), 替换(<function>replace</function>)和解析 (<function>split</function>), 但他们仅限于处理最简单的情况。搜索方法查找单个和固定编码的子串,并且他们总是大小写敏感的。对一个字符串<varname>s</varname>, 如果要进行大小写不敏感的搜索,则你必须调用 <function>s.lower()</function> 或 <function>s.upper()</function> 将s转换成全小写或者全大写,然后确保搜索串有着相匹配的大小写。<function>replace</function> 和 <function>split</function>方法有着类似的限制。</para>  
      13 <abstract>  
      14 <title/>  
      15 <para>如果你要解决的问题利用字符串函数能够完成,你应该使用他们。他们快速、简单且容易阅读,而对于快速、简单、可读性强的代码等方面有很多内容。但是,如果你发现你用了许多不同的字符串函数和if语句来处理一个特殊情况,或者你组合使用了split、join等函数而导致用一种奇怪的甚至读不下去的方式理解列表,此时,你也许需要转到正则表达式了。</para>  
      16 </abstract>  
      17 <para>尽管正则表达式语法较之普通代码相对麻烦一些,但是却可以得到更可读的结果,与用一长串字符串函数的解决方案相比要好很多。在正则表达式内部有多种方法嵌入注释,从而使之具有自文档化(self-documenting)的能力。</para>  
      18 </section>  
      19 <section id="re.matching">  
      20 <?dbhtml filename="regular_expressions/street_addresses.html"?>  
      21 <title>个案研究:街道地址</title>  
      22 <abstract>  
      23 <title/>  
      24 <para>这一系列的例子是由我几年前日常工作中的现实问题启发而来的,当时我需要从一个老化系统中导出街道地址,在将他们导入新的系统之前,进行清理和标准化。(看,我不是只将这些东西堆到一起,他有实际的用处)。这个例子展示我如何处理这个问题。</para>  
      25 </abstract>  
      26 <example>  
      27 <title>在字符串的结尾匹配</title>  
      28 <screen>  
      29 &prompt;<userinput>s = '100 NORTH MAIN ROAD'</userinput>  
      30 &prompt;<userinput>s.replace('ROAD', 'RD.')</userinput>               <co id="re.matching.1.1"/>  
      31 <computeroutput>'100 NORTH MAIN RD.'</computeroutput>  
      32 &prompt;<userinput>s = '100 NORTH BROAD ROAD'</userinput>  
      33 &prompt;<userinput>s.replace('ROAD', 'RD.')</userinput>               <co id="re.matching.1.2"/>  
      34 <computeroutput>'100 NORTH BRD. RD.'</computeroutput>  
      35 &prompt;<userinput>s[:-4] + s[-4:].replace('ROAD', 'RD.')</userinput> <co id="re.matching.1.3"/>  
      36 <computeroutput>'100 NORTH BROAD RD.'</computeroutput>  
      37 &prompt;<userinput>import re</userinput>                              <co id="re.matching.1.4"/>  
      38 &prompt;<userinput>re.sub('ROAD$', 'RD.', s)</userinput>              <co id="re.matching.1.5"/> <co id="re.matching.1.6"/>  
      39 <computeroutput>'100 NORTH BROAD RD.'</computeroutput></screen>  
      40 <calloutlist>  
      41 <callout arearefs="re.matching.1.1">  
      42 <para>我的目标是将街道地址标准化,<literal>'ROAD'</literal>通常被略写为<literal>'RD.'</literal>。乍看起来,我以为这个太简单了,只用字符串的方法replace就可以了。毕竟,所有的数据都已经是大写的了,因此大小写不匹配将不是问题。并且,要搜索的串<literal>'ROAD'</literal>是一个常量,在这个迷惑的简单例子中,s.replace的确能够胜任。</para>  
      43 </callout>  
      44 <callout arearefs="re.matching.1.2">  
      45 <para>不幸的是,生活充满了特例,并且我很快就意识到这个问题。比如:<literal>'ROAD'</literal>在地址中出现两次,一次是作为街道名称<literal>'BROAD'</literal>的一部分,一次是作为<literal>'ROAD'</literal>本身。replace方法遇到这两处的<literal>'ROAD'</literal>并没有区别,因此都进行了替换,而我发现地址被破坏掉了。</para>  
      46 </callout>  
      47 <callout arearefs="re.matching.1.3">  
      48 <para>为了解决在地址中出现多次<literal>'ROAD'</literal>子串的问题,有可能采用类似这样的方法:只在地址的最后四个字符中搜索替换<literal>'ROAD'</literal>(s[-4:]),忽略字符串的其他部分(s[:-4])。但是,你可能发现这已经变得不方便了。例如,该模式依赖于你要替换的字符串的长度了(如果你要把<literal>'STREET'</literal>替换为'ST.',你需要利用s[:-6]和s[-6:].replace(...))。你愿意在六月个期间回来调试他们么?我本人是不愿意的。</para>  
      49 </callout>  
      50 <callout arearefs="re.matching.1.4">  
      51 <para>是时候转到正则表达式了。在Python中,所有和正则表达式相关的功能都包含在re模块中。</para>  
      52 </callout>  
      53 <callout arearefs="re.matching.1.5">  
      54 <para>来看第一个参数:<literal>'ROAD$'</literal>。这个正则表达式非常简单,只有当<literal>'ROAD'</literal>出现在一个字符串的尾部时才会匹配。字符<literal>$</literal>表示“字符串的末尾”(还有一个对应的字符,尖号^,表示“字符串的开始”)。</para>  
      55 </callout>  
      56 <callout arearefs="re.matching.1.6">  
      57 <para>利用re.sub函数,对字符串s进行搜索,满足正则表达式<literal>'ROAD$'</literal>的用<literal>'RD.'</literal>替换。这样将匹配字符串s末尾的<literal>'ROAD'</literal>,而不会匹配属于单词<literal>'ROAD'</literal>一部分的<literal>'ROAD'</literal>,这是因为它是出现在s的中间。</para>  
      58 </callout>  
      59 </calloutlist>  
      60 </example>  
      61 <para>继续我的清理地址的故事,很快,我发现上面的例子,仅仅匹配地址末尾的<literal>'ROAD'</literal>不是很好,因为不是所有的地址都包括一个街道的命名;有一些是以街道名结尾的。大部分情况下,不会遇到这种情况,但是,如果街道名称为<literal>'BROAD'</literal>,那么正则表达式将会匹配<literal>'BROAD'</literal>的一部分为<literal>'ROAD'</literal>,而这并不是我想要的。</para>  
      62 <example>  
      63 <title>匹配整个单词</title>  
      64 <screen>  
      65 &prompt;<userinput>s = '100 BROAD'</userinput>  
      66 &prompt;<userinput>re.sub('ROAD$', 'RD.', s)</userinput>  
      67 <computeroutput>'100 BRD.'</computeroutput>  
      68 &prompt;<userinput>re.sub('\\bROAD$', 'RD.', s)</userinput>  <co id="re.matching.2.2"/>  
      69 <computeroutput>'100 BROAD'</computeroutput>  
      70 &prompt;<userinput>re.sub(r'\bROAD$', 'RD.', s)</userinput>  <co id="re.matching.2.3"/>  
      71 <computeroutput>'100 BROAD'</computeroutput>  
      72 &prompt;<userinput>s = '100 BROAD ROAD APT. 3'</userinput>  
      73 &prompt;<userinput>re.sub(r'\bROAD$', 'RD.', s)</userinput>  <co id="re.matching.2.4"/>  
      74 <computeroutput>'100 BROAD ROAD APT. 3'</computeroutput>  
      75 &prompt;<userinput>re.sub(r'\bROAD\b', 'RD.', s)</userinput> <co id="re.matching.2.5"/>  
      76 <computeroutput>'100 BROAD RD. APT 3'</computeroutput></screen>  
      77 <calloutlist>  
      78 <callout arearefs="re.matching.2.2">  
      79 <para>我真正想要做的是,当<literal>'ROAD'</literal>出现在字符串的末尾,并且是作为一个独立的单词时,而不是一些长单词的一部分,才对他进行匹配。为了在正则表达式中表达这个意思,你利用\b,它的含义是“单词的边界必须在这里”。在Python中,由于字符'\'在一个字符串中必须转义这个事实,从而变得非常麻烦。有时候,这类问题被称为“反斜线灾难”,这也是Perl中正则表达式比Python的正则表达式要相对容易的原因之一。另一方面,Perl也混淆了正则表达式和其他语法,因此,如果你发现一个bug,很难弄清楚究竟是一个语法错误,还是一个正则表达式错误。</para>  
      80 </callout>  
      81 <callout arearefs="re.matching.2.3">  
      82 <para>为了避免反斜线灾难,你可以利用所谓的“原始字符串”,只要为字符串添加一个前缀'r.'就可以了。这将告诉Python,字符串中的所有字符都不转义;'\t'是一个制表符,而r'\t'是一个真正的反斜线字符'\',紧跟着一个字母r。我推荐只要处理正则表达式,就使用原始字符串;否则,事情会很快变得混乱(并且正则表达式自己也会很快被自己搞乱了)。</para>  
      83 </callout>  
      84 <callout arearefs="re.matching.2.4">  
      85 <para>(一声叹息),很不幸,我很快发现更多的与我的逻辑相矛盾的例子。在这个例子中,街道地址包含有作为整个单词的<literal>'ROAD'</literal>,但是他不是在末尾,因为地址在街道命名后会有一个房间号。由于<literal>'ROAD'</literal>不是在每一个字符串的末尾,没有匹配上,因此调用re.sub没有替换任何东西,你获得的知识初始字符串,这也不是我们想要的。</para>  
      86 </callout>  
      87 <callout arearefs="re.matching.2.5">  
      88 <para>为了解决这个问题,我去掉了$字符,加上另一个\b。现在,正则表达式“匹配字符串中作为整个单词出现的<literal>'ROAD'</literal>了”,不论是在末尾、开始还是中间。</para>  
      89 </callout>  
      90 </calloutlist>  
      91 </example>  
      92 </section>  
      93 <section id="re.roman">  
      94 <?dbhtml filename="regular_expressions/roman_numerals.html"?>  
      95 <title>个案研究:罗马字母</title>  
      96 <abstract>  
      97 <title/>  
      98 <para>你可能经常看到罗马数字,即使你没有意识到他们。你可能曾经在老电影或者电视中看到他们(<quote>版权所有 <literal>MCMXLVI</literal></quote> 而不是 <quote>版权所有<literal>1946</literal></quote>),或者在某图书馆或某大学的贡献墙上看到他们(<quote>成立于 <literal>MDCCCLXXXVIII</literal></quote>而不是<quote>成立于<literal>1888</literal></quote>)。你也可能在某些文献的大纲或者目录上看到他们。这是一个表示数字的系统,他能够真正回溯到远古的罗马帝国(因此而得名)。</para>  
      99 </abstract>  
      100 <para>在罗马数字中,利用7个不同字母进行重复或者组合来表达各式各样的数字。</para>  
      101 <itemizedlist>  
      102 <listitem><para><literal>I</literal> = <literal>1</literal></para></listitem>  
      103 <listitem><para><literal>V</literal> = <literal>5</literal></para></listitem>  
      104 <listitem><para><literal>X</literal> = <literal>10</literal></para></listitem>  
      105 <listitem><para><literal>L</literal> = <literal>50</literal></para></listitem>  
      106 <listitem><para><literal>C</literal> = <literal>100</literal></para></listitem>  
      107 <listitem><para><literal>D</literal> = <literal>500</literal></para></listitem>  
      108 <listitem><para><literal>M</literal> = <literal>1000</literal></para></listitem>  
      109 </itemizedlist>  
      110 <para>下面是关于构造罗马数字的一些通用的规则的介绍:</para>  
      111 <itemizedlist>  
      112 <listitem><para>字符是叠加的。  <literal>I</literal>表示&one;, <literal>II</literal>表示<literal>2</literal>, 而<literal>III</literal>表示<literal>3</literal>.  <literal>VI</literal> 表示 <literal>6</literal> (字面上为逐字符相加, <quote><literal>5</literal> 加 <literal>1</literal></quote>), <literal>VII</literal> 表示 <literal>7</literal>, <literal>VIII</literal> 表示 <literal>8</literal>.</para></listitem>  
      113 <listitem><para>能够被10整除的字符(<literal>I</literal>, <literal>X</literal>, <literal>C</literal>, 和 <literal>M</literal>)至多可以重复三次.  对于<literal>4</literal>, 你则需要利用下一个最大的能够被5整除的字符进行减操作得到,你不能把<literal>4</literal> 表示成 <literal>IIII</literal>; 而应表示为 <literal>IV</literal> (比<quote><literal>5</literal>小 <literal>1</literal></quote>)。数字<literal>40</literal>写成<literal>XL</literal> (比<literal>50</literal>小<literal>10</literal>), <literal>41</literal> 写成 <literal>XLI</literal>, <literal>42</literal> 写成 <literal>XLII</literal>, <literal>43</literal> 写成 <literal>XLIII</literal>, 而 <literal>44</literal> 写成 <literal>XLIV</literal> (比<literal>50</literal> 小<literal>10</literal>, 然后比<literal>5</literal>小<literal>1</literal>).</para></listitem>  
      114 <listitem><para>类似的,对于数字 <literal>9</literal>,你必须利用下一个能够被10整除的字符进行减操作得到: <literal>8</literal> 表示为 <literal>VIII</literal>, 而 <literal>9</literal> 则表示为 <literal>IX</literal> (比<literal>10</literal> 小<literal>1</literal>), 而不是 <literal>VIIII</literal> (因为字符<literal>I</literal> 不能连续重复四次)。数字<literal>90</literal> 表示为 <literal>XC</literal>, <literal>900</literal> 表示为 <literal>CM</literal>.</para></listitem>  
      115 <listitem><para>被5整除的字符不能重复。数字<literal>10</literal> 常表示为<literal>X</literal>, 而从来不用<literal>VV</literal>来表示。数字<literal>100</literal>常表示为<literal>C</literal>, 也从来不表示为 <literal>LL</literal>.</para></listitem>  
      116 <listitem><para>罗马数字经常从高位到低位书写,从左到右阅读,因此不同顺序的字符意义大不相同。<literal>DC</literal> 表示 <literal>600</literal>; 而<literal>CD</literal> 是一个完全不同的数字(为<literal>400</literal>, 也就是比<literal>500</literal> 小<literal>100</literal>).  <literal>CI</literal> 表示 <literal>101</literal>; 而<literal>IC</literal> 甚至不是一个合法的罗马字母(因为你不能直接从数字<literal>100</literal>减去<literal>1</literal>; 比需要写成<literal>XCIX</literal>, 意思是 比<literal>100</literal> 小<literal>10</literal>, 然后加上数字<literal>9</literal>,也就是比 <literal>10</literal>小<literal>1</literal>的数字).</para></listitem>  
      117 </itemizedlist>  
      118 <note>  
      119 <title/>  
      120 <para>本章译者注:“被5整除的数”这个译法并不严谨,因为所有被10整除的数也能够被5整除,此处表达的含义是:那些包含有5的含义的罗马数字字符。</para>  
      121 </note>  
      122 <section>  
      123 <title>校验千位数</title>  
      124 <para>怎样校验任意一个字符串是否为一个有效的罗马数字呢?我们每次只看一个数字,由于罗马数字经常是从高位到低位书写,我们从高位开始:千位。对于大于、等于1000的数字,千位有一系列的字符 <literal>M</literal> 表示。</para>  
      125 <example>  
      126 <title>校验千位数</title>  
      127 <screen>  
      128 &prompt;<userinput>import re</userinput>  
      129 &prompt;<userinput>pattern = '^M?M?M?$'</userinput>       <co id="re.roman.1.1"/>  
      130 &prompt;<userinput>re.search(pattern, 'M')</userinput>    <co id="re.roman.1.2"/>  
      131 <computeroutput>&lt;SRE_Match object at 0106FB58></computeroutput>  
      132 &prompt;<userinput>re.search(pattern, 'MM')</userinput>   <co id="re.roman.1.3"/>  
      133 <computeroutput>&lt;SRE_Match object at 0106C290></computeroutput>  
      134 &prompt;<userinput>re.search(pattern, 'MMM')</userinput>  <co id="re.roman.1.4"/>  
      135 <computeroutput>&lt;SRE_Match object at 0106AA38></computeroutput>  
      136 &prompt;<userinput>re.search(pattern, 'MMMM')</userinput> <co id="re.roman.1.5"/>  
      137 &prompt;<userinput>re.search(pattern, '')</userinput>     <co id="re.roman.1.6"/>  
      138 <computeroutput>&lt;SRE_Match object at 0106F4A8></computeroutput></screen>  
      139 <calloutlist>  
      140 <callout arearefs="re.roman.1.1">  
      141 <para>这个模式有三部分:</para>  
      142 <itemizedlist>  
      143 <listitem><para><literal>^</literal>表示仅仅在一个字符串的开始匹配其后的字符串内容。如果没有这个字符,这个模式将匹配出现在字符串任意位置上的  
      144 <literal>M</literal>,而这并不是你想要的。你想确认的是:字符串中是否出现字符<literal>M</literal>,如果出现,则必须是在字符串的开始。</para></listitem>  
      145 <listitem><para><literal>M?</literal> 可选的匹配单个字符<literal>M</literal>,由于他重复出现三次,你可以在一行中匹配0次到3次字符<literal>M</literal>。</para></listitem>  
      146 <listitem><para><literal>$</literal> 字符限制模式只能够在一个字符串的结尾匹配。当和模式开头的字符<literal>^</literal>结合使用时,这意味着模式必须匹配整个串,并且在在字符<literal>M</literal>的前后都不能够出现其他的任意字符。</para></listitem>  
      147 </itemizedlist>  
      148 </callout>  
      149 <callout arearefs="re.roman.1.2">  
      150 <para>&re; 模块的本质是一个&search; 函数,该函数有两个参数,一个是正则表达式(<varname>pattern</varname>),一个是字符串 (<literal>'M'</literal>),函数试图匹配正则表达式。如果发现一个匹配,&search; 函数返回一个拥有多种方法可以描述这个匹配的对象,如果没有发现匹配,&search; 函数返回一个&none;, 一个&python; 空值(null value)。你此刻关注的唯一事情,就是模式是否匹配上,可以利用 &search;函数的返回值弄清这个事实。字符串<literal>'M'</literal> 匹配上这个正则表达式,因为第一个可选的<literal>M</literal>匹配上,而第二个和第三个<literal>M</literal> 被忽略掉了。</para>  
      151 </callout>  
      152 <callout arearefs="re.roman.1.3">  
      153 <para><literal>'MM'</literal> 匹配上是因为第一和第二个可选的<literal>M</literal>匹配上,而忽略掉第三个<literal>M</literal>。</para>  
      154 </callout>  
      155 <callout arearefs="re.roman.1.4">  
      156 <para><literal>'MMM'</literal> 匹配上因为三个<literal>M</literal> 都匹配上了</para>  
      157 </callout>  
      158 <callout arearefs="re.roman.1.5">  
      159 <para><literal>'MMMM'</literal> 没有匹配上。因为所有的三个<literal>M</literal>都匹配上,但是正则表达式还有字符串尾部的限制 (由于字符 <literal>$</literal>), 然而字符串没有结束(因为还有第四个<literal>M</literal>字符), 因此 &search; 函数返回一个&none;.</para>  
      160 </callout>  
      161 <callout arearefs="re.roman.1.6">  
      162 <para>有趣的是,一个空字符串也能够匹配这个正则表达式,因为所有的字符 <literal>M</literal> 都是可选的。</para>  
      163 </callout>  
      164 </calloutlist>  
      165 </example>  
      166 </section>  
      167 <section>  
      168 <title>检验百位数</title>  
      169 <para>百位数的位置与千位数相比,识别起来要困难得多,这是因为有多种相互独立的表达方式都可以表达百位数,具体用那种方式表达和具体的数值相关。</para>  
      170 <itemizedlist>  
      171 <listitem><para><literal>100</literal> = <literal>C</literal></para></listitem>  
      172 <listitem><para><literal>200</literal> = <literal>CC</literal></para></listitem>  
      173 <listitem><para><literal>300</literal> = <literal>CCC</literal></para></listitem>  
      174 <listitem><para><literal>400</literal> = <literal>CD</literal></para></listitem>  
      175 <listitem><para><literal>500</literal> = <literal>D</literal></para></listitem>  
      176 <listitem><para><literal>600</literal> = <literal>DC</literal></para></listitem>  
      177 <listitem><para><literal>700</literal> = <literal>DCC</literal></para></listitem>  
      178 <listitem><para><literal>800</literal> = <literal>DCCC</literal></para></listitem>  
      179 <listitem><para><literal>900</literal> = <literal>CM</literal></para></listitem>  
      180 </itemizedlist>  
      181 <para>因此有四种可能的模式:</para>  
      182 <itemizedlist>  
      183 <listitem><para><literal>CM</literal></para></listitem>  
      184 <listitem><para><literal>CD</literal></para></listitem>  
      185 <listitem><para>零到三次出现<literal>C</literal> 字符 (如果是零,表示百位数为0)</para></listitem>  
      186 <listitem><para><literal>D</literal>, 后面跟零个到三个<literal>C</literal>字符</para></listitem>  
      187 </itemizedlist>  
      188 <para>后面两个模式可以结合到一起:</para>  
      189 <itemizedlist>  
      190 <listitem><para>一个可选的字符<literal>D</literal>, 加上零到3个<literal>C</literal> 字符。</para></listitem>  
      191 </itemizedlist>  
      192 <para>这个例子显示如何有效的识别罗马数字的百位数位置。</para>  
      193 <example id="re.roman.hundreds">  
      194 <title>检验百位数</title>  
      195 <screen>  
      196 &prompt;<userinput>import re</userinput>  
      197 &prompt;<userinput>pattern = '^M?M?M?(CM|CD|D?C?C?C?)$'</userinput> <co id="re.roman.2.1"/>  
      198 &prompt;<userinput>re.search(pattern, 'MCM')</userinput>            <co id="re.roman.2.2"/>  
      199 <computeroutput>&lt;SRE_Match object at 01070390></computeroutput>  
      200 &prompt;<userinput>re.search(pattern, 'MD')</userinput>             <co id="re.roman.2.3"/>  
      201 <computeroutput>&lt;SRE_Match object at 01073A50></computeroutput>  
      202 &prompt;<userinput>re.search(pattern, 'MMMCCC')</userinput>         <co id="re.roman.2.4"/>  
      203 <computeroutput>&lt;SRE_Match object at 010748A8></computeroutput>  
      204 &prompt;<userinput>re.search(pattern, 'MCMC')</userinput>           <co id="re.roman.2.5"/>  
      205 &prompt;<userinput>re.search(pattern, '')</userinput>               <co id="re.roman.2.6"/>  
      206 <computeroutput>&lt;SRE_Match object at 01071D98></computeroutput></screen>  
      207 <calloutlist>  
      208 <callout arearefs="re.roman.2.1">  
      209 <para>这个模式的首部和上一个模式相同,检查字符串的开始(<literal>^</literal>), 接着匹配千位数位置(<literal>M?M?M?</literal>),然后才是这个模式新的内容,在括号内,定义了包含有三个互相独立的模式集合,由垂直线隔开:<literal>CM</literal>, <literal>CD</literal>, 和 <literal>D?C?C?C?</literal> (<literal>D</literal>是可选字符,接着是0到3个可选的<literal>C</literal> 字符)。正则表达式解析器依次检查这些模式(从左到右), 如果匹配上第一个模式,则忽略剩下的模式。</para>  
      210 </callout>  
      211 <callout arearefs="re.roman.2.2">  
      212 <para><literal>'MCM'</literal> 匹配上,因为第一个<literal>M</literal> 字符匹配,第二和第三个<literal>M</literal>字符被忽略掉,而<literal>CM</literal> 匹配上 (因此 <literal>CD</literal> 和 <literal>D?C?C?C?</literal> 两个模式甚至不再考虑)。  <literal>MCM</literal> 表示罗马数字<literal>1900</literal>。</para>  
      213 </callout>  
      214 <callout arearefs="re.roman.2.3">  
      215 <para><literal>'MD'</literal> 匹配上,因为第一个字符<literal>M</literal> 匹配上, 第二第三个<literal>M</literal>字符忽略,而模式<literal>D?C?C?C?</literal> 匹配上<literal>D</literal> (模式中的三个可选的字符<literal>C</literal>都被忽略掉了)。  <literal>MD</literal> 表示罗马数字<literal>1500</literal>。</para>  
      216 </callout>  
      217 <callout arearefs="re.roman.2.4">  
      218 <para><literal>'MMMCCC'</literal> 匹配上,因为三个<literal>M</literal> 字符都匹配上,而模式<literal>D?C?C?C?</literal>匹配上<literal>CCC</literal> (字符<literal>D</literal>是可选的,此处忽略)。  <literal>MMMCCC</literal> 表示罗马数字<literal>3300</literal>。</para>  
      219 </callout>  
      220 <callout arearefs="re.roman.2.5">  
      221 <para><literal>'MCMC'</literal> 没有匹配上。第一个<literal>M</literal> 字符匹配上,第二第三个<literal>M</literal>字符忽略,接着是<literal>CM</literal> 匹配上,但是接着是 <literal>$</literal> 字符没有匹配,因为字符串还没有结束(你仍然还有一个没有匹配的<literal>C</literal>字符)。 <literal>C</literal> 字符 也<emphasis>不</emphasis> 匹配模式<literal>D?C?C?C?</literal>的一部分,因为与之相互独立的模式<literal>CM</literal>已经匹配上。</para>  
      222 </callout>  
      223 <callout arearefs="re.roman.2.6">  
      224 <para>有趣的是,一个空字符串也可以匹配这个模式,因为所有的 <literal>M</literal> 字符都是可选的,它们都被忽略,并且一个空字符串可以匹配<literal>D?C?C?C?</literal> 模式,此处所有的字符也都是可选的,并且都被忽略。</para>  
      225 </callout>  
      226 </calloutlist>  
      227 </example>  
      228 <para>吆!来看正则表达式能够多快变得难以理解?你仅仅表示了罗马数字的千位和百位上的数字。如果你根据类似的方法,十位数和各位数就非常简单了,因为是完全相同的模式。让我们来看表达这个模式的另一种方式吧。</para>  
      229 </section>  
      230 </section>  
      231 <section id="re.nm">  
      232 <?dbhtml filename="regular_expressions/n_m_syntax.html"?>  
      233 <title>使用<literal>{n,m}</literal> 语法</title>  
      234 <abstract>  
      235 <title/>  
      236 <para>在 <link linkend="re.roman">前面的章节</link>,你处理了相同字符可以重复三次的情况,在正则表达式中有另外一个方式来表达这种情况,并且使代码的可读性更好。首先来看我们在前面的例子中使用的方法。</para>  
      237 </abstract>  
      238 <example>  
      239 <title>老方法:每一个字符都是可选的</title>  
      240 <screen>  
      241 &prompt;<userinput>import re</userinput>  
      242 &prompt;<userinput>pattern = '^M?M?M?$'</userinput>  
      243 &prompt;<userinput>re.search(pattern, 'M')</userinput>    <co id="re.nm.1.1"/>  
      244 <computeroutput>&lt;_sre.SRE_Match object at 0x008EE090></computeroutput>  
      245 &prompt;<userinput>pattern = '^M?M?M?$'</userinput>  
      246 &prompt;<userinput>re.search(pattern, 'MM')</userinput>   <co id="re.nm.1.2"/>  
      247 <computeroutput>&lt;_sre.SRE_Match object at 0x008EEB48></computeroutput>  
      248 &prompt;<userinput>pattern = '^M?M?M?$'</userinput>  
      249 &prompt;<userinput>re.search(pattern, 'MMM')</userinput>  <co id="re.nm.1.3"/>  
      250 <computeroutput>&lt;_sre.SRE_Match object at 0x008EE090></computeroutput>  
      251 &prompt;<userinput>re.search(pattern, 'MMMM')</userinput> <co id="re.nm.1.4"/>  
      252 &prompt;  
      253 </screen>  
      254 <calloutlist>  
      255 <callout arearefs="re.nm.1.1">  
      256 <para>这个模式匹配串的开始,接着是第一个可选的字符<literal>M</literal>, 第二第三个<literal>M</literal>字符则被忽略,(这是可行的因为它们都是可选的),最后是字符串的结尾。</para>  
      257 </callout>  
      258 <callout arearefs="re.nm.1.2">  
      259 <para>这个模式匹配串的开始,接着是第一和第二个可选字符<literal>M</literal> ,而第三个<literal>M</literal> 字符被忽略(这是可行的因为它们都是可选的),最后匹配字符串的结尾。</para>  
      260 </callout>  
      261 <callout arearefs="re.nm.1.3">  
      262 <para>这个模式匹配字符串的开始,接着匹配所有的三个可选字符 <literal>M</literal>, 最后匹配字符串的结尾。</para>  
      263 </callout>  
      264 <callout arearefs="re.nm.1.4">  
      265 <para>这个模式匹配字符串的开始,接着匹配所有的三个可选字符<literal>M</literal>,但是不能够匹配字符串的结尾(因为还有一个未匹配的字符<literal>M</literal>),因此不能够匹配而返回一个&none;.</para>  
      266 </callout>  
      267 </calloutlist>  
      268 </example>  
      269 <example>  
      270 <title>一个新的方法:From <literal>n</literal> to <literal>m</literal></title>  
      271 <screen>  
      272 &prompt;<userinput>pattern = '^M{0,3}$'</userinput>       <co id="re.nm.2.0"/>  
      273 &prompt;<userinput>re.search(pattern, 'M')</userinput>    <co id="re.nm.2.1"/>  
      274 <computeroutput>&lt;_sre.SRE_Match object at 0x008EEB48></computeroutput>  
      275 &prompt;<userinput>re.search(pattern, 'MM')</userinput>   <co id="re.nm.2.2"/>  
      276 <computeroutput>&lt;_sre.SRE_Match object at 0x008EE090></computeroutput>  
      277 &prompt;<userinput>re.search(pattern, 'MMM')</userinput>  <co id="re.nm.2.3"/>  
      278 <computeroutput>&lt;_sre.SRE_Match object at 0x008EEDA8></computeroutput>  
      279 &prompt;<userinput>re.search(pattern, 'MMMM')</userinput> <co id="re.nm.2.4"/>  
      280 &prompt;  
      281 </screen>  
      282 <calloutlist>  
      283 <callout arearefs="re.nm.2.0">  
      284 <para>这个模式意识是说:<quote>匹配字符串的开始,接着匹配0到3个<literal>M</literal>字符,然后匹配字符串的结尾。</quote>  可是是0到3之间的任何数字,如果你想要匹配至少1次,至多3次字符<literal>M</literal>,则可以写成 <literal>M{1,3}</literal>。</para>  
      285 </callout>  
      286 <callout arearefs="re.nm.2.1">  
      287 <para>这个模式匹配字符串的开始,接着匹配三个可选<literal>M</literal>字符中的一个,最后是字符串的结尾。</para>  
      288 </callout>  
      289 <callout arearefs="re.nm.2.2">  
      290 <para>这个模式匹配字符串的开始,接着匹配三个可选<literal>M</literal>字符中的两个,最后是字符串的结尾。</para>  
      291 </callout>  
      292 <callout arearefs="re.nm.2.3">  
      293 <para>这个模式匹配字符串的开始,接着匹配三个可选<literal>M</literal>字符中的三个,最后是字符串的结尾。</para>  
      294 </callout>  
      295 <callout arearefs="re.nm.2.4">  
      296 <para>这个模式匹配字符串的开始,接着匹配三个可选<literal>M</literal>字符中的三个,但是<emphasis>没有匹配</emphasis>字符串的结尾。正则表达式在字符串结尾之前最多只允许批评三次<literal>M</literal>字符,但是实际上有四个 <literal>M</literal>字符,因此模式没有匹配上这个字符串,返回一个&none;.</para>  
      297 </callout>  
      298 </calloutlist>  
      299 </example>  
      300 <note>  
      301 <!--<title>比较正则表达式</title>-->  
      302 <title/>  
      303 <para>没有一个轻松的方法来确定两个正则表达式是否为等价的,你能采用的最好的办法就是列出很多的测试样例,确定这两个正则表达式对所有的相关输入都有相同的输出。在本书后面的章节,关于如何书写测试样例有更多的讨论。</para>  
      304 </note>  
      305 <section>  
      306 <title>校验十位数和个位数</title>  
      307 <para>现在我们来扩展扩展关于罗马数字的正则表达式,以匹配十位数和个位数,下面的例子展示十位数的校验方法。</para>  
      308 <example id="re.tens.example">  
      309 <title>校验十位数</title>  
      310 <screen>  
      311 &prompt;<userinput>pattern = '^M?M?M?M?(CM|CD|D?C?C?C?)(XC|XL|L?X?X?X?)$'</userinput>  
      312 &prompt;<userinput>re.search(pattern, 'MCMXL')</userinput>    <co id="re.nm.3.3"/>  
      313 <computeroutput>&lt;_sre.SRE_Match object at 0x008EEB48></computeroutput>  
      314 &prompt;<userinput>re.search(pattern, 'MCML')</userinput>     <co id="re.nm.3.4"/>  
      315 <computeroutput>&lt;_sre.SRE_Match object at 0x008EEB48></computeroutput>  
      316 &prompt;<userinput>re.search(pattern, 'MCMLX')</userinput>    <co id="re.nm.3.5"/>  
      317 <computeroutput>&lt;_sre.SRE_Match object at 0x008EEB48></computeroutput>  
      318 &prompt;<userinput>re.search(pattern, 'MCMLXXX')</userinput>  <co id="re.nm.3.7"/>  
      319 <computeroutput>&lt;_sre.SRE_Match object at 0x008EEB48></computeroutput>  
      320 &prompt;<userinput>re.search(pattern, 'MCMLXXXX')</userinput> <co id="re.nm.3.8"/>  
      321 &prompt;  
      322 </screen>  
      323 <calloutlist>  
      324 <callout arearefs="re.nm.3.3">  
      325 <para>这个模式匹配字符串的开始,接着是第一个可选字符<literal>M</literal>,接着是<literal>CM</literal>, 接着 <literal>XL</literal>, 接着是字符串的结尾。请记住,<literal>(A|B|C)</literal>这个语法的含义是<quote>精确匹配A, B, 或者 C其中的一个</quote>。此处匹配了<literal>XL</literal>, 因此不再匹配<literal>XC</literal> 和 <literal>L?X?X?X?</literal> ,接着就匹配到字符串的结尾。<literal>MCML</literal>表示罗马数字<literal>1940</literal>.</para>  
      326 </callout>  
      327 <callout arearefs="re.nm.3.4">  
      328 <para>这个模式匹配字符串的开始,接着是第一个可选字符<literal>M</literal>,接着是<literal>CM</literal>, 接着 <literal>L?X?X?X?</literal>。在模式<literal>L?X?X?X?</literal>中,他匹配<literal>L</literal>字符并且跳过所有可选的<literal>X</literal>字符,接着匹配字符串的结尾。<literal>MCML</literal> 表示罗马数字<literal>1950</literal>.</para>  
      329 </callout>  
      330 <callout arearefs="re.nm.3.5">  
      331 <para>这个模式匹配字符串的开始,接着是第一个可选字符<literal>M</literal>,接着是<literal>CM</literal>, 接着是可选的 <literal>L</literal>字符和可选的第一个<literal>X</literal>字符,并且跳过第二第三个可选的<literal>X</literal>字符,接着是字符串的结尾。 <literal>MCMLX</literal>表示罗马数字<literal>1960</literal>.</para>  
      332 </callout>  
      333 <callout arearefs="re.nm.3.7">  
      334 <para>这个模式匹配字符串的开始,接着是第一个可选字符<literal>M</literal>,接着是<literal>CM</literal>, 接着是可选的 <literal>L</literal>字符和所有的三个可选的<literal>X</literal>字符,接着匹配字符串的结尾。<literal>MCMLXXX</literal> 表示罗马数字 <literal>1980</literal>.</para>  
      335 </callout>  
      336 <callout arearefs="re.nm.3.8">  
      337 <para>这个模式匹配字符串的开始,接着是第一个可选字符<literal>M</literal>,接着是<literal>CM</literal>, 接着是可选的 <literal>L</literal>字符和所有的三个可选的<literal>X</literal>字符,接着就 <emphasis>未能匹配</emphasis> 字符串的结尾ie,因为还有一个未匹配的<literal>X</literal> 字符。所以整个模式匹配失败并返回一个 &none;.  <literal>MCMLXXXX</literal> 不是一个有效的罗马数字。</para>  
      338 </callout>  
      339 </calloutlist>  
      340 </example>  
      341 <para>对于个位数的正则表达式有类似的表达方式i,我将省略细节,直接展示结果。</para>  
      342 <informalexample>  
      343 <!--<title>个位数</title>-->  
      344 <screen>  
      345 &prompt;<userinput>pattern = '^M?M?M?M?(CM|CD|D?C?C?C?)(XC|XL|L?X?X?X?)(IX|IV|V?I?I?I?)$'</userinput>  
      346 </screen>  
      347 </informalexample>  
      348 <para>用另一种<literal>{n,m}</literal>语法表达这个正则表达式会如何呢?这个例子展示新的语法。</para>  
      349 <example id="re.nm.example">  
      350 <title>用<literal>{n,m}</literal>语法确认罗马数字</title>  
      351 <screen>  
      352 &prompt;<userinput>pattern = '^M{0,4}(CM|CD|D?C{0,3})(XC|XL|L?X{0,3})(IX|IV|V?I{0,3})$'</userinput>  
      353 &prompt;<userinput>re.search(pattern, 'MDLV')</userinput>             <co id="re.nm.4.1"/>  
      354 <computeroutput>&lt;_sre.SRE_Match object at 0x008EEB48></computeroutput>  
      355 &prompt;<userinput>re.search(pattern, 'MMDCLXVI')</userinput>         <co id="re.nm.4.2"/>  
      356 <computeroutput>&lt;_sre.SRE_Match object at 0x008EEB48></computeroutput>  
      357 &prompt;<userinput>re.search(pattern, 'MMMMDCCCLXXXVIII')</userinput> <co id="re.nm.4.3"/>  
      358 <computeroutput>&lt;_sre.SRE_Match object at 0x008EEB48></computeroutput>  
      359 &prompt;<userinput>re.search(pattern, 'I')</userinput>                <co id="re.nm.4.4"/>  
      360 <computeroutput>&lt;_sre.SRE_Match object at 0x008EEB48></computeroutput>  
      361 </screen>  
      362 <calloutlist>  
      363 <callout arearefs="re.nm.4.1">  
      364 <para>这个模式匹配字符串的开始,接着匹配四个可选的<literal>M</literal>字符的一个,接着匹配<literal>D?C{0,3}</literal>,此处,紧紧匹配可选的字符<literal>D</literal>和0个可选字符<literal>C</literal>。继续向前匹配,匹配<literal>L?X{0,3}</literal>,此处,匹配可选的<literal>L</literal> 字符和0个可选字符<literal>X</literal>,接着匹配<literal>V?I{0,3}</literal> ,此处,匹配可选的V和0个可选字符<literal>I</literal>,最后匹配字符串的结尾。<literal>MDLV</literal> 表示罗马数字<literal>1555</literal>.</para>  
      365 </callout>  
      366 <callout arearefs="re.nm.4.2">  
      367 <para>这个模式匹配字符串的开始,接着是四个可选的<literal>M</literal> 字符的两个,接着匹配 <literal>D?C{0,3}</literal> ,此处为一个字符<literal>D</literal>和三个可选 <literal>C</literal>字符中的一个,接着匹配<literal>L?X{0,3}</literal>,此处为一个<literal>L</literal>字符和三个可选<literal>X</literal>字符中的一个,接着匹配<literal>V?I{0,3}</literal>,此处为一个字符<literal>V</literal>和三个可选<literal>I</literal>字符中的一个,接着匹配字符串的结尾。<literal>MMDCLXVI</literal> 表示罗马数字<literal>2666</literal>.</para>  
      368 </callout>  
      369 <callout arearefs="re.nm.4.3">  
      370 <para>这个模式匹配字符串的开始,接着是四个可选的<literal>M</literal>字符的所有字符,接着匹配 <literal>D?C{0,3}</literal> ,此处为一个字符<literal>D</literal>和三个可选 <literal>C</literal>字符中所有字符,接着匹配<literal>L?X{0,3}</literal>,此处为一个<literal>L</literal>字符和三个可选<literal>X</literal>字符中所有字符,接着匹配<literal>V?I{0,3}</literal>,此处为一个字符<literal>V</literal>和三个可选<literal>I</literal>字符中所有字符,接着匹配字符串的结尾。<literal>MMMMDCCCLXXXVIII</literal> 表示罗马数字<literal>3888</literal>, 这个数字是不用扩展语法可以写出的最大的罗马数字。</para>  
      371 </callout>  
      372 <callout arearefs="re.nm.4.4">  
      373 <para>仔细看哪!(我象一个魔术师一样,<quote>看仔细喽,孩子们,我将要从我的帽子中拽出一只兔子来啦!</quote>)  这个模式匹配字符串的开始,接着匹配4个可选<literal>M</literal>字符的0个,接着匹配<literal>D?C{0,3}</literal>,此处,跳过可选字符<literal>D</literal>并匹配三个可选<literal>C</literal>字符的0个,接着匹配<literal>L?X{0,3}</literal>,此处,跳过可选字符<literal>L</literal>并匹配三个可选 <literal>X</literal>字符的0个,接着匹配<literal>V?I{0,3}</literal>,此处跳过可选字符 <literal>V</literal>并且匹配三个可选<literal>I</literal>字符的一个,最后匹配字符串的结尾。哇赛!</para>  
      374 </callout>  
      375 </calloutlist>  
      376 </example>  
      377 <note>  
      378 <para>本章译者注:这个例子在正则表达式的匹配上没有问题,但是对于罗马数字的表示办法本身似乎有点问题,代表千位数的字符<literal>M</literal>,根据规定最多只能重复3次,但是在这个例子中重复了4次,但是这个罗马数字最后又表示3888,此处矛盾。不过,我们是为了搞清楚正则表达式的用法,罗马数字的表示法不是重点,因此从这个角度,这个例子没有问题。因此,在翻译的过程中保持了原文,大家在理解的时候需要注意一下这里。</para>  
      379 </note>  
      380 <para>如果你在第一遍就跟上并理解了所讲的这些,那么你做的比我还要好。现在,你可以尝试着理解别人大规模程序里关键函数中的正则表达式了。或者想象着几个月后回头理解你自己的正则表达式。我曾经做过这样的事情,但是它并不是那么好看。</para>  
      381 <para>在下一节里,你将会研究另外一种正则表达式语法,它可以使你的表达式具有更好的可维持性。</para>  
      382 </section>  
      383 </section>  
      384 <section id="re.verbose">  
      385 <?dbhtml filename="regular_expressions/verbose.html"?>  
      386 <title>松散正则表达式</title>  
      387 <abstract>  
      388 <title/>  
      389 <para>迄今为止,你只是处理过被我称之为<quote>紧凑</quote>类型的正则表达式。正如你曾看到的,它们难以阅读,即使你清楚正则表达式的含义,你也不能保证六个月以后你还能理解它。你真正所需的就是利用内联文档(inline documentation)。</para>  
      390 </abstract>  
      391 <para>&python; 允许用户利用所谓的 <emphasis>松散正则表达式</emphasis>来完成这个任务。一个松散正则表达式和一个紧凑正则表达式主要区别表现在两个方面:</para>  
      392 <itemizedlist>  
      393 <listitem><para>忽略空白符。空格符,制表符,回车符不匹配它们自身,他们根本不参与匹配。(如果你想在松散正则表达式中匹配一个空格符,你必须在它前面添加一个反斜线符号对他进行转义)</para></listitem>  
      394 <listitem><para>忽略注释。在松散正则表达式中的注释和在普通Python代码中的一样:开始于一个<literal>#</literal>符号,结束于行尾。这种情况下,采用在一个多行字符串中注释,而不是在源代码中注释,他们以相同的方式工作。</para></listitem>  
      395 </itemizedlist>  
      396 <para>用一个例子可以解释的更清楚。让我们重新来看前面的紧凑正则表达式,利用松散正则表达式重新表达。下面的例子显示实现方法。</para>  
      397 <example>  
      398 <title>带有内联注释(Inline Comments)的正则表达式</title>  
      399 <screen>  
      400 &prompt;<userinput>pattern = """  
      401     ^                   # beginning of string  
      402     M{0,4}              # thousands - 0 to 4 M's  
      403     (CM|CD|D?C{0,3})    # hundreds - 900 (CM), 400 (CD), 0-300 (0 to 3 C's),  
      404                         #            or 500-800 (D, followed by 0 to 3 C's)  
      405     (XC|XL|L?X{0,3})    # tens - 90 (XC), 40 (XL), 0-30 (0 to 3 X's),  
      406                         #        or 50-80 (L, followed by 0 to 3 X's)  
      407     (IX|IV|V?I{0,3})    # ones - 9 (IX), 4 (IV), 0-3 (0 to 3 I's),  
      408                         #        or 5-8 (V, followed by 0 to 3 I's)  
      409     $                   # end of string  
      410     """</userinput>  
      411 &prompt;<userinput>re.search(pattern, 'M', re.VERBOSE)</userinput>                <co id="re.verbose.1.1"/>  
      412 <computeroutput>&lt;_sre.SRE_Match object at 0x008EEB48></computeroutput>  
      413 &prompt;<userinput>re.search(pattern, 'MCMLXXXIX', re.VERBOSE)</userinput>        <co id="re.verbose.1.2"/>  
      414 <computeroutput>&lt;_sre.SRE_Match object at 0x008EEB48></computeroutput>  
      415 &prompt;<userinput>re.search(pattern, 'MMMMDCCCLXXXVIII', re.VERBOSE)</userinput> <co id="re.verbose.1.3"/>  
      416 <computeroutput>&lt;_sre.SRE_Match object at 0x008EEB48></computeroutput>  
      417 &prompt;<userinput>re.search(pattern, 'M')</userinput>                            <co id="re.verbose.1.4"/>  
      418 </screen>  
      419 <calloutlist>  
      420 <callout arearefs="re.verbose.1.1">  
      421 <para>当使用松散正则表达式是,请记住最重要的一件事情就是:必须传递另外一个参数<literal>re.VERBOSE</literal>,该参数是定义在&re; 模块中的一个常量,标志着待匹配的正则表达式是一个松散正则表达式。正如你看到的,这个模式中,有很多空格(所有的空格都被忽略),和几个注释(所有的注释也被忽略)。一旦你忽略所有的空格和注释,就和<link linkend="re.nm">前面章节</link>里的正则表达式完全相同,但是它具有更好的可读性。</para>  
      422 </callout>  
      423 <callout arearefs="re.verbose.1.2">  
      424 <para>这个模式匹配字符串的开始,接着匹配四个可选<literal>M</literal>字符中的一个,接着匹配<literal>CM</literal>, 接着是字符<literal>L</literal>和三个可选<literal>X</literal>字符的所有字符,接着是<literal>IX</literal>,然后是字符串的结尾ie。</para>  
      425 </callout>  
      426 <callout arearefs="re.verbose.1.3">  
      427 <para>这个模式匹配字符串的开始,接着是四个可选的<literal>M</literal>字符的所有字符,接着匹配 <literal>D?C{0,3}</literal> ,此处为一个字符<literal>D</literal>和三个可选 <literal>C</literal>字符中所有字符,接着匹配<literal>L?X{0,3}</literal>,此处为一个<literal>L</literal>字符和三个可选<literal>X</literal>字符中所有字符,接着匹配<literal>V?I{0,3}</literal>,此处为一个字符<literal>V</literal>和三个可选<literal>I</literal>字符中所有字符,接着匹配字符串的结尾。</para>  
      428 </callout>  
      429 <callout arearefs="re.verbose.1.4">  
      430 <para>这个没有匹配。为什么呢?因为没有<literal>re.VERBOSE</literal>标记,所以<function>re.search</function>函数把模式作为一个紧凑正则表达式进行匹配。&python; 不能自动检测一个正则表达式是为松散类型还是紧凑类型。&python; 默认每一个正则表达式都是紧凑类型的,除非你显式的标明一个正则表达式为松散类型。</para>  
      431 </callout>  
      432 </calloutlist>  
      433 </example>  
      434 </section>  
      435 <section id="re.phone">  
      436 <?dbhtml filename="regular_expressions/phone_numbers.html"?>  
      437 <title>个案研究: 解析电话号码</title>  
      438 <abstract>  
      439 <title/>  
      440 <para>迄今为止,你主要是匹配整个模式,不论是匹配上,还是没有匹配上。但是正则表达式还有比这更为强大的功能。当一个模式<emphasis>确实</emphasis>匹配上时,你可以获取模式中特定的片断,你可以发现具体匹配的位置。</para>  
      441 </abstract>  
      442 <para>这个例子来源于我遇到的另一个现实世界的问题,也是在以前的工作中遇到的。问题是:解析一个美国电话号码。客户要能(在一个单一的区域中)输入任何数字,然后存储区号,干线号,电话号和一个可选的独立的分机号到公司数据库里。为此,我通过网络找了很多正则表达式的例子,但是没有一个能够完全满足我的要求。</para>  
      443 <para>这里列举了我必须能够接受的电话号码:</para>  
      444 <itemizedlist>  
      445 <listitem><para><literal>800-555-1212</literal></para></listitem>  
      446 <listitem><para><literal>800 555 1212</literal></para></listitem>  
      447 <listitem><para><literal>800.555.1212</literal></para></listitem>  
      448 <listitem><para><literal>(800) 555-1212</literal></para></listitem>  
      449 <listitem><para><literal>1-800-555-1212</literal></para></listitem>  
      450 <listitem><para><literal>800-555-1212-1234</literal></para></listitem>  
      451 <listitem><para><literal>800-555-1212x1234</literal></para></listitem>  
      452 <listitem><para><literal>800-555-1212 ext. 1234</literal></para></listitem>  
      453 <listitem><para><literal>work 1-(800) 555.1212 #1234</literal></para></listitem>  
      454 </itemizedlist>  
      455 <para>格式可真够多的!我需要知道区号是<literal>800</literal>,干线号是<literal>555</literal>,电话号的其他数字为<literal>1212</literal>。对于那些有分机好的,我需要知道分机号我为 <literal>1234</literal>.</para>  
      456 <para>让我们完成电话号码解析这个工作,这个例子展示第一步。</para>  
      457 <example id="re.phone.example">  
      458 <title>发现数字</title>  
      459 <screen>  
      460 &prompt;<userinput>phonePattern = re.compile(r'^(\d{3})-(\d{3})-(\d{4})$')</userinput> <co id="re.phone.1.1"/>  
      461 &prompt;<userinput>phonePattern.search('800-555-1212').groups()</userinput>            <co id="re.phone.1.2"/>  
      462 <computeroutput>('800', '555', '1212')</computeroutput>  
      463 &prompt;<userinput>phonePattern.search('800-555-1212-1234')</userinput>                <co id="re.phone.1.3"/>  
      464 &prompt;  
      465 </screen>  
      466 <calloutlist>  
      467 <callout arearefs="re.phone.1.1">  
      468 <para>通常是从左到右阅读正则表达式,这个正则表达式匹配字符串的开始,接着匹配<literal>(\d{3})</literal>。<literal>\d{3}</literal>是什么呢?好吧,<literal>{3}</literal> 的含义是<quote>精确匹配三个数字</quote>;是曾在前面见到过的<link linkend="re.nm"><literal>{n,m} 语法</literal></link>的一种变形。<literal>\d</literal> 的含义是 <quote>任何一个数字</quote> (<literal>0</literal> 到 <literal>9</literal>)。把它们放大括号中意味着要<quote>精确匹配三个数字位,<emphasis>接着把他们作为一个组保存下来,以便后面的调用</emphasis></quote>。接着匹配一个连字符,接着是另外一个精确匹配三个数字位的组,接着另外一个连字符,接着另外一个精确匹配四个数字为的组, 接着匹配字符串的结尾。</para>  
      469 </callout>  
      470 <callout arearefs="re.phone.1.2">  
      471 <para>为了访问正则表达式解析过程中记忆下来的多个组,我们使用 <function>search</function> 函数返回对象的<function>groups()</function>函数。这个函数将返回一个元组,元组中的元素就是正则表达式中定义的组。在这个例子中,定义了三个组,第一个组有三个数字位,第二个组有三个数字位,第三个组有四个数字位。</para>  
      472 </callout>  
      473 <callout arearefs="re.phone.1.3">  
      474 <para>这个正则表达式不是最终的答案,因为它不能处理在电话号码结尾有分机号的情况,为此,我们需要扩展这个正则表达式。</para>  
      475 </callout>  
      476 </calloutlist>  
      477 </example>  
      478 <example>  
      479 <title>发现分机号</title>  
      480 <screen>  
      481 &prompt;<userinput>phonePattern = re.compile(r'^(\d{3})-(\d{3})-(\d{4})-(\d+)$')</userinput> <co id="re.phone.2.1"/>  
      482 &prompt;<userinput>phonePattern.search('800-555-1212-1234').groups()</userinput>             <co id="re.phone.2.2"/>  
      483 <computeroutput>('800', '555', '1212', '1234')</computeroutput>  
      484 &prompt;<userinput>phonePattern.search('800 555 1212 1234')</userinput>                      <co id="re.phone.2.3"/>  
      485 &prompt;  
      486 &prompt;<userinput>phonePattern.search('800-555-1212')</userinput>                           <co id="re.phone.2.4"/>  
      487 &prompt;  
      488 </screen>  
      489 <calloutlist>  
      490 <callout arearefs="re.phone.2.1">  
      491 <para>这个正则表达式和上一个几乎相同,正像前面的那样,匹配字符串的开始,接着匹配一个有三个数字位的组并记忆下来,接着是一个连字符,接着是一个有三个数字位的组并记忆下来,接着是一个连字符,接着是一个有四个数字位的组并记忆下来。不同的地方是你接着又匹配了另一个连字符,然后是一个有一个或者多个数字位的组并记忆下来,最后是字符串的结尾。</para>  
      492 </callout>  
      493 <callout arearefs="re.phone.2.2">  
      494 <para>函数<function>groups()</function>现在返回一个有四个元素的元组,由于正则表达式中定义了四个记忆的组。</para>  
      495 </callout>  
      496 <callout arearefs="re.phone.2.3">  
      497 <para>不幸的是,这个正则表达式也不是最终的答案,因为它假设电话号码的不同部分是由连字符分割的。如果一个电话号码是由空格符、逗号或者点号分割呢?你需要一个更一般的解决方案来匹配几种不同的分割类型。</para>  
      498 </callout>  
      499 <callout arearefs="re.phone.2.4">  
      500 <para>啊呀!这个正则表达式不仅不能解决你想要的任何问题,反而性能更弱了,因为现在你甚至不能解析一个没有分机号的电话号码了。这根本不是你想要的,如果有分机号,你要知道分机号是什么,如果没有分机号,你仍然想要知道主电话号码的其他部分是什么。</para>  
      501 </callout>  
      502 </calloutlist>  
      503 </example>  
      504 <para>下一个例子展示正则表达式处理一个电话号码内部,采用不同分割符的情况。</para>  
      505 <example>  
      506 <title>处理不同分割符</title>  
      507 <screen>  
      508 &prompt;<userinput>phonePattern = re.compile(r'^(\d{3})\D+(\d{3})\D+(\d{4})\D+(\d+)$')</userinput> <co id="re.phone.3.1"/>  
      509 &prompt;<userinput>phonePattern.search('800 555 1212 1234').groups()</userinput>                   <co id="re.phone.3.2"/>  
      510 <computeroutput>('800', '555', '1212', '1234')</computeroutput>  
      511 &prompt;<userinput>phonePattern.search('800-555-1212-1234').groups()</userinput>                   <co id="re.phone.3.3"/>  
      512 <computeroutput>('800', '555', '1212', '1234')</computeroutput>  
      513 &prompt;<userinput>phonePattern.search('80055512121234')</userinput>                               <co id="re.phone.3.4"/>  
      514 &prompt;  
      515 &prompt;<userinput>phonePattern.search('800-555-1212')</userinput>                                 <co id="re.phone.3.5"/>  
      516 &prompt;  
      517 </screen>  
      518 <calloutlist>  
      519 <callout arearefs="re.phone.3.1">  
      520 <para>当心啦!你首先匹配字符串的开始,接着是一个三个数字位的组,接着是 <literal>\D+</literal>,这是个什么东西?好吧,<literal>\D</literal>匹配任意字符,<emphasis>除了</emphasis>数字位,<literal>+</literal> 表示<quote>1个或者多个</quote>,  因此<literal>\D+</literal> 匹配一个或者多个不是数字位的字符。这就是你替换连字符为了匹配不同分隔符所用的方法。</para>  
      521 </callout>  
      522 <callout arearefs="re.phone.3.2">  
      523 <para>使用<literal>\D+</literal> 代替<literal>-</literal>意味着现在你可以匹配中间是空格符分割的电话号码了。</para>  
      524 </callout>  
      525 <callout arearefs="re.phone.3.3">  
      526 <para>当然,用连字符分割的电话号码也能够被识别。</para>  
      527 </callout>  
      528 <callout arearefs="re.phone.3.4">  
      529 <para>不幸的是,这个正则表达式仍然不是最终答案,因为他假设电话号码一定有分隔符。如果电话号码中间没有空格符或者连字符的情况会怎样哪?</para>  
      530 </callout>  
      531 <callout arearefs="re.phone.3.4">  
      532 <para>我的天!这个正则表达式也没有达到我们对于分机号识别的要求。现在你共有两个问题,但是你可以利用相同的技术来解决他们。</para>  
      533 </callout>  
      534 </calloutlist>  
      535 </example>  
      536 <para>下一个例子展示正则表达式处理<emphasis>没有</emphasis>分隔符的电话号码的情况。</para>  
      537 <example>  
      538 <title>处理没有分隔符的数字</title>  
      539 <screen>  
      540 &prompt;<userinput>phonePattern = re.compile(r'^(\d{3})\D*(\d{3})\D*(\d{4})\D*(\d*)$')</userinput> <co id="re.phone.4.1"/>  
      541 &prompt;<userinput>phonePattern.search('80055512121234').groups()</userinput>                      <co id="re.phone.4.2"/>  
      542 <computeroutput>('800', '555', '1212', '1234')</computeroutput>  
      543 &prompt;<userinput>phonePattern.search('800.555.1212 x1234').groups()</userinput>                  <co id="re.phone.4.3"/>  
      544 <computeroutput>('800', '555', '1212', '1234')</computeroutput>  
      545 &prompt;<userinput>phonePattern.search('800-555-1212').groups()</userinput>                        <co id="re.phone.4.4"/>  
      546 <computeroutput>('800', '555', '1212', '')</computeroutput>  
      547 &prompt;<userinput>phonePattern.search('(800)5551212 x1234')</userinput>                           <co id="re.phone.4.5"/>  
      548 &prompt;  
      549 </screen>  
      550 <calloutlist>  
      551 <callout arearefs="re.phone.4.1">  
      552 <para>和上一步相比,你所做的唯一变化就是把所有的<literal>+</literal> 变成 <literal>*</literal>。在电话号码的不同部分之间不再匹配 <literal>\D+</literal> ,而是匹配<literal>\D*</literal>了。还记得 <literal>+</literal>的含义是<quote>1或者多个</quote>吗? 好的,<literal>*</literal>的含义是<quote>0或者多个</quote>。因此,现在你应该能够解析没有分割符的电话号码了。</para>  
      553 </callout>  
      554 <callout arearefs="re.phone.4.2">  
      555 <para>你瞧,它真的可以胜任。为什么?首先匹配字符串的开始,接着是一个有三个数字位 (<literal>800</literal>)的组,接着是0个非数字字符,接着是一个有三个数字位 (<literal>555</literal>)的组,接着是0个非数字字符,接着是一个有四个数字位 (<literal>1212</literal>)的组,接着是0个非数字字符,接着是一个有任意数字位 (<literal>1234</literal>)的组,最后是字符串的结尾。</para>  
      556 </callout>  
      557 <callout arearefs="re.phone.4.3">  
      558 <para>对于其他的变化也能够匹配:比如点号分割符,在分机号前面既有空格符又有 <literal>x</literal> 符号的情况也能够匹配。</para>  
      559 </callout>  
      560 <callout arearefs="re.phone.4.4">  
      561 <para>最后,你已经解决了长期存在的一个问题:现在分机号是可选的了。如果没有发现分机号,<function>groups()</function> 函数仍然返回一个有四个元素的元组,但是第四个元素只是一个空字符串。</para>  
      562 </callout>  
      563 <callout arearefs="re.phone.4.5">  
      564 <para>我不喜欢做一个坏消息的传递人,此时你还没有完全结束这个问题。还有什么问题呢?当在区号前面还有一个额外的字符时,而正则表达式假设区号是一个字符串的开始,因此不能匹配。这个不是问题,你可以利用相同的技术<quote>0或者多个非数字字符</quote>来跳过区号前面的字符。</para>  
      565 </callout>  
      566 </calloutlist>  
      567 </example>  
      568 <para>下一个例子展示如何解决电话号码前面有其他字符的情况。</para>  
      569 <example>  
      570 <title>处理开始字符</title>  
      571 <screen>  
      572 &prompt;<userinput>phonePattern = re.compile(r'^\D*(\d{3})\D*(\d{3})\D*(\d{4})\D*(\d*)$')</userinput> <co id="re.phone.5.1"/>  
      573 &prompt;<userinput>phonePattern.search('(800)5551212 ext. 1234').groups()</userinput>                 <co id="re.phone.5.2"/>  
      574 <computeroutput>('800', '555', '1212', '1234')</computeroutput>  
      575 &prompt;<userinput>phonePattern.search('800-555-1212').groups()</userinput>                           <co id="re.phone.5.3"/>  
      576 <computeroutput>('800', '555', '1212', '')</computeroutput>  
      577 &prompt;<userinput>phonePattern.search('work 1-(800) 555.1212 #1234')</userinput>                     <co id="re.phone.5.4"/>  
      578 &prompt;  
      579 </screen>  
      580 <calloutlist>  
      581 <callout arearefs="re.phone.5.1">  
      582 <para>这个正则表达式和前面的几乎相同,除了在第一个记忆组(区号)前面匹配<literal>\D*</literal>,0或者多个非数字字符。注意,此处你没有记忆这些非数字字符(他们没有被括号括起来)。如果你发现他们,只是跳过他们,接着只要匹配上就开始记忆区号。</para>  
      583 </callout>  
      584 <callout arearefs="re.phone.5.2">  
      585 <para>你可以成功的解析电话号码,即使在区号前面有一个左括号。(在区号后面的右括号也已经被处理,它被看成非数字字符分隔符,由第一个记忆组后面的 <literal>\D*</literal>匹配。)</para>  
      586 </callout>  
      587 <callout arearefs="re.phone.5.3">  
      588 <para>进行仔细的检查,保证你没有破坏前面能够匹配的任何情况。由于首字符是完全可选的,这个模式匹配字符串的开始,接着是0个非数字字符,接着是一个有三个数字字符的记忆组(<literal>800</literal>),接着是1个非数字字符(连字符),接着是一个有三个数字字符的记忆组(<literal>555</literal>),接着是1个非数字字符(连字符),接着是一个有四个数字字符的记忆组(<literal>1212</literal>),接着是0个非数字字符,接着是一个有0个数字位的记忆组,最后是字符串的结尾。</para>  
      589 </callout>  
      590 <callout arearefs="re.phone.5.4">  
      591 <para>此处是正则表达式让我产生了找一个硬东西挖出自己的眼睛的冲动。为什么这个电话号码没有匹配上?因为在它的区号前面有一个 <literal>1</literal>,但是你认为在区号前面的所有字符都是非数字字符(<literal>\D*</literal>)。  Aargh.</para>  
      592 </callout>  
      593 </calloutlist>  
      594 </example>  
      595 <para>让我们往回看一下。迄今为止,正则表达式总是从一个字符串的开始匹配。但是现在你看到了,有很多不确定的情况需要你忽略。与其尽力全部匹配他们,还不如全部跳过他们,让我们采用一个不同的方法:根本不显式的匹配字符串的开始。下面的这个例子展示这个方法。</para>  
      596 <example>  
      597 <title>电话号码,无论何时我都要找到它</title>  
      598 <screen>  
      599 &prompt;<userinput>phonePattern = re.compile(r'(\d{3})\D*(\d{3})\D*(\d{4})\D*(\d*)$')</userinput> <co id="re.phone.6.1"/>  
      600 &prompt;<userinput>phonePattern.search('work 1-(800) 555.1212 #1234').groups()</userinput>        <co id="re.phone.6.2"/>  
      601 <computeroutput>('800', '555', '1212', '1234')</computeroutput>  
      602 &prompt;<userinput>phonePattern.search('800-555-1212')</userinput>                                <co id="re.phone.6.3"/>  
      603 <computeroutput>('800', '555', '1212', '')</computeroutput>  
      604 &prompt;<userinput>phonePattern.search('80055512121234')</userinput>                              <co id="re.phone.6.4"/>  
      605 <computeroutput>('800', '555', '1212', '1234')</computeroutput>  
      606 </screen>  
      607 <calloutlist>  
      608 <callout arearefs="re.phone.6.1">  
      609 <para>注意,在这个正则表达式的开始少了一个<literal>^</literal> 字符。你不再匹配字符串的开始了,也就是说,你需要用你的正则表达式匹配整个输入字符串,除此之外没有别的意思了。正则表达式引擎将要努力计算出开始匹配输入字符串的位置,并且从这个位置开始匹配。</para>  
      610 </callout>  
      611 <callout arearefs="re.phone.6.2">  
      612 <para>现在你可以成功解析一个电话号码了,不论这个电话号码的首字符是数字还是不是数字,还是在电话号码不同部分之间加上任意数目的任意类型的分隔符。</para>  
      613 </callout>  
      614 <callout arearefs="re.phone.6.3">  
      615 <para>仔细检查,这个正则表达式仍然工作的很好。</para>  
      616 </callout>  
      617 <callout arearefs="re.phone.6.4">  
      618 <para>还是能够工作。</para>  
      619 </callout>  
      620 </calloutlist>  
      621 </example>  
      622 <para>看看一个正则表达式能够失控的多快?回头看看前面的例子,你还能区别他们么?</para>  
      623 <para>当你还能够理解这个最终答案的时候(这个正则表达式就是最终答案,即使你发现一种它不能处理的情况,我也真的不想知道它了),在你忘记为什么你这么选择之前,让我们把它写成松散正则表达式的形式。</para>  
      624 <example>  
      625 <title>解析电话号码(最终版本)</title>  
      626 <screen>  
      627 &prompt;<userinput>phonePattern = re.compile(r'''  
      628                 # don't match beginning of string, number can start anywhere  
      629     (\d{3})     # area code is 3 digits (e.g. '800')  
      630     \D*         # optional separator is any number of non-digits  
      631     (\d{3})     # trunk is 3 digits (e.g. '555')  
      632     \D*         # optional separator  
      633     (\d{4})     # rest of number is 4 digits (e.g. '1212')  
      634     \D*         # optional separator  
      635     (\d*)       # extension is optional and can be any number of digits  
      636     $           # end of string  
      637     ''', re.VERBOSE)</userinput>  
      638 &prompt;<userinput>phonePattern.search('work 1-(800) 555.1212 #1234').groups()</userinput>        <co id="re.phone.7.1"/>  
      639 <computeroutput>('800', '555', '1212', '1234')</computeroutput>  
      640 &prompt;<userinput>phonePattern.search('800-555-1212')</userinput>                                <co id="re.phone.7.2"/>  
      641 <computeroutput>('800', '555', '1212', '')</computeroutput>  
      642 </screen>  
      643 <calloutlist>  
      644 <callout arearefs="re.phone.7.1">  
      645 <para>除了被分成多行,这个正则表达式和最后一步的那个完全相同,因此他能够解析相同的输入一点也不奇怪。</para>  
      646 </callout>  
      647 <callout arearefs="re.phone.7.2">  
      648 <para>进行最后的仔细检查。很好,仍然工作。你终于完成了这件任务。</para>  
      649 </callout>  
      650 </calloutlist>  
      651 </example>  
      652 <itemizedlist role="furtherreading">  
      653 <title>关于正则表达式的进一步阅读</title>  
      654 <listitem><para>&rehowto; 讲解正则表达式和如何在&python;中使用正则表达式。</para></listitem>  
      655 <listitem><para>&pythonlibraryreference; 概述了&remodule_link;.</para></listitem>  
      656 </itemizedlist>  
      657 </section>  
      658 <section id="re.summary">  
      659 <?dbhtml filename="regular_expressions/summary.html"?>  
      660 <title>总结</title>  
      661 <abstract>  
      662 <title/>  
      663 <para>这只是正则表达式能够完成工作的很少一部分。换句话说,即使你现在备受打击,相信我,你也不是什么也没见过了。</para>  
      664 </abstract>  
      665 <para>现在,你应该熟悉下列技巧:</para>  
      666 <itemizedlist>  
      667 <listitem><para><literal>^</literal> 匹配字符串的开始。</para></listitem>  
      668 <listitem><para><literal>$</literal> 匹配字符串的结尾。</para></listitem>  
      669 <listitem><para><literal>\b</literal> 匹配一个单词的边界。</para></listitem>  
      670 <listitem><para><literal>\d</literal> 匹配任意数字。</para></listitem>  
      671 <listitem><para><literal>\D</literal> 匹配任意非数字字符。</para></listitem>  
      672 <listitem><para><literal>x?</literal> 匹配一个可选的<literal>x</literal>字符(换句话说,它匹配1次或者0次<literal>x</literal> 字符)。</para></listitem>  
      673 <listitem><para><literal>x*</literal> 匹配0次或者多次<literal>x</literal>字符。</para></listitem>  
      674 <listitem><para><literal>x+</literal>匹配1次或者多次<literal>x</literal>字符。</para></listitem>  
      675 <listitem><para><literal>x{n,m}</literal> 匹配<literal>x</literal>字符,至少<literal>n</literal>次,至多<literal>m</literal>次。</para></listitem>  
      676 <listitem><para><literal>(a|b|c)</literal>要么匹配<literal>a</literal>,要么匹配<literal>b</literal>,要么匹配<literal>c</literal>。</para></listitem>  
      677 <listitem><para><literal>(x)</literal> 一般情况下表示一个<emphasis>记忆组(remembered group)</emphasis>. 你可以利用<function>re.search</function>函数返回对象的<function>groups()</function>函数获取它的值。</para></listitem>  
      678 </itemizedlist>  
      679 <para>正则表达式非常强大,但是它并不能为每一个问题提供正确的解决方案。你应该学习足够多的知识,以辨别什么时候他们是合适的,什么时候他们会解决你的问题,什么时候他们产生的问题比要解决的问题还要多。</para>  
      680 <blockquote><attribution>Jamie Zawinski, <ulink url="http://groups.google.com/groups?selm=33F0C496.370D7C45%40netscape.com">in comp.emacs.xemacs</ulink></attribution><para>一些人,当遇到一个问题时,想<quote>我知道,我将使用正则表达式。</quote> 现在他有两个问题了。</para></blockquote>  
      681 </section>  
      682 </chapter>  
      683  
      684 <!--  
      685 * Intro/Diving in  
      686   * This chapter is for Python programmers who have read the first three chapters of this book, but have never used regular expressions.  If you have used regular expressions in some other language (such as Perl), this chapter is not for you; go read some other document [link, find "Python RE for Perl programmers", or write it] and get on with your life.  
      687   * Jamie Zawinski (comp.lang.emacs): "Some people, when confronted with a problem, think 'I know, I'll use regular expressions.'  Now they have two problems."  
      688 * Case study: normalizing addresses  
      689   * Use dialect.re section  
      690   * Patterns:  
      691     $  Match at end  
      692     \b Match at word boundary  
      693     ^  Match at beginning (add mention)  
      694 - Case study: validating Roman numerals  
      695   * Roman numeral pattern (storyboard)  
      696   * Storyboard:  
      697     steal most of roman.stage5  
      698     '^M?M?M?(CM|CD|D?C?C?C?)(XC|XL|L?X?X?X?)(IX|IV|V?I?I?I?)$'  
      699     '^M{0,3}(CM|CD|D?C{0,3})(XC|XL|L?X{0,3})(IX|IV|V?I{0,3})$')  
      700  
      701   * Patterns:  
      702     .   Match any character  
      703     ?   Match previous 0 or 1 times (optional)  
      704     |   or  
      705     {n,m} Match previous anywhere from n to m times  
      706  
      707   * Verbose regular expressions  
      708  
      709 - Case study: extracting parts of an American phone number  
      710   - Use phone number validation example  
      711   - "grouping"  
      712   - re.search(r'\D*(\d{3})\D*(\d{3})\D*(\d{4})\D*(\d*)$', s).groups()  
      713   - Patterns:  
      714     () define group which can be accessed later (really saw it in last section, but didn't fully utilize it)  
      715     \d Match any digit  
      716     \D Match any non-digit  
      717     *  Match previous as many times as possible, 0 or more times  
      718     +  Match previous as many times as possible, at least once  
      719     *? Match previous as few times as possible, 0 or more times  
      720     +? Match previous as few times as possible, at least once  
      721 -->  
      722