home *** CD-ROM | disk | FTP | other *** search
/ The Datafile PD-CD 5 / DATAFILE_PDCD5.iso / utilities / p / python / pyhtmldoc / r / regular_ex < prev    next >
Text File  |  1996-11-14  |  7KB  |  129 lines

  1. <TITLE>Regular Expressions -- Python library reference</TITLE>
  2. Next: <A HREF="../m/module_contents" TYPE="Next">Module Contents</A>  
  3. Prev: <A HREF="../r/regex" TYPE="Prev">regex</A>  
  4. Up: <A HREF="../r/regex" TYPE="Up">regex</A>  
  5. Top: <A HREF="../t/top" TYPE="Top">Top</A>  
  6. <H2>4.2.1. Regular Expressions</H2>
  7. A regular expression (or RE) specifies a set of strings that matches
  8. it; the functions in this module let you check if a particular string
  9. matches a given regular expression (or if a given regular expression
  10. matches a particular string, which comes down to the same thing).
  11. <P>
  12. Regular expressions can be concatenated to form new regular
  13. expressions; if <I>A</I> and <I>B</I> are both regular expressions,
  14. then <I>AB</I> is also an regular expression.  If a string <I>p</I>
  15. matches A and another string <I>q</I> matches B, the string <I>pq</I>
  16. will match AB.  Thus, complex expressions can easily be constructed
  17. from simpler ones like the primitives described here.  For details of
  18. the theory and implementation of regular expressions, consult almost
  19. any textbook about compiler construction.
  20. <P>
  21. A brief explanation of the format of regular expressions follows.
  22. <P>
  23. Regular expressions can contain both special and ordinary characters.
  24. Ordinary characters, like '<CODE>A</CODE>', '<CODE>a</CODE>', or '<CODE>0</CODE>', are
  25. the simplest regular expressions; they simply match themselves.  You
  26. can concatenate ordinary characters, so '<CODE>last</CODE>' matches the
  27. characters 'last'.  (In the rest of this section, we'll write RE's in
  28. <CODE>this special font</CODE>, usually without quotes, and strings to be
  29. matched 'in single quotes'.)
  30. <P>
  31. Special characters either stand for classes of ordinary characters, or
  32. affect how the regular expressions around them are interpreted.
  33. <P>
  34. The special characters are:
  35. <UL>
  36. <LI>•<CODE>.</CODE>  (Dot.)  Matches any character except a newline.
  37. <LI>•<CODE>^</CODE>  (Caret.)  Matches the start of the string.
  38. <LI>•<CODE>$</CODE>  Matches the end of the string.  
  39. <CODE>foo</CODE> matches both 'foo' and 'foobar', while the regular
  40. expression '<CODE>foo$</CODE>' matches only 'foo'.
  41. <LI>•<CODE>*</CODE>  Causes the resulting RE to
  42. match 0 or more repetitions of the preceding RE.  <CODE>ab*</CODE> will
  43. match 'a', 'ab', or 'a' followed by any number of 'b's.
  44. <LI>•<CODE>+</CODE>  Causes the
  45. resulting RE to match 1 or more repetitions of the preceding RE.
  46. <CODE>ab+</CODE> will match 'a' followed by any non-zero number of 'b's; it
  47. will not match just 'a'.
  48. <LI>•<CODE>?</CODE>  Causes the resulting RE to
  49. match 0 or 1 repetitions of the preceding RE.  <CODE>ab?</CODE> will
  50. match either 'a' or 'ab'.
  51. <P>
  52. <LI>•<CODE>@e</CODE>  Either escapes special characters (permitting you to match
  53. characters like '*?+&$'), or signals a special sequence; special
  54. sequences are discussed below.  Remember that Python also uses the
  55. backslash as an escape sequence in string literals; if the escape
  56. sequence isn't recognized by Python's parser, the backslash and
  57. subsequent character are included in the resulting string.  However,
  58. if Python would recognize the resulting sequence, the backslash should
  59. be repeated twice.  
  60. <P>
  61. <LI>•<CODE>[]</CODE>  Used to indicate a set of characters.  Characters can
  62. be listed individually, or a range is indicated by giving two
  63. characters and separating them by a '-'.  Special characters are
  64. not active inside sets.  For example, <CODE>[akm$]</CODE>
  65. will match any of the characters 'a', 'k', 'm', or '$'; <CODE>[a-z]</CODE> will
  66. match any lowercase letter.  
  67. <P>
  68. If you want to include a <CODE>]</CODE> inside a
  69. set, it must be the first character of the set; to include a <CODE>-</CODE>,
  70. place it as the first or last character. 
  71. <P>
  72. Characters <I>not</I> within a range can be matched by including a
  73. <CODE>^</CODE> as the first character of the set; <CODE>^</CODE> elsewhere will
  74. simply match the '<CODE>^</CODE>' character.  
  75. </UL>
  76. The special sequences consist of '<CODE>\</CODE>' and a character
  77. from the list below.  If the ordinary character is not on the list,
  78. then the resulting RE will match the second character.  For example,
  79. <CODE>\$</CODE> matches the character '$'.  Ones where the backslash
  80. should be doubled are indicated.
  81. <P>
  82. <UL>
  83. <LI>•<CODE>@e |</CODE>  <CODE>A\|B</CODE>, where A and B can be arbitrary REs,
  84. creates a regular expression that will match either A or B.  This can
  85. be used inside groups (see below) as well.
  86. <P>
  87. <LI>•<CODE>@e ( @e )</CODE>  Indicates the start and end of a group; the
  88. contents of a group can be matched later in the string with the
  89. <CODE>\[1-9]</CODE> special sequence, described next.
  90. @fulllineitems
  91. <LI>•<CODE>@e@e 1, ... @e@e 7, @e 8, @e 9</CODE>  Matches the contents of the group of the same
  92. number.  For example, <CODE>\(.+\) \\1</CODE> matches 'the the' or
  93. '55 55', but not 'the end' (note the space after the group).  This
  94. special sequence can only be used to match one of the first 9 groups;
  95. groups with higher numbers can be matched using the <CODE>\v</CODE>
  96. sequence.  (<CODE>\8</CODE> and <CODE>\9</CODE> don't need a double backslash
  97. because they are not octal digits.)}
  98. <P>
  99. <LI>•<CODE>@e@e b</CODE>  Matches the empty string, but only at the
  100. beginning or end of a word.  A word is defined as a sequence of
  101. alphanumeric characters, so the end of a word is indicated by
  102. whitespace or a non-alphanumeric character.
  103. <P>
  104. <LI>•<CODE>@e B</CODE>  Matches the empty string, but when it is <I>not</I> at the
  105. beginning or end of a word. 
  106. <P>
  107. <LI>•<CODE>@e v</CODE>  Must be followed by a two digit decimal number, and
  108. matches the contents of the group of the same number.  The group number must be between 1 and 99, inclusive.
  109. <P>
  110. <LI>•<CODE>@e w</CODE>  Matches any alphanumeric character; this is
  111. equivalent to the set <CODE>[a-zA-Z0-9]</CODE>.
  112. <P>
  113. <LI>•<CODE>@e W</CODE>  Matches any non-alphanumeric character; this is
  114. equivalent to the set <CODE>[^a-zA-Z0-9]</CODE>. 
  115. <LI>•<CODE>@e <</CODE>  Matches the empty string, but only at the beginning of a
  116. word.  A word is defined as a sequence of alphanumeric characters, so
  117. the end of a word is indicated by whitespace or a non-alphanumeric 
  118. character.
  119. <LI>•<CODE>@e ></CODE>  Matches the empty string, but only at the end of a
  120. word.
  121. <P>
  122. <LI>•<CODE>@e@e@e@e</CODE>  Matches a literal backslash.
  123. <P>
  124. <LI>•<CODE>@e `</CODE>  Like <CODE>^</CODE>, this only matches at the start of the
  125. string.
  126. <LI>•<CODE>@e@e '</CODE>  Like <CODE>$</CODE>, this only matches at the end of the
  127. string.
  128. </UL>
  129.