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