| Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 1 | \section{Built-in Module \sectcode{regex}} | 
| Guido van Rossum | e47da0a | 1997-07-17 16:34:52 +0000 | [diff] [blame] | 2 | \label{module-regex} | 
| Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 3 |  | 
 | 4 | \bimodindex{regex} | 
 | 5 | This module provides regular expression matching operations similar to | 
| Guido van Rossum | 28f9a68 | 1997-12-09 19:45:47 +0000 | [diff] [blame] | 6 | those found in Emacs. | 
 | 7 |  | 
 | 8 | \strong{Obsolescence note:} | 
 | 9 | This module is obsolete as of Python version 1.5; it is still being | 
 | 10 | maintained because much existing code still uses it.  All new code in | 
 | 11 | need of regular expressions should use the new \code{re} module, which | 
 | 12 | supports the more powerful and regular Perl-style regular expressions. | 
 | 13 | Existing code should be converted.  The standard library module | 
 | 14 | \code{reconvert} helps in converting \code{regex} style regular | 
 | 15 | expressions to \code{re} style regular expressions.  (The interfaces | 
 | 16 | are different too, so the conversion cannot be fully automated.) | 
| Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 17 |  | 
| Guido van Rossum | 6240b0b | 1996-10-24 22:49:13 +0000 | [diff] [blame] | 18 | By default the patterns are Emacs-style regular expressions | 
 | 19 | (with one exception).  There is | 
| Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 20 | a way to change the syntax to match that of several well-known | 
| Guido van Rossum | fe4254e | 1995-08-11 00:31:57 +0000 | [diff] [blame] | 21 | \UNIX{} utilities.  The exception is that Emacs' \samp{\e s} | 
 | 22 | pattern is not supported, since the original implementation references | 
 | 23 | the Emacs syntax tables. | 
| Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 24 |  | 
 | 25 | This module is 8-bit clean: both patterns and strings may contain null | 
 | 26 | bytes and characters whose high bit is set. | 
 | 27 |  | 
| Guido van Rossum | 326c0bc | 1994-01-03 00:00:31 +0000 | [diff] [blame] | 28 | \strong{Please note:} There is a little-known fact about Python string | 
 | 29 | literals which means that you don't usually have to worry about | 
 | 30 | doubling backslashes, even though they are used to escape special | 
 | 31 | characters in string literals as well as in regular expressions.  This | 
 | 32 | is because Python doesn't remove backslashes from string literals if | 
 | 33 | they are followed by an unrecognized escape character. | 
 | 34 | \emph{However}, if you want to include a literal \dfn{backslash} in a | 
 | 35 | regular expression represented as a string literal, you have to | 
| Guido van Rossum | 1f8cee2 | 1997-03-14 04:10:13 +0000 | [diff] [blame] | 36 | \emph{quadruple} it or enclose it in a singleton character class. | 
 | 37 | E.g.\  to extract \LaTeX\ \samp{\e section\{{\rm | 
| Guido van Rossum | 326c0bc | 1994-01-03 00:00:31 +0000 | [diff] [blame] | 38 | \ldots}\}} headers from a document, you can use this pattern: | 
| Guido van Rossum | 1f8cee2 | 1997-03-14 04:10:13 +0000 | [diff] [blame] | 39 | \code{'[\e ] section\{\e (.*\e )\}'}.  \emph{Another exception:} | 
| Guido van Rossum | 1a53560 | 1996-06-26 19:43:22 +0000 | [diff] [blame] | 40 | the escape sequece \samp{\e b} is significant in string literals | 
 | 41 | (where it means the ASCII bell character) as well as in Emacs regular | 
 | 42 | expressions (where it stands for a word boundary), so in order to | 
 | 43 | search for a word boundary, you should use the pattern \code{'\e \e b'}. | 
 | 44 | Similarly, a backslash followed by a digit 0-7 should be doubled to | 
 | 45 | avoid interpretation as an octal escape. | 
 | 46 |  | 
 | 47 | \subsection{Regular Expressions} | 
 | 48 |  | 
 | 49 | A regular expression (or RE) specifies a set of strings that matches | 
 | 50 | it; the functions in this module let you check if a particular string | 
| Guido van Rossum | 6240b0b | 1996-10-24 22:49:13 +0000 | [diff] [blame] | 51 | matches a given regular expression (or if a given regular expression | 
 | 52 | matches a particular string, which comes down to the same thing). | 
| Guido van Rossum | 1a53560 | 1996-06-26 19:43:22 +0000 | [diff] [blame] | 53 |  | 
 | 54 | Regular expressions can be concatenated to form new regular | 
 | 55 | expressions; if \emph{A} and \emph{B} are both regular expressions, | 
 | 56 | then \emph{AB} is also an regular expression.  If a string \emph{p} | 
 | 57 | matches A and another string \emph{q} matches B, the string \emph{pq} | 
 | 58 | will match AB.  Thus, complex expressions can easily be constructed | 
 | 59 | from simpler ones like the primitives described here.  For details of | 
 | 60 | the theory and implementation of regular expressions, consult almost | 
 | 61 | any textbook about compiler construction. | 
 | 62 |  | 
 | 63 | % XXX The reference could be made more specific, say to  | 
 | 64 | % "Compilers: Principles, Techniques and Tools", by Alfred V. Aho,  | 
 | 65 | % Ravi Sethi, and Jeffrey D. Ullman, or some FA text.    | 
 | 66 |  | 
| Guido van Rossum | 6240b0b | 1996-10-24 22:49:13 +0000 | [diff] [blame] | 67 | A brief explanation of the format of regular expressions follows. | 
| Guido van Rossum | 1a53560 | 1996-06-26 19:43:22 +0000 | [diff] [blame] | 68 |  | 
 | 69 | Regular expressions can contain both special and ordinary characters. | 
 | 70 | Ordinary characters, like '\code{A}', '\code{a}', or '\code{0}', are | 
 | 71 | the simplest regular expressions; they simply match themselves.  You | 
 | 72 | can concatenate ordinary characters, so '\code{last}' matches the | 
| Guido van Rossum | 6240b0b | 1996-10-24 22:49:13 +0000 | [diff] [blame] | 73 | characters 'last'.  (In the rest of this section, we'll write RE's in | 
 | 74 | \code{this special font}, usually without quotes, and strings to be | 
 | 75 | matched 'in single quotes'.) | 
| Guido van Rossum | 1a53560 | 1996-06-26 19:43:22 +0000 | [diff] [blame] | 76 |  | 
 | 77 | Special characters either stand for classes of ordinary characters, or | 
 | 78 | affect how the regular expressions around them are interpreted. | 
 | 79 |  | 
 | 80 | The special characters are: | 
 | 81 | \begin{itemize} | 
| Fred Drake | 4b3f031 | 1996-12-13 22:04:31 +0000 | [diff] [blame] | 82 | \item[\code{.}] (Dot.)  Matches any character except a newline. | 
 | 83 | \item[\code{\^}] (Caret.)  Matches the start of the string. | 
 | 84 | \item[\code{\$}] Matches the end of the string.   | 
| Guido van Rossum | 1a53560 | 1996-06-26 19:43:22 +0000 | [diff] [blame] | 85 | \code{foo} matches both 'foo' and 'foobar', while the regular | 
| Fred Drake | 4b3f031 | 1996-12-13 22:04:31 +0000 | [diff] [blame] | 86 | expression '\code{foo\$}' matches only 'foo'. | 
| Guido van Rossum | 1a53560 | 1996-06-26 19:43:22 +0000 | [diff] [blame] | 87 | \item[\code{*}] Causes the resulting RE to | 
 | 88 | match 0 or more repetitions of the preceding RE.  \code{ab*} will | 
 | 89 | match 'a', 'ab', or 'a' followed by any number of 'b's. | 
 | 90 | \item[\code{+}] Causes the | 
 | 91 | resulting RE to match 1 or more repetitions of the preceding RE. | 
 | 92 | \code{ab+} will match 'a' followed by any non-zero number of 'b's; it | 
 | 93 | will not match just 'a'. | 
 | 94 | \item[\code{?}] Causes the resulting RE to | 
 | 95 | match 0 or 1 repetitions of the preceding RE.  \code{ab?} will | 
 | 96 | match either 'a' or 'ab'. | 
 | 97 |  | 
 | 98 | \item[\code{\e}] Either escapes special characters (permitting you to match | 
 | 99 | characters like '*?+\&\$'), or signals a special sequence; special | 
 | 100 | sequences are discussed below.  Remember that Python also uses the | 
 | 101 | backslash as an escape sequence in string literals; if the escape | 
 | 102 | sequence isn't recognized by Python's parser, the backslash and | 
 | 103 | subsequent character are included in the resulting string.  However, | 
 | 104 | if Python would recognize the resulting sequence, the backslash should | 
 | 105 | be repeated twice.   | 
 | 106 |  | 
 | 107 | \item[\code{[]}] Used to indicate a set of characters.  Characters can | 
 | 108 | be listed individually, or a range is indicated by giving two | 
 | 109 | characters and separating them by a '-'.  Special characters are | 
 | 110 | not active inside sets.  For example, \code{[akm\$]} | 
 | 111 | will match any of the characters 'a', 'k', 'm', or '\$'; \code{[a-z]} will | 
 | 112 | match any lowercase letter.   | 
 | 113 |  | 
 | 114 | If you want to include a \code{]} inside a | 
 | 115 | set, it must be the first character of the set; to include a \code{-}, | 
 | 116 | place it as the first or last character.  | 
 | 117 |  | 
 | 118 | Characters \emph{not} within a range can be matched by including a | 
 | 119 | \code{\^} as the first character of the set; \code{\^} elsewhere will | 
 | 120 | simply match the '\code{\^}' character.   | 
 | 121 | \end{itemize} | 
 | 122 |  | 
 | 123 | The special sequences consist of '\code{\e}' and a character | 
 | 124 | from the list below.  If the ordinary character is not on the list, | 
 | 125 | then the resulting RE will match the second character.  For example, | 
 | 126 | \code{\e\$} matches the character '\$'.  Ones where the backslash | 
 | 127 | should be doubled are indicated. | 
 | 128 |  | 
 | 129 | \begin{itemize} | 
 | 130 | \item[\code{\e|}]\code{A\e|B}, where A and B can be arbitrary REs, | 
| Guido van Rossum | 6240b0b | 1996-10-24 22:49:13 +0000 | [diff] [blame] | 131 | creates a regular expression that will match either A or B.  This can | 
 | 132 | be used inside groups (see below) as well. | 
| Guido van Rossum | 1a53560 | 1996-06-26 19:43:22 +0000 | [diff] [blame] | 133 | % | 
| Fred Drake | 4b3f031 | 1996-12-13 22:04:31 +0000 | [diff] [blame] | 134 | \item[\code{\e( \e)}] Indicates the start and end of a group; the | 
| Guido van Rossum | 1a53560 | 1996-06-26 19:43:22 +0000 | [diff] [blame] | 135 | contents of a group can be matched later in the string with the | 
| Fred Drake | 4b3f031 | 1996-12-13 22:04:31 +0000 | [diff] [blame] | 136 | \code{\e [1-9]} special sequence, described next. | 
| Guido van Rossum | 1a53560 | 1996-06-26 19:43:22 +0000 | [diff] [blame] | 137 | % | 
 | 138 | {\fulllineitems\item[\code{\e \e 1, ... \e \e 7, \e 8, \e 9}] | 
| Fred Drake | 4b3f031 | 1996-12-13 22:04:31 +0000 | [diff] [blame] | 139 | Matches the contents of the group of the same | 
| Guido van Rossum | 1a53560 | 1996-06-26 19:43:22 +0000 | [diff] [blame] | 140 | number.  For example, \code{\e (.+\e ) \e \e 1} matches 'the the' or | 
 | 141 | '55 55', but not 'the end' (note the space after the group).  This | 
 | 142 | special sequence can only be used to match one of the first 9 groups; | 
 | 143 | groups with higher numbers can be matched using the \code{\e v} | 
| Guido van Rossum | 6240b0b | 1996-10-24 22:49:13 +0000 | [diff] [blame] | 144 | sequence.  (\code{\e 8} and \code{\e 9} don't need a double backslash | 
| Fred Drake | 4b3f031 | 1996-12-13 22:04:31 +0000 | [diff] [blame] | 145 | because they are not octal digits.)} | 
| Guido van Rossum | 1a53560 | 1996-06-26 19:43:22 +0000 | [diff] [blame] | 146 | % | 
| Fred Drake | 4b3f031 | 1996-12-13 22:04:31 +0000 | [diff] [blame] | 147 | \item[\code{\e \e b}] Matches the empty string, but only at the | 
| Guido van Rossum | 1a53560 | 1996-06-26 19:43:22 +0000 | [diff] [blame] | 148 | beginning or end of a word.  A word is defined as a sequence of | 
 | 149 | alphanumeric characters, so the end of a word is indicated by | 
| Fred Drake | 4b3f031 | 1996-12-13 22:04:31 +0000 | [diff] [blame] | 150 | whitespace or a non-alphanumeric character. | 
| Guido van Rossum | 1a53560 | 1996-06-26 19:43:22 +0000 | [diff] [blame] | 151 | % | 
| Fred Drake | 4b3f031 | 1996-12-13 22:04:31 +0000 | [diff] [blame] | 152 | \item[\code{\e B}] Matches the empty string, but when it is \emph{not} at the | 
 | 153 | beginning or end of a word. | 
| Guido van Rossum | 1a53560 | 1996-06-26 19:43:22 +0000 | [diff] [blame] | 154 | % | 
| Fred Drake | 4b3f031 | 1996-12-13 22:04:31 +0000 | [diff] [blame] | 155 | \item[\code{\e v}] Must be followed by a two digit decimal number, and | 
 | 156 | matches the contents of the group of the same number.  The group number must be between 1 and 99, inclusive. | 
| Guido van Rossum | 1a53560 | 1996-06-26 19:43:22 +0000 | [diff] [blame] | 157 | % | 
 | 158 | \item[\code{\e w}]Matches any alphanumeric character; this is | 
 | 159 | equivalent to the set \code{[a-zA-Z0-9]}. | 
 | 160 | % | 
| Fred Drake | 4b3f031 | 1996-12-13 22:04:31 +0000 | [diff] [blame] | 161 | \item[\code{\e W}] Matches any non-alphanumeric character; this is | 
 | 162 | equivalent to the set \code{[\^a-zA-Z0-9]}. | 
 | 163 | \item[\code{\e <}] Matches the empty string, but only at the beginning of a | 
| Guido van Rossum | 1a53560 | 1996-06-26 19:43:22 +0000 | [diff] [blame] | 164 | word.  A word is defined as a sequence of alphanumeric characters, so | 
 | 165 | the end of a word is indicated by whitespace or a non-alphanumeric  | 
| Fred Drake | 4b3f031 | 1996-12-13 22:04:31 +0000 | [diff] [blame] | 166 | character. | 
 | 167 | \item[\code{\e >}] Matches the empty string, but only at the end of a | 
 | 168 | word. | 
| Guido van Rossum | 1a53560 | 1996-06-26 19:43:22 +0000 | [diff] [blame] | 169 |  | 
| Fred Drake | 4b3f031 | 1996-12-13 22:04:31 +0000 | [diff] [blame] | 170 | \item[\code{\e \e \e \e}] Matches a literal backslash. | 
| Guido van Rossum | 6240b0b | 1996-10-24 22:49:13 +0000 | [diff] [blame] | 171 |  | 
| Guido van Rossum | 1a53560 | 1996-06-26 19:43:22 +0000 | [diff] [blame] | 172 | % In Emacs, the following two are start of buffer/end of buffer.  In | 
 | 173 | % Python they seem to be synonyms for ^$. | 
| Fred Drake | 4b3f031 | 1996-12-13 22:04:31 +0000 | [diff] [blame] | 174 | \item[\code{\e `}] Like \code{\^}, this only matches at the start of the | 
 | 175 | string. | 
| Guido van Rossum | 1a53560 | 1996-06-26 19:43:22 +0000 | [diff] [blame] | 176 | \item[\code{\e \e '}] Like \code{\$}, this only matches at the end of the | 
 | 177 | string. | 
 | 178 | % end of buffer | 
 | 179 | \end{itemize} | 
 | 180 |  | 
 | 181 | \subsection{Module Contents} | 
| Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 182 |  | 
 | 183 | The module defines these functions, and an exception: | 
 | 184 |  | 
 | 185 | \renewcommand{\indexsubitem}{(in module regex)} | 
| Guido van Rossum | 326c0bc | 1994-01-03 00:00:31 +0000 | [diff] [blame] | 186 |  | 
| Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 187 | \begin{funcdesc}{match}{pattern\, string} | 
 | 188 |   Return how many characters at the beginning of \var{string} match | 
 | 189 |   the regular expression \var{pattern}.  Return \code{-1} if the | 
 | 190 |   string does not match the pattern (this is different from a | 
 | 191 |   zero-length match!). | 
 | 192 | \end{funcdesc} | 
 | 193 |  | 
 | 194 | \begin{funcdesc}{search}{pattern\, string} | 
 | 195 |   Return the first position in \var{string} that matches the regular | 
| Guido van Rossum | 6240b0b | 1996-10-24 22:49:13 +0000 | [diff] [blame] | 196 |   expression \var{pattern}.  Return \code{-1} if no position in the string | 
| Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 197 |   matches the pattern (this is different from a zero-length match | 
 | 198 |   anywhere!). | 
 | 199 | \end{funcdesc} | 
 | 200 |  | 
| Guido van Rossum | 16d6e71 | 1994-08-08 12:30:22 +0000 | [diff] [blame] | 201 | \begin{funcdesc}{compile}{pattern\optional{\, translate}} | 
| Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 202 |   Compile a regular expression pattern into a regular expression | 
 | 203 |   object, which can be used for matching using its \code{match} and | 
| Guido van Rossum | 470be14 | 1995-03-17 16:07:09 +0000 | [diff] [blame] | 204 |   \code{search} methods, described below.  The optional argument | 
| Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 205 |   \var{translate}, if present, must be a 256-character string | 
 | 206 |   indicating how characters (both of the pattern and of the strings to | 
 | 207 |   be matched) are translated before comparing them; the \code{i}-th | 
 | 208 |   element of the string gives the translation for the character with | 
| Guido van Rossum | 470be14 | 1995-03-17 16:07:09 +0000 | [diff] [blame] | 209 |   \ASCII{} code \code{i}.  This can be used to implement | 
 | 210 |   case-insensitive matching; see the \code{casefold} data item below. | 
| Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 211 |  | 
 | 212 |   The sequence | 
 | 213 |  | 
 | 214 | \bcode\begin{verbatim} | 
 | 215 | prog = regex.compile(pat) | 
 | 216 | result = prog.match(str) | 
 | 217 | \end{verbatim}\ecode | 
| Guido van Rossum | e47da0a | 1997-07-17 16:34:52 +0000 | [diff] [blame] | 218 | % | 
| Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 219 | is equivalent to | 
 | 220 |  | 
 | 221 | \bcode\begin{verbatim} | 
 | 222 | result = regex.match(pat, str) | 
 | 223 | \end{verbatim}\ecode | 
| Guido van Rossum | e47da0a | 1997-07-17 16:34:52 +0000 | [diff] [blame] | 224 | % | 
| Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 225 | but the version using \code{compile()} is more efficient when multiple | 
 | 226 | regular expressions are used concurrently in a single program.  (The | 
 | 227 | compiled version of the last pattern passed to \code{regex.match()} or | 
 | 228 | \code{regex.search()} is cached, so programs that use only a single | 
 | 229 | regular expression at a time needn't worry about compiling regular | 
 | 230 | expressions.) | 
 | 231 | \end{funcdesc} | 
 | 232 |  | 
 | 233 | \begin{funcdesc}{set_syntax}{flags} | 
 | 234 |   Set the syntax to be used by future calls to \code{compile}, | 
 | 235 |   \code{match} and \code{search}.  (Already compiled expression objects | 
 | 236 |   are not affected.)  The argument is an integer which is the OR of | 
 | 237 |   several flag bits.  The return value is the previous value of | 
 | 238 |   the syntax flags.  Names for the flags are defined in the standard | 
 | 239 |   module \code{regex_syntax}; read the file \file{regex_syntax.py} for | 
 | 240 |   more information. | 
 | 241 | \end{funcdesc} | 
 | 242 |  | 
| Barry Warsaw | cd77df6 | 1997-02-18 18:54:30 +0000 | [diff] [blame] | 243 | \begin{funcdesc}{get_syntax}{} | 
 | 244 |   Returns the current value of the syntax flags as an integer. | 
 | 245 | \end{funcdesc} | 
 | 246 |  | 
| Guido van Rossum | 16d6e71 | 1994-08-08 12:30:22 +0000 | [diff] [blame] | 247 | \begin{funcdesc}{symcomp}{pattern\optional{\, translate}} | 
| Guido van Rossum | 326c0bc | 1994-01-03 00:00:31 +0000 | [diff] [blame] | 248 | This is like \code{compile}, but supports symbolic group names: if a | 
| Guido van Rossum | 6c4f003 | 1995-03-07 10:14:09 +0000 | [diff] [blame] | 249 | parenthesis-enclosed group begins with a group name in angular | 
| Guido van Rossum | 326c0bc | 1994-01-03 00:00:31 +0000 | [diff] [blame] | 250 | brackets, e.g. \code{'\e(<id>[a-z][a-z0-9]*\e)'}, the group can | 
 | 251 | be referenced by its name in arguments to the \code{group} method of | 
 | 252 | the resulting compiled regular expression object, like this: | 
| Guido van Rossum | 7defee7 | 1995-02-27 17:52:35 +0000 | [diff] [blame] | 253 | \code{p.group('id')}.  Group names may contain alphanumeric characters | 
 | 254 | and \code{'_'} only. | 
| Guido van Rossum | 326c0bc | 1994-01-03 00:00:31 +0000 | [diff] [blame] | 255 | \end{funcdesc} | 
 | 256 |  | 
| Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 257 | \begin{excdesc}{error} | 
 | 258 |   Exception raised when a string passed to one of the functions here | 
 | 259 |   is not a valid regular expression (e.g., unmatched parentheses) or | 
 | 260 |   when some other error occurs during compilation or matching.  (It is | 
 | 261 |   never an error if a string contains no match for a pattern.) | 
 | 262 | \end{excdesc} | 
 | 263 |  | 
 | 264 | \begin{datadesc}{casefold} | 
 | 265 | A string suitable to pass as \var{translate} argument to | 
 | 266 | \code{compile} to map all upper case characters to their lowercase | 
 | 267 | equivalents. | 
 | 268 | \end{datadesc} | 
 | 269 |  | 
 | 270 | \noindent | 
 | 271 | Compiled regular expression objects support these methods: | 
 | 272 |  | 
 | 273 | \renewcommand{\indexsubitem}{(regex method)} | 
| Guido van Rossum | 16d6e71 | 1994-08-08 12:30:22 +0000 | [diff] [blame] | 274 | \begin{funcdesc}{match}{string\optional{\, pos}} | 
| Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 275 |   Return how many characters at the beginning of \var{string} match | 
 | 276 |   the compiled regular expression.  Return \code{-1} if the string | 
 | 277 |   does not match the pattern (this is different from a zero-length | 
 | 278 |   match!). | 
 | 279 |    | 
 | 280 |   The optional second parameter \var{pos} gives an index in the string | 
 | 281 |   where the search is to start; it defaults to \code{0}.  This is not | 
 | 282 |   completely equivalent to slicing the string; the \code{'\^'} pattern | 
 | 283 |   character matches at the real begin of the string and at positions | 
 | 284 |   just after a newline, not necessarily at the index where the search | 
 | 285 |   is to start. | 
 | 286 | \end{funcdesc} | 
 | 287 |  | 
| Guido van Rossum | 16d6e71 | 1994-08-08 12:30:22 +0000 | [diff] [blame] | 288 | \begin{funcdesc}{search}{string\optional{\, pos}} | 
| Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 289 |   Return the first position in \var{string} that matches the regular | 
 | 290 |   expression \code{pattern}.  Return \code{-1} if no position in the | 
 | 291 |   string matches the pattern (this is different from a zero-length | 
 | 292 |   match anywhere!). | 
 | 293 |    | 
 | 294 |   The optional second parameter has the same meaning as for the | 
 | 295 |   \code{match} method. | 
 | 296 | \end{funcdesc} | 
 | 297 |  | 
 | 298 | \begin{funcdesc}{group}{index\, index\, ...} | 
 | 299 | This method is only valid when the last call to the \code{match} | 
 | 300 | or \code{search} method found a match.  It returns one or more | 
 | 301 | groups of the match.  If there is a single \var{index} argument, | 
 | 302 | the result is a single string; if there are multiple arguments, the | 
 | 303 | result is a tuple with one item per argument.  If the \var{index} is | 
 | 304 | zero, the corresponding return value is the entire matching string; if | 
| Guido van Rossum | 326c0bc | 1994-01-03 00:00:31 +0000 | [diff] [blame] | 305 | it is in the inclusive range [1..99], it is the string matching the | 
| Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 306 | the corresponding parenthesized group (using the default syntax, | 
 | 307 | groups are parenthesized using \code{\\(} and \code{\\)}).  If no | 
 | 308 | such group exists, the corresponding result is \code{None}. | 
| Guido van Rossum | 326c0bc | 1994-01-03 00:00:31 +0000 | [diff] [blame] | 309 |  | 
 | 310 | If the regular expression was compiled by \code{symcomp} instead of | 
 | 311 | \code{compile}, the \var{index} arguments may also be strings | 
 | 312 | identifying groups by their group name. | 
| Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 313 | \end{funcdesc} | 
 | 314 |  | 
 | 315 | \noindent | 
 | 316 | Compiled regular expressions support these data attributes: | 
 | 317 |  | 
 | 318 | \renewcommand{\indexsubitem}{(regex attribute)} | 
| Guido van Rossum | 326c0bc | 1994-01-03 00:00:31 +0000 | [diff] [blame] | 319 |  | 
| Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 320 | \begin{datadesc}{regs} | 
 | 321 | When the last call to the \code{match} or \code{search} method found a | 
 | 322 | match, this is a tuple of pairs of indices corresponding to the | 
 | 323 | beginning and end of all parenthesized groups in the pattern.  Indices | 
 | 324 | are relative to the string argument passed to \code{match} or | 
 | 325 | \code{search}.  The 0-th tuple gives the beginning and end or the | 
 | 326 | whole pattern.  When the last match or search failed, this is | 
 | 327 | \code{None}. | 
 | 328 | \end{datadesc} | 
 | 329 |  | 
 | 330 | \begin{datadesc}{last} | 
 | 331 | When the last call to the \code{match} or \code{search} method found a | 
 | 332 | match, this is the string argument passed to that method.  When the | 
 | 333 | last match or search failed, this is \code{None}. | 
 | 334 | \end{datadesc} | 
 | 335 |  | 
 | 336 | \begin{datadesc}{translate} | 
 | 337 | This is the value of the \var{translate} argument to | 
 | 338 | \code{regex.compile} that created this regular expression object.  If | 
 | 339 | the \var{translate} argument was omitted in the \code{regex.compile} | 
 | 340 | call, this is \code{None}. | 
 | 341 | \end{datadesc} | 
| Guido van Rossum | 326c0bc | 1994-01-03 00:00:31 +0000 | [diff] [blame] | 342 |  | 
 | 343 | \begin{datadesc}{givenpat} | 
 | 344 | The regular expression pattern as passed to \code{compile} or | 
 | 345 | \code{symcomp}. | 
 | 346 | \end{datadesc} | 
 | 347 |  | 
 | 348 | \begin{datadesc}{realpat} | 
 | 349 | The regular expression after stripping the group names for regular | 
 | 350 | expressions compiled with \code{symcomp}.  Same as \code{givenpat} | 
 | 351 | otherwise. | 
 | 352 | \end{datadesc} | 
 | 353 |  | 
 | 354 | \begin{datadesc}{groupindex} | 
 | 355 | A dictionary giving the mapping from symbolic group names to numerical | 
 | 356 | group indices for regular expressions compiled with \code{symcomp}. | 
 | 357 | \code{None} otherwise. | 
 | 358 | \end{datadesc} |