home *** CD-ROM | disk | FTP | other *** search
/ The Datafile PD-CD 5 / DATAFILE_PDCD5.iso / utilities / p / python / pyhtmldoc / m / module_con < prev    next >
Encoding:
Text File  |  1996-11-14  |  7.5 KB  |  152 lines

  1. <TITLE>Module Contents -- Python library reference</TITLE>
  2. Prev: <A HREF="../r/regular_expressions" TYPE="Prev">Regular Expressions</A>  
  3. Up: <A HREF="../r/regex" TYPE="Up">regex</A>  
  4. Top: <A HREF="../t/top" TYPE="Top">Top</A>  
  5. <H2>4.2.2. Module Contents</H2>
  6. The module defines these functions, and an exception:
  7. <P>
  8. <DL><DT><B>match</B> (<VAR>pattern</VAR>, <VAR>string</VAR>) -- function of module regex<DD>
  9. Return how many characters at the beginning of <VAR>string</VAR> match
  10. the regular expression <VAR>pattern</VAR>.  Return <CODE>-1</CODE> if the
  11. string does not match the pattern (this is different from a
  12. zero-length match!).
  13. </DL>
  14. <DL><DT><B>search</B> (<VAR>pattern</VAR>, <VAR>string</VAR>) -- function of module regex<DD>
  15. Return the first position in <VAR>string</VAR> that matches the regular
  16. expression <VAR>pattern</VAR>.  Return <CODE>-1</CODE> if no position in the string
  17. matches the pattern (this is different from a zero-length match
  18. anywhere!).
  19. </DL>
  20. <DL><DT><B>compile</B> (<VAR>pattern</VAR>[, <VAR>translate</VAR>]) -- function of module regex<DD>
  21. Compile a regular expression pattern into a regular expression
  22. object, which can be used for matching using its <CODE>match</CODE> and
  23. <CODE>search</CODE> methods, described below.  The optional argument
  24. <VAR>translate</VAR>, if present, must be a 256-character string
  25. indicating how characters (both of the pattern and of the strings to
  26. be matched) are translated before comparing them; the <CODE>i</CODE>-th
  27. element of the string gives the translation for the character with
  28. ASCII code <CODE>i</CODE>.  This can be used to implement
  29. case-insensitive matching; see the <CODE>casefold</CODE> data item below.
  30. <P>
  31. The sequence
  32. <P>
  33. <UL COMPACT><CODE>prog = regex.compile(pat)<P>
  34. result = prog.match(str)<P>
  35. </CODE></UL>
  36. is equivalent to
  37. <P>
  38. <UL COMPACT><CODE>result = regex.match(pat, str)<P>
  39. </CODE></UL>
  40. but the version using <CODE>compile()</CODE> is more efficient when multiple
  41. regular expressions are used concurrently in a single program.  (The
  42. compiled version of the last pattern passed to <CODE>regex.match()</CODE> or
  43. <CODE>regex.search()</CODE> is cached, so programs that use only a single
  44. regular expression at a time needn't worry about compiling regular
  45. expressions.)
  46. </DL>
  47. <DL><DT><B>set_syntax</B> (<VAR>flags</VAR>) -- function of module regex<DD>
  48. Set the syntax to be used by future calls to <CODE>compile</CODE>,
  49. <CODE>match</CODE> and <CODE>search</CODE>.  (Already compiled expression objects
  50. are not affected.)  The argument is an integer which is the OR of
  51. several flag bits.  The return value is the previous value of
  52. the syntax flags.  Names for the flags are defined in the standard
  53. module <CODE>regex_syntax</CODE>; read the file <FILE>regex_syntax.py</FILE> for
  54. more information.
  55. </DL>
  56. <DL><DT><B>symcomp</B> (<VAR>pattern</VAR>[, <VAR>translate</VAR>]) -- function of module regex<DD>
  57. This is like <CODE>compile</CODE>, but supports symbolic group names: if a
  58. parenthesis-enclosed group begins with a group name in angular
  59. brackets, e.g. <CODE>'\(<id>[a-z][a-z0-9]*\)'</CODE>, the group can
  60. be referenced by its name in arguments to the <CODE>group</CODE> method of
  61. the resulting compiled regular expression object, like this:
  62. <CODE>p.group('id')</CODE>.  Group names may contain alphanumeric characters
  63. and <CODE>'_'</CODE> only.
  64. </DL>
  65. <DL><DT><B>error</B> -- exception of module regex<DD>
  66. Exception raised when a string passed to one of the functions here
  67. is not a valid regular expression (e.g., unmatched parentheses) or
  68. when some other error occurs during compilation or matching.  (It is
  69. never an error if a string contains no match for a pattern.)
  70. </DL>
  71. <DL><DT><B>casefold</B> -- data of module regex<DD>
  72. A string suitable to pass as <VAR>translate</VAR> argument to
  73. <CODE>compile</CODE> to map all upper case characters to their lowercase
  74. equivalents.
  75. </DL>
  76.  Compiled regular expression objects support these methods:
  77. <P>
  78. <DL><DT><B>match</B> (<VAR>string</VAR>[, <VAR>pos</VAR>]) -- Method on regex<DD>
  79. Return how many characters at the beginning of <VAR>string</VAR> match
  80. the compiled regular expression.  Return <CODE>-1</CODE> if the string
  81. does not match the pattern (this is different from a zero-length
  82. match!).
  83. <P>
  84. The optional second parameter <VAR>pos</VAR> gives an index in the string
  85. where the search is to start; it defaults to <CODE>0</CODE>.  This is not
  86. completely equivalent to slicing the string; the <CODE>'^'</CODE> pattern
  87. character matches at the real begin of the string and at positions
  88. just after a newline, not necessarily at the index where the search
  89. is to start.
  90. </DL>
  91. <DL><DT><B>search</B> (<VAR>string</VAR>[, <VAR>pos</VAR>]) -- Method on regex<DD>
  92. Return the first position in <VAR>string</VAR> that matches the regular
  93. expression <CODE>pattern</CODE>.  Return <CODE>-1</CODE> if no position in the
  94. string matches the pattern (this is different from a zero-length
  95. match anywhere!).
  96. <P>
  97. The optional second parameter has the same meaning as for the
  98. <CODE>match</CODE> method.
  99. </DL>
  100. <DL><DT><B>group</B> (<VAR>index</VAR>, <VAR>index</VAR>, ...) -- Method on regex<DD>
  101. This method is only valid when the last call to the <CODE>match</CODE>
  102. or <CODE>search</CODE> method found a match.  It returns one or more
  103. groups of the match.  If there is a single <VAR>index</VAR> argument,
  104. the result is a single string; if there are multiple arguments, the
  105. result is a tuple with one item per argument.  If the <VAR>index</VAR> is
  106. zero, the corresponding return value is the entire matching string; if
  107. it is in the inclusive range [1..99], it is the string matching the
  108. the corresponding parenthesized group (using the default syntax,
  109. groups are parenthesized using <CODE>*(</CODE> and <CODE>*)</CODE>).  If no
  110. such group exists, the corresponding result is <CODE>None</CODE>.
  111. <P>
  112. If the regular expression was compiled by <CODE>symcomp</CODE> instead of
  113. <CODE>compile</CODE>, the <VAR>index</VAR> arguments may also be strings
  114. identifying groups by their group name.
  115. </DL>
  116.  Compiled regular expressions support these data attributes:
  117. <P>
  118. <DL><DT><B>regs</B> -- attribute of regex<DD>
  119. When the last call to the <CODE>match</CODE> or <CODE>search</CODE> method found a
  120. match, this is a tuple of pairs of indices corresponding to the
  121. beginning and end of all parenthesized groups in the pattern.  Indices
  122. are relative to the string argument passed to <CODE>match</CODE> or
  123. <CODE>search</CODE>.  The 0-th tuple gives the beginning and end or the
  124. whole pattern.  When the last match or search failed, this is
  125. <CODE>None</CODE>.
  126. </DL>
  127. <DL><DT><B>last</B> -- attribute of regex<DD>
  128. When the last call to the <CODE>match</CODE> or <CODE>search</CODE> method found a
  129. match, this is the string argument passed to that method.  When the
  130. last match or search failed, this is <CODE>None</CODE>.
  131. </DL>
  132. <DL><DT><B>translate</B> -- attribute of regex<DD>
  133. This is the value of the <VAR>translate</VAR> argument to
  134. <CODE>regex.compile</CODE> that created this regular expression object.  If
  135. the <VAR>translate</VAR> argument was omitted in the <CODE>regex.compile</CODE>
  136. call, this is <CODE>None</CODE>.
  137. </DL>
  138. <DL><DT><B>givenpat</B> -- attribute of regex<DD>
  139. The regular expression pattern as passed to <CODE>compile</CODE> or
  140. <CODE>symcomp</CODE>.
  141. </DL>
  142. <DL><DT><B>realpat</B> -- attribute of regex<DD>
  143. The regular expression after stripping the group names for regular
  144. expressions compiled with <CODE>symcomp</CODE>.  Same as <CODE>givenpat</CODE>
  145. otherwise.
  146. </DL>
  147. <DL><DT><B>groupindex</B> -- attribute of regex<DD>
  148. A dictionary giving the mapping from symbolic group names to numerical
  149. group indices for regular expressions compiled with <CODE>symcomp</CODE>.
  150. <CODE>None</CODE> otherwise.
  151. </DL>
  152.