Fred Drake | 3a0351c | 1998-04-04 07:23:21 +0000 | [diff] [blame^] | 1 | \section{Built-in Module \module{re}} |
Guido van Rossum | 1acceb0 | 1997-08-14 23:12:18 +0000 | [diff] [blame] | 2 | \label{module-re} |
| 3 | |
| 4 | \bimodindex{re} |
| 5 | |
Guido van Rossum | 1acceb0 | 1997-08-14 23:12:18 +0000 | [diff] [blame] | 6 | This module provides regular expression matching operations similar to |
Guido van Rossum | 0b33410 | 1997-12-08 17:33:40 +0000 | [diff] [blame] | 7 | those found in Perl. It's 8-bit clean: both patterns and strings may |
| 8 | contain null bytes and characters whose high bit is set. It is always |
| 9 | available. |
Guido van Rossum | 1acceb0 | 1997-08-14 23:12:18 +0000 | [diff] [blame] | 10 | |
Fred Drake | 20e0196 | 1998-02-19 15:09:35 +0000 | [diff] [blame] | 11 | Regular expressions use the backslash character (\samp{\e}) to |
Guido van Rossum | 1acceb0 | 1997-08-14 23:12:18 +0000 | [diff] [blame] | 12 | indicate special forms or to allow special characters to be used |
| 13 | without invoking their special meaning. This collides with Python's |
| 14 | usage of the same character for the same purpose in string literals; |
| 15 | for example, to match a literal backslash, one might have to write |
Fred Drake | 20e0196 | 1998-02-19 15:09:35 +0000 | [diff] [blame] | 16 | \samp{\e\e\e\e} as the pattern string, because the regular expression |
| 17 | must be \samp{\e\e}, and each backslash must be expressed as |
| 18 | \samp{\e\e} inside a regular Python string literal. |
Guido van Rossum | 1acceb0 | 1997-08-14 23:12:18 +0000 | [diff] [blame] | 19 | |
| 20 | The solution is to use Python's raw string notation for regular |
| 21 | expression patterns; backslashes are not handled in any special way in |
| 22 | a string literal prefixed with 'r'. So \code{r"\e n"} is a two |
| 23 | character string containing a backslash and the letter 'n', while |
| 24 | \code{"\e n"} is a one-character string containing a newline. Usually |
| 25 | patterns will be expressed in Python code using this raw string notation. |
| 26 | |
Guido van Rossum | 48d0437 | 1997-12-11 20:19:08 +0000 | [diff] [blame] | 27 | \subsection{Regular Expression Syntax} |
Guido van Rossum | 1acceb0 | 1997-08-14 23:12:18 +0000 | [diff] [blame] | 28 | |
| 29 | A regular expression (or RE) specifies a set of strings that matches |
| 30 | it; the functions in this module let you check if a particular string |
| 31 | matches a given regular expression (or if a given regular expression |
| 32 | matches a particular string, which comes down to the same thing). |
| 33 | |
| 34 | Regular expressions can be concatenated to form new regular |
| 35 | expressions; if \emph{A} and \emph{B} are both regular expressions, |
| 36 | then \emph{AB} is also an regular expression. If a string \emph{p} |
| 37 | matches A and another string \emph{q} matches B, the string \emph{pq} |
| 38 | will match AB. Thus, complex expressions can easily be constructed |
| 39 | from simpler primitive expressions like the ones described here. For |
| 40 | details of the theory and implementation of regular expressions, |
| 41 | consult the Friedl book referenced below, or almost any textbook about |
| 42 | compiler construction. |
| 43 | |
Guido van Rossum | 0b33410 | 1997-12-08 17:33:40 +0000 | [diff] [blame] | 44 | A brief explanation of the format of regular expressions follows. |
| 45 | %For further information and a gentler presentation, consult XXX somewhere. |
Guido van Rossum | 1acceb0 | 1997-08-14 23:12:18 +0000 | [diff] [blame] | 46 | |
| 47 | Regular expressions can contain both special and ordinary characters. |
Fred Drake | 20e0196 | 1998-02-19 15:09:35 +0000 | [diff] [blame] | 48 | Most ordinary characters, like \samp{A}, \samp{a}, or \samp{0}, |
Guido van Rossum | 1acceb0 | 1997-08-14 23:12:18 +0000 | [diff] [blame] | 49 | are the simplest regular expressions; they simply match themselves. |
Fred Drake | 20e0196 | 1998-02-19 15:09:35 +0000 | [diff] [blame] | 50 | You can concatenate ordinary characters, so \samp{last} matches the |
Guido van Rossum | 1acceb0 | 1997-08-14 23:12:18 +0000 | [diff] [blame] | 51 | characters 'last'. (In the rest of this section, we'll write RE's in |
| 52 | \code{this special font}, usually without quotes, and strings to be |
| 53 | matched 'in single quotes'.) |
| 54 | |
Fred Drake | 20e0196 | 1998-02-19 15:09:35 +0000 | [diff] [blame] | 55 | Some characters, like \samp{|} or \samp{(}, are special. Special |
Guido van Rossum | 1acceb0 | 1997-08-14 23:12:18 +0000 | [diff] [blame] | 56 | characters either stand for classes of ordinary characters, or affect |
| 57 | how the regular expressions around them are interpreted. |
| 58 | |
| 59 | The special characters are: |
Fred Drake | 2705e80 | 1998-02-16 21:21:13 +0000 | [diff] [blame] | 60 | % define these since they're used twice: |
| 61 | \newcommand{\MyLeftMargin}{0.7in} |
| 62 | \newcommand{\MyLabelWidth}{0.65in} |
| 63 | \begin{list}{}{\leftmargin \MyLeftMargin \labelwidth \MyLabelWidth} |
Guido van Rossum | 1acceb0 | 1997-08-14 23:12:18 +0000 | [diff] [blame] | 64 | \item[\code{.}] (Dot.) In the default mode, this matches any |
Fred Drake | 20e0196 | 1998-02-19 15:09:35 +0000 | [diff] [blame] | 65 | character except a newline. If the \constant{DOTALL} flag has been |
Guido van Rossum | 1acceb0 | 1997-08-14 23:12:18 +0000 | [diff] [blame] | 66 | specified, this matches any character including a newline. |
Fred Drake | 20e0196 | 1998-02-19 15:09:35 +0000 | [diff] [blame] | 67 | % |
Guido van Rossum | 1acceb0 | 1997-08-14 23:12:18 +0000 | [diff] [blame] | 68 | \item[\code{\^}] (Caret.) Matches the start of the string, and in |
Fred Drake | 20e0196 | 1998-02-19 15:09:35 +0000 | [diff] [blame] | 69 | \constant{MULTILINE} mode also immediately after each newline. |
| 70 | % |
Guido van Rossum | 48d0437 | 1997-12-11 20:19:08 +0000 | [diff] [blame] | 71 | \item[\code{\$}] Matches the end of the string, and in |
Fred Drake | 20e0196 | 1998-02-19 15:09:35 +0000 | [diff] [blame] | 72 | \constant{MULTILINE} mode also matches before a newline. |
Guido van Rossum | 1acceb0 | 1997-08-14 23:12:18 +0000 | [diff] [blame] | 73 | \code{foo} matches both 'foo' and 'foobar', while the regular |
Guido van Rossum | 48d0437 | 1997-12-11 20:19:08 +0000 | [diff] [blame] | 74 | expression \code{foo\$} matches only 'foo'. |
Guido van Rossum | 1acceb0 | 1997-08-14 23:12:18 +0000 | [diff] [blame] | 75 | % |
| 76 | \item[\code{*}] Causes the resulting RE to |
| 77 | match 0 or more repetitions of the preceding RE, as many repetitions |
| 78 | as are possible. \code{ab*} will |
| 79 | match 'a', 'ab', or 'a' followed by any number of 'b's. |
| 80 | % |
| 81 | \item[\code{+}] Causes the |
| 82 | resulting RE to match 1 or more repetitions of the preceding RE. |
| 83 | \code{ab+} will match 'a' followed by any non-zero number of 'b's; it |
| 84 | will not match just 'a'. |
| 85 | % |
| 86 | \item[\code{?}] Causes the resulting RE to |
| 87 | match 0 or 1 repetitions of the preceding RE. \code{ab?} will |
| 88 | match either 'a' or 'ab'. |
| 89 | \item[\code{*?}, \code{+?}, \code{??}] The \code{*}, \code{+}, and |
| 90 | \code{?} qualifiers are all \dfn{greedy}; they match as much text as |
| 91 | possible. Sometimes this behaviour isn't desired; if the RE |
| 92 | \code{<.*>} is matched against \code{<H1>title</H1>}, it will match the |
| 93 | entire string, and not just \code{<H1>}. |
| 94 | Adding \code{?} after the qualifier makes it perform the match in |
| 95 | \dfn{non-greedy} or \dfn{minimal} fashion; as few characters as |
| 96 | possible will be matched. Using \code{.*?} in the previous |
Guido van Rossum | 0b33410 | 1997-12-08 17:33:40 +0000 | [diff] [blame] | 97 | expression will match only \code{<H1>}. |
Guido van Rossum | 1acceb0 | 1997-08-14 23:12:18 +0000 | [diff] [blame] | 98 | % |
Guido van Rossum | 0148bbf | 1997-12-22 22:41:40 +0000 | [diff] [blame] | 99 | \item[\code{\{\var{m},\var{n}\}}] Causes the resulting RE to match from |
| 100 | \var{m} to \var{n} repetitions of the preceding RE, attempting to |
| 101 | match as many repetitions as possible. For example, \code{a\{3,5\}} |
| 102 | will match from 3 to 5 'a' characters. |
| 103 | % |
| 104 | \item[\code{\{\var{m},\var{n}\}?}] Causes the resulting RE to |
| 105 | match from \var{m} to \var{n} repetitions of the preceding RE, |
| 106 | attempting to match as \emph{few} repetitions as possible. This is |
| 107 | the non-greedy version of the previous qualifier. For example, on the |
| 108 | 6-character string 'aaaaaa', \code{a\{3,5\}} will match 5 'a' |
| 109 | characters, while \code{a\{3,5\}?} will only match 3 characters. |
| 110 | % |
Guido van Rossum | 1acceb0 | 1997-08-14 23:12:18 +0000 | [diff] [blame] | 111 | \item[\code{\e}] Either escapes special characters (permitting you to match |
| 112 | characters like '*?+\&\$'), or signals a special sequence; special |
| 113 | sequences are discussed below. |
| 114 | |
| 115 | If you're not using a raw string to |
| 116 | express the pattern, remember that Python also uses the |
| 117 | backslash as an escape sequence in string literals; if the escape |
| 118 | sequence isn't recognized by Python's parser, the backslash and |
| 119 | subsequent character are included in the resulting string. However, |
| 120 | if Python would recognize the resulting sequence, the backslash should |
Fred Drake | 023f87f | 1998-01-12 19:16:24 +0000 | [diff] [blame] | 121 | be repeated twice. This is complicated and hard to understand, so |
| 122 | it's highly recommended that you use raw strings for all but the |
| 123 | simplest expressions. |
Guido van Rossum | 1acceb0 | 1997-08-14 23:12:18 +0000 | [diff] [blame] | 124 | % |
| 125 | \item[\code{[]}] Used to indicate a set of characters. Characters can |
Guido van Rossum | 48d0437 | 1997-12-11 20:19:08 +0000 | [diff] [blame] | 126 | be listed individually, or a range of characters can be indicated by |
| 127 | giving two characters and separating them by a '-'. Special |
| 128 | characters are not active inside sets. For example, \code{[akm\$]} |
Fred Drake | 76547c5 | 1998-04-03 05:59:05 +0000 | [diff] [blame] | 129 | will match any of the characters \character{a}, \character{k}, |
| 130 | \character{m}, or \character{\$}; \code{[a-z]} |
Guido van Rossum | 48d0437 | 1997-12-11 20:19:08 +0000 | [diff] [blame] | 131 | will match any lowercase letter and \code{[a-zA-Z0-9]} matches any |
| 132 | letter or digit. Character classes such as \code{\e w} or \code {\e |
| 133 | S} (defined below) are also acceptable inside a range. If you want to |
Fred Drake | 20e0196 | 1998-02-19 15:09:35 +0000 | [diff] [blame] | 134 | include a \samp{]} or a \samp{-} inside a set, precede it with a |
Guido van Rossum | 48d0437 | 1997-12-11 20:19:08 +0000 | [diff] [blame] | 135 | backslash. |
Guido van Rossum | 1acceb0 | 1997-08-14 23:12:18 +0000 | [diff] [blame] | 136 | |
| 137 | Characters \emph{not} within a range can be matched by including a |
| 138 | \code{\^} as the first character of the set; \code{\^} elsewhere will |
Fred Drake | 20e0196 | 1998-02-19 15:09:35 +0000 | [diff] [blame] | 139 | simply match the \samp{\^} character. |
Guido van Rossum | 1acceb0 | 1997-08-14 23:12:18 +0000 | [diff] [blame] | 140 | % |
| 141 | \item[\code{|}]\code{A|B}, where A and B can be arbitrary REs, |
| 142 | creates a regular expression that will match either A or B. This can |
Fred Drake | 20e0196 | 1998-02-19 15:09:35 +0000 | [diff] [blame] | 143 | be used inside groups (see below) as well. To match a literal \samp{|}, |
Guido van Rossum | 1acceb0 | 1997-08-14 23:12:18 +0000 | [diff] [blame] | 144 | use \code{\e|}, or enclose it inside a character class, like \code{[|]}. |
| 145 | % |
Guido van Rossum | 48d0437 | 1997-12-11 20:19:08 +0000 | [diff] [blame] | 146 | \item[\code{(...)}] Matches whatever regular expression is inside the |
| 147 | parentheses, and indicates the start and end of a group; the contents |
| 148 | of a group can be retrieved after a match has been performed, and can |
| 149 | be matched later in the string with the \code{\e \var{number}} special |
| 150 | sequence, described below. To match the literals '(' or ')', |
Guido van Rossum | 1acceb0 | 1997-08-14 23:12:18 +0000 | [diff] [blame] | 151 | use \code{\e(} or \code{\e)}, or enclose them inside a character |
| 152 | class: \code{[(] [)]}. |
| 153 | % |
Guido van Rossum | 0b33410 | 1997-12-08 17:33:40 +0000 | [diff] [blame] | 154 | \item[\code{(?...)}] This is an extension notation (a '?' following a |
| 155 | '(' is not meaningful otherwise). The first character after the '?' |
| 156 | determines what the meaning and further syntax of the construct is. |
Guido van Rossum | e9625e8 | 1998-04-02 01:32:24 +0000 | [diff] [blame] | 157 | Extensions usually do not create a new group; |
| 158 | \code{(?P<\var{name}>...)} is the only exception to this rule. |
Guido van Rossum | 0b33410 | 1997-12-08 17:33:40 +0000 | [diff] [blame] | 159 | Following are the currently supported extensions. |
| 160 | % |
Fred Drake | 20e0196 | 1998-02-19 15:09:35 +0000 | [diff] [blame] | 161 | \item[\code{(?iLmsx)}] (One or more letters from the set \samp{i}, |
| 162 | \samp{L}, \samp{m}, \samp{s}, \samp{x}.) The group matches |
Fred Drake | 023f87f | 1998-01-12 19:16:24 +0000 | [diff] [blame] | 163 | the empty string; the letters set the corresponding flags |
Fred Drake | 20e0196 | 1998-02-19 15:09:35 +0000 | [diff] [blame] | 164 | (\constant{re.I}, \constant{re.L}, \constant{re.M}, \constant{re.S}, |
| 165 | \constant{re.X}) for the entire regular expression. This is useful if |
Guido van Rossum | e9625e8 | 1998-04-02 01:32:24 +0000 | [diff] [blame] | 166 | you wish to include the flags as part of the regular expression, instead |
Fred Drake | 20e0196 | 1998-02-19 15:09:35 +0000 | [diff] [blame] | 167 | of passing a \var{flag} argument to the \function{compile()} function. |
Guido van Rossum | 0b33410 | 1997-12-08 17:33:40 +0000 | [diff] [blame] | 168 | % |
Guido van Rossum | 1acceb0 | 1997-08-14 23:12:18 +0000 | [diff] [blame] | 169 | \item[\code{(?:...)}] A non-grouping version of regular parentheses. |
Guido van Rossum | e9625e8 | 1998-04-02 01:32:24 +0000 | [diff] [blame] | 170 | Matches whatever's inside the parentheses, but the substring matched by the |
Guido van Rossum | 1acceb0 | 1997-08-14 23:12:18 +0000 | [diff] [blame] | 171 | group \emph{cannot} be retrieved after performing a match or |
| 172 | referenced later in the pattern. |
| 173 | % |
| 174 | \item[\code{(?P<\var{name}>...)}] Similar to regular parentheses, but |
Guido van Rossum | e9625e8 | 1998-04-02 01:32:24 +0000 | [diff] [blame] | 175 | the substring matched by the group is accessible via the symbolic group |
Guido van Rossum | 1acceb0 | 1997-08-14 23:12:18 +0000 | [diff] [blame] | 176 | name \var{name}. Group names must be valid Python identifiers. A |
| 177 | symbolic group is also a numbered group, just as if the group were not |
| 178 | named. So the group named 'id' in the example above can also be |
| 179 | referenced as the numbered group 1. |
| 180 | |
Guido van Rossum | 48d0437 | 1997-12-11 20:19:08 +0000 | [diff] [blame] | 181 | For example, if the pattern is |
| 182 | \code{(?P<id>[a-zA-Z_]\e w*)}, the group can be referenced by its |
Guido van Rossum | 1acceb0 | 1997-08-14 23:12:18 +0000 | [diff] [blame] | 183 | name in arguments to methods of match objects, such as \code{m.group('id')} |
Fred Drake | 023f87f | 1998-01-12 19:16:24 +0000 | [diff] [blame] | 184 | or \code{m.end('id')}, and also by name in pattern text |
| 185 | (e.g. \code{(?P=id)}) and replacement text (e.g. \code{\e g<id>}). |
Guido van Rossum | 1acceb0 | 1997-08-14 23:12:18 +0000 | [diff] [blame] | 186 | % |
Fred Drake | 023f87f | 1998-01-12 19:16:24 +0000 | [diff] [blame] | 187 | \item[\code{(?P=\var{name})}] Matches whatever text was matched by the |
| 188 | earlier group named \var{name}. |
Guido van Rossum | 48d0437 | 1997-12-11 20:19:08 +0000 | [diff] [blame] | 189 | % |
Fred Drake | 023f87f | 1998-01-12 19:16:24 +0000 | [diff] [blame] | 190 | \item[\code{(?\#...)}] A comment; the contents of the parentheses are |
| 191 | simply ignored. |
Guido van Rossum | 1acceb0 | 1997-08-14 23:12:18 +0000 | [diff] [blame] | 192 | % |
Fred Drake | 023f87f | 1998-01-12 19:16:24 +0000 | [diff] [blame] | 193 | \item[\code{(?=...)}] Matches if \code{...} matches next, but doesn't |
| 194 | consume any of the string. This is called a lookahead assertion. For |
| 195 | example, \code{Isaac (?=Asimov)} will match 'Isaac~' only if it's |
| 196 | followed by 'Asimov'. |
Guido van Rossum | 1acceb0 | 1997-08-14 23:12:18 +0000 | [diff] [blame] | 197 | % |
Fred Drake | 023f87f | 1998-01-12 19:16:24 +0000 | [diff] [blame] | 198 | \item[\code{(?!...)}] Matches if \code{...} doesn't match next. This |
| 199 | is a negative lookahead assertion. For example, |
| 200 | \code{Isaac (?!Asimov)} will match 'Isaac~' only if it's \emph{not} |
| 201 | followed by 'Asimov'. |
Guido van Rossum | 0b33410 | 1997-12-08 17:33:40 +0000 | [diff] [blame] | 202 | |
Fred Drake | 2705e80 | 1998-02-16 21:21:13 +0000 | [diff] [blame] | 203 | \end{list} |
Guido van Rossum | 1acceb0 | 1997-08-14 23:12:18 +0000 | [diff] [blame] | 204 | |
Fred Drake | 20e0196 | 1998-02-19 15:09:35 +0000 | [diff] [blame] | 205 | The special sequences consist of \samp{\e} and a character from the |
Guido van Rossum | 1acceb0 | 1997-08-14 23:12:18 +0000 | [diff] [blame] | 206 | list below. If the ordinary character is not on the list, then the |
| 207 | resulting RE will match the second character. For example, |
Fred Drake | 20e0196 | 1998-02-19 15:09:35 +0000 | [diff] [blame] | 208 | \code{\e\$} matches the character \samp{\$}. |
Guido van Rossum | 1acceb0 | 1997-08-14 23:12:18 +0000 | [diff] [blame] | 209 | |
Fred Drake | 2705e80 | 1998-02-16 21:21:13 +0000 | [diff] [blame] | 210 | \begin{list}{}{\leftmargin \MyLeftMargin \labelwidth \MyLabelWidth} |
Guido van Rossum | 1acceb0 | 1997-08-14 23:12:18 +0000 | [diff] [blame] | 211 | |
| 212 | % |
| 213 | \item[\code{\e \var{number}}] Matches the contents of the group of the |
Guido van Rossum | 0b33410 | 1997-12-08 17:33:40 +0000 | [diff] [blame] | 214 | same number. Groups are numbered starting from 1. For example, |
| 215 | \code{(.+) \e 1} matches 'the the' or '55 55', but not 'the end' (note |
| 216 | the space after the group). This special sequence can only be used to |
| 217 | match one of the first 99 groups. If the first digit of \var{number} |
| 218 | is 0, or \var{number} is 3 octal digits long, it will not be interpreted |
| 219 | as a group match, but as the character with octal value \var{number}. |
Guido van Rossum | e9625e8 | 1998-04-02 01:32:24 +0000 | [diff] [blame] | 220 | Inside the \code{[} and \code{]} of a character class, all numeric |
| 221 | escapes are treated as characters. |
Guido van Rossum | 1acceb0 | 1997-08-14 23:12:18 +0000 | [diff] [blame] | 222 | % |
| 223 | \item[\code{\e A}] Matches only at the start of the string. |
| 224 | % |
| 225 | \item[\code{\e b}] Matches the empty string, but only at the |
| 226 | beginning or end of a word. A word is defined as a sequence of |
| 227 | alphanumeric characters, so the end of a word is indicated by |
Guido van Rossum | 48d0437 | 1997-12-11 20:19:08 +0000 | [diff] [blame] | 228 | whitespace or a non-alphanumeric character. Inside a character range, |
| 229 | \code{\e b} represents the backspace character, for compatibility with |
| 230 | Python's string literals. |
Guido van Rossum | 1acceb0 | 1997-08-14 23:12:18 +0000 | [diff] [blame] | 231 | % |
Guido van Rossum | 0b33410 | 1997-12-08 17:33:40 +0000 | [diff] [blame] | 232 | \item[\code{\e B}] Matches the empty string, but only when it is |
| 233 | \emph{not} at the beginning or end of a word. |
Guido van Rossum | 1acceb0 | 1997-08-14 23:12:18 +0000 | [diff] [blame] | 234 | % |
| 235 | \item[\code{\e d}]Matches any decimal digit; this is |
| 236 | equivalent to the set \code{[0-9]}. |
| 237 | % |
| 238 | \item[\code{\e D}]Matches any non-digit character; this is |
Fred Drake | 20e0196 | 1998-02-19 15:09:35 +0000 | [diff] [blame] | 239 | equivalent to the set \code{[\^0-9]}. |
Guido van Rossum | 1acceb0 | 1997-08-14 23:12:18 +0000 | [diff] [blame] | 240 | % |
| 241 | \item[\code{\e s}]Matches any whitespace character; this is |
| 242 | equivalent to the set \code{[ \e t\e n\e r\e f\e v]}. |
| 243 | % |
| 244 | \item[\code{\e S}]Matches any non-whitespace character; this is |
Guido van Rossum | f5370f4 | 1998-02-11 22:52:47 +0000 | [diff] [blame] | 245 | equivalent to the set \code{[\^\ \e t\e n\e r\e f\e v]}. |
Guido van Rossum | 1acceb0 | 1997-08-14 23:12:18 +0000 | [diff] [blame] | 246 | % |
Fred Drake | 20e0196 | 1998-02-19 15:09:35 +0000 | [diff] [blame] | 247 | \item[\code{\e w}]When the \constant{LOCALE} flag is not specified, |
Fred Drake | 023f87f | 1998-01-12 19:16:24 +0000 | [diff] [blame] | 248 | matches any alphanumeric character; this is equivalent to the set |
Fred Drake | 20e0196 | 1998-02-19 15:09:35 +0000 | [diff] [blame] | 249 | \code{[a-zA-Z0-9_]}. With \constant{LOCALE}, it will match the set |
Fred Drake | 023f87f | 1998-01-12 19:16:24 +0000 | [diff] [blame] | 250 | \code{[0-9_]} plus whatever characters are defined as letters for the |
| 251 | current locale. |
Guido van Rossum | 1acceb0 | 1997-08-14 23:12:18 +0000 | [diff] [blame] | 252 | % |
Fred Drake | 20e0196 | 1998-02-19 15:09:35 +0000 | [diff] [blame] | 253 | \item[\code{\e W}]When the \constant{LOCALE} flag is not specified, |
Fred Drake | 023f87f | 1998-01-12 19:16:24 +0000 | [diff] [blame] | 254 | matches any non-alphanumeric character; this is equivalent to the set |
Fred Drake | 20e0196 | 1998-02-19 15:09:35 +0000 | [diff] [blame] | 255 | \code{[\^a-zA-Z0-9_]}. With \constant{LOCALE}, it will match any |
Fred Drake | 023f87f | 1998-01-12 19:16:24 +0000 | [diff] [blame] | 256 | character not in the set \code{[0-9_]}, and not defined as a letter |
Guido van Rossum | 0b33410 | 1997-12-08 17:33:40 +0000 | [diff] [blame] | 257 | for the current locale. |
Guido van Rossum | 1acceb0 | 1997-08-14 23:12:18 +0000 | [diff] [blame] | 258 | |
| 259 | \item[\code{\e Z}]Matches only at the end of the string. |
| 260 | % |
| 261 | |
| 262 | \item[\code{\e \e}] Matches a literal backslash. |
| 263 | |
Fred Drake | 2705e80 | 1998-02-16 21:21:13 +0000 | [diff] [blame] | 264 | \end{list} |
Guido van Rossum | 1acceb0 | 1997-08-14 23:12:18 +0000 | [diff] [blame] | 265 | |
| 266 | \subsection{Module Contents} |
Fred Drake | 78f8e98 | 1997-12-29 21:39:39 +0000 | [diff] [blame] | 267 | \nodename{Contents of Module re} |
Guido van Rossum | 1acceb0 | 1997-08-14 23:12:18 +0000 | [diff] [blame] | 268 | |
| 269 | The module defines the following functions and constants, and an exception: |
| 270 | |
Guido van Rossum | 1acceb0 | 1997-08-14 23:12:18 +0000 | [diff] [blame] | 271 | |
Fred Drake | 013ad98 | 1998-03-08 07:38:27 +0000 | [diff] [blame] | 272 | \begin{funcdesc}{compile}{pattern\optional{, flags}} |
Guido van Rossum | 1acceb0 | 1997-08-14 23:12:18 +0000 | [diff] [blame] | 273 | Compile a regular expression pattern into a regular expression |
Fred Drake | 20e0196 | 1998-02-19 15:09:35 +0000 | [diff] [blame] | 274 | object, which can be used for matching using its \function{match()} and |
| 275 | \function{search()} methods, described below. |
Guido van Rossum | 1acceb0 | 1997-08-14 23:12:18 +0000 | [diff] [blame] | 276 | |
Guido van Rossum | 0b33410 | 1997-12-08 17:33:40 +0000 | [diff] [blame] | 277 | The expression's behaviour can be modified by specifying a |
| 278 | \var{flags} value. Values can be any of the following variables, |
| 279 | combined using bitwise OR (the \code{|} operator). |
| 280 | |
Fred Drake | 76547c5 | 1998-04-03 05:59:05 +0000 | [diff] [blame] | 281 | The sequence |
| 282 | |
| 283 | \begin{verbatim} |
| 284 | prog = re.compile(pat) |
| 285 | result = prog.match(str) |
| 286 | \end{verbatim} |
| 287 | |
| 288 | is equivalent to |
| 289 | |
| 290 | \begin{verbatim} |
| 291 | result = re.match(pat, str) |
| 292 | \end{verbatim} |
| 293 | |
| 294 | but the version using \function{compile()} is more efficient when the |
| 295 | expression will be used several times in a single program. |
| 296 | %(The compiled version of the last pattern passed to |
| 297 | %\function{regex.match()} or \function{regex.search()} is cached, so |
| 298 | %programs that use only a single regular expression at a time needn't |
| 299 | %worry about compiling regular expressions.) |
| 300 | \end{funcdesc} |
| 301 | |
Fred Drake | 013ad98 | 1998-03-08 07:38:27 +0000 | [diff] [blame] | 302 | \begin{datadesc}{I} |
| 303 | \dataline{IGNORECASE} |
Fred Drake | 78f8e98 | 1997-12-29 21:39:39 +0000 | [diff] [blame] | 304 | Perform case-insensitive matching; expressions like \code{[A-Z]} will match |
Guido van Rossum | 48d0437 | 1997-12-11 20:19:08 +0000 | [diff] [blame] | 305 | lowercase letters, too. This is not affected by the current locale. |
Fred Drake | 013ad98 | 1998-03-08 07:38:27 +0000 | [diff] [blame] | 306 | \end{datadesc} |
Guido van Rossum | 0b33410 | 1997-12-08 17:33:40 +0000 | [diff] [blame] | 307 | |
Fred Drake | 013ad98 | 1998-03-08 07:38:27 +0000 | [diff] [blame] | 308 | \begin{datadesc}{L} |
| 309 | \dataline{LOCALE} |
Fred Drake | 78f8e98 | 1997-12-29 21:39:39 +0000 | [diff] [blame] | 310 | Make \code{\e w}, \code{\e W}, \code{\e b}, |
Guido van Rossum | 48d0437 | 1997-12-11 20:19:08 +0000 | [diff] [blame] | 311 | \code{\e B}, dependent on the current locale. |
Fred Drake | 013ad98 | 1998-03-08 07:38:27 +0000 | [diff] [blame] | 312 | \end{datadesc} |
Guido van Rossum | a42c178 | 1997-12-09 20:41:47 +0000 | [diff] [blame] | 313 | |
Fred Drake | 013ad98 | 1998-03-08 07:38:27 +0000 | [diff] [blame] | 314 | \begin{datadesc}{M} |
| 315 | \dataline{MULTILINE} |
Fred Drake | 78f8e98 | 1997-12-29 21:39:39 +0000 | [diff] [blame] | 316 | When specified, the pattern character \code{\^} matches at the |
Fred Drake | 023f87f | 1998-01-12 19:16:24 +0000 | [diff] [blame] | 317 | beginning of the string and at the beginning of each line |
| 318 | (immediately following each newline); and the pattern character |
Guido van Rossum | 48d0437 | 1997-12-11 20:19:08 +0000 | [diff] [blame] | 319 | \code{\$} matches at the end of the string and at the end of each line |
| 320 | (immediately preceding each newline). |
Guido van Rossum | 0b33410 | 1997-12-08 17:33:40 +0000 | [diff] [blame] | 321 | By default, \code{\^} matches only at the beginning of the string, and |
| 322 | \code{\$} only at the end of the string and immediately before the |
| 323 | newline (if any) at the end of the string. |
Fred Drake | 013ad98 | 1998-03-08 07:38:27 +0000 | [diff] [blame] | 324 | \end{datadesc} |
Guido van Rossum | 0b33410 | 1997-12-08 17:33:40 +0000 | [diff] [blame] | 325 | |
Fred Drake | 013ad98 | 1998-03-08 07:38:27 +0000 | [diff] [blame] | 326 | \begin{datadesc}{S} |
| 327 | \dataline{DOTALL} |
Guido van Rossum | e9625e8 | 1998-04-02 01:32:24 +0000 | [diff] [blame] | 328 | Make the \code{.} special character match any character at all, including a |
Guido van Rossum | 48d0437 | 1997-12-11 20:19:08 +0000 | [diff] [blame] | 329 | newline; without this flag, \code{.} will match anything \emph{except} |
Fred Drake | 78f8e98 | 1997-12-29 21:39:39 +0000 | [diff] [blame] | 330 | a newline. |
Fred Drake | 013ad98 | 1998-03-08 07:38:27 +0000 | [diff] [blame] | 331 | \end{datadesc} |
Guido van Rossum | 48d0437 | 1997-12-11 20:19:08 +0000 | [diff] [blame] | 332 | |
Fred Drake | 013ad98 | 1998-03-08 07:38:27 +0000 | [diff] [blame] | 333 | \begin{datadesc}{X} |
| 334 | \dataline{VERBOSE} |
Fred Drake | 78f8e98 | 1997-12-29 21:39:39 +0000 | [diff] [blame] | 335 | Ignore whitespace within the pattern |
Guido van Rossum | 48d0437 | 1997-12-11 20:19:08 +0000 | [diff] [blame] | 336 | except when in a character class or preceded by an unescaped |
| 337 | backslash, and, when a line contains a \code{\#} neither in a character |
| 338 | class or preceded by an unescaped backslash, all characters from the |
Fred Drake | 78f8e98 | 1997-12-29 21:39:39 +0000 | [diff] [blame] | 339 | leftmost such \code{\#} through the end of the line are ignored. |
Fred Drake | 013ad98 | 1998-03-08 07:38:27 +0000 | [diff] [blame] | 340 | \end{datadesc} |
Guido van Rossum | 0b33410 | 1997-12-08 17:33:40 +0000 | [diff] [blame] | 341 | |
Guido van Rossum | 0b33410 | 1997-12-08 17:33:40 +0000 | [diff] [blame] | 342 | |
Guido van Rossum | 1acceb0 | 1997-08-14 23:12:18 +0000 | [diff] [blame] | 343 | \begin{funcdesc}{escape}{string} |
Guido van Rossum | 48d0437 | 1997-12-11 20:19:08 +0000 | [diff] [blame] | 344 | Return \var{string} with all non-alphanumerics backslashed; this is |
| 345 | useful if you want to match an arbitrary literal string that may have |
| 346 | regular expression metacharacters in it. |
Guido van Rossum | 1acceb0 | 1997-08-14 23:12:18 +0000 | [diff] [blame] | 347 | \end{funcdesc} |
| 348 | |
Fred Drake | 013ad98 | 1998-03-08 07:38:27 +0000 | [diff] [blame] | 349 | \begin{funcdesc}{match}{pattern, string\optional{, flags}} |
Guido van Rossum | 1acceb0 | 1997-08-14 23:12:18 +0000 | [diff] [blame] | 350 | If zero or more characters at the beginning of \var{string} match |
| 351 | the regular expression \var{pattern}, return a corresponding |
Fred Drake | 20e0196 | 1998-02-19 15:09:35 +0000 | [diff] [blame] | 352 | \class{MatchObject} instance. Return \code{None} if the string does not |
Guido van Rossum | 1acceb0 | 1997-08-14 23:12:18 +0000 | [diff] [blame] | 353 | match the pattern; note that this is different from a zero-length |
| 354 | match. |
| 355 | \end{funcdesc} |
| 356 | |
Fred Drake | 013ad98 | 1998-03-08 07:38:27 +0000 | [diff] [blame] | 357 | \begin{funcdesc}{search}{pattern, string\optional{, flags}} |
Guido van Rossum | 1acceb0 | 1997-08-14 23:12:18 +0000 | [diff] [blame] | 358 | Scan through \var{string} looking for a location where the regular |
Fred Drake | 023f87f | 1998-01-12 19:16:24 +0000 | [diff] [blame] | 359 | expression \var{pattern} produces a match, and return a |
Fred Drake | 20e0196 | 1998-02-19 15:09:35 +0000 | [diff] [blame] | 360 | corresponding \class{MatchObject} instance. |
Guido van Rossum | 0148bbf | 1997-12-22 22:41:40 +0000 | [diff] [blame] | 361 | Return \code{None} if no |
Guido van Rossum | 1acceb0 | 1997-08-14 23:12:18 +0000 | [diff] [blame] | 362 | position in the string matches the pattern; note that this is |
| 363 | different from finding a zero-length match at some point in the string. |
| 364 | \end{funcdesc} |
| 365 | |
Fred Drake | 013ad98 | 1998-03-08 07:38:27 +0000 | [diff] [blame] | 366 | \begin{funcdesc}{split}{pattern, string, \optional{, maxsplit\code{ = 0}}} |
Guido van Rossum | 1acceb0 | 1997-08-14 23:12:18 +0000 | [diff] [blame] | 367 | Split \var{string} by the occurrences of \var{pattern}. If |
| 368 | capturing parentheses are used in pattern, then occurrences of |
| 369 | patterns or subpatterns are also returned. |
Guido van Rossum | 9754639 | 1998-01-12 18:58:53 +0000 | [diff] [blame] | 370 | If \var{maxsplit} is nonzero, at most \var{maxsplit} splits |
| 371 | occur, and the remainder of the string is returned as the final |
| 372 | element of the list. (Incompatibility note: in the original Python |
| 373 | 1.5 release, \var{maxsplit} was ignored. This has been fixed in |
| 374 | later releases.) |
Guido van Rossum | 1acceb0 | 1997-08-14 23:12:18 +0000 | [diff] [blame] | 375 | % |
Fred Drake | 1947991 | 1998-02-13 06:58:54 +0000 | [diff] [blame] | 376 | \begin{verbatim} |
Guido van Rossum | 1acceb0 | 1997-08-14 23:12:18 +0000 | [diff] [blame] | 377 | >>> re.split('[\W]+', 'Words, words, words.') |
| 378 | ['Words', 'words', 'words', ''] |
| 379 | >>> re.split('([\W]+)', 'Words, words, words.') |
| 380 | ['Words', ', ', 'words', ', ', 'words', '.', ''] |
Guido van Rossum | 9754639 | 1998-01-12 18:58:53 +0000 | [diff] [blame] | 381 | >>> re.split('[\W]+', 'Words, words, words.', 1) |
| 382 | ['Words', 'words, words.'] |
Fred Drake | 1947991 | 1998-02-13 06:58:54 +0000 | [diff] [blame] | 383 | \end{verbatim} |
Guido van Rossum | 1acceb0 | 1997-08-14 23:12:18 +0000 | [diff] [blame] | 384 | % |
| 385 | This function combines and extends the functionality of |
Fred Drake | 20e0196 | 1998-02-19 15:09:35 +0000 | [diff] [blame] | 386 | the old \function{regsub.split()} and \function{regsub.splitx()}. |
Guido van Rossum | 1acceb0 | 1997-08-14 23:12:18 +0000 | [diff] [blame] | 387 | \end{funcdesc} |
| 388 | |
Fred Drake | 013ad98 | 1998-03-08 07:38:27 +0000 | [diff] [blame] | 389 | \begin{funcdesc}{sub}{pattern, repl, string\optional{, count\code{ = 0}}} |
Guido van Rossum | 1acceb0 | 1997-08-14 23:12:18 +0000 | [diff] [blame] | 390 | Return the string obtained by replacing the leftmost non-overlapping |
| 391 | occurrences of \var{pattern} in \var{string} by the replacement |
Barry Warsaw | 4552f3d | 1997-11-20 00:15:13 +0000 | [diff] [blame] | 392 | \var{repl}. If the pattern isn't found, \var{string} is returned |
| 393 | unchanged. \var{repl} can be a string or a function; if a function, |
| 394 | it is called for every non-overlapping occurance of \var{pattern}. |
Guido van Rossum | 0b33410 | 1997-12-08 17:33:40 +0000 | [diff] [blame] | 395 | The function takes a single match object argument, and returns the |
| 396 | replacement string. For example: |
Barry Warsaw | 4552f3d | 1997-11-20 00:15:13 +0000 | [diff] [blame] | 397 | % |
Fred Drake | 1947991 | 1998-02-13 06:58:54 +0000 | [diff] [blame] | 398 | \begin{verbatim} |
Barry Warsaw | 4552f3d | 1997-11-20 00:15:13 +0000 | [diff] [blame] | 399 | >>> def dashrepl(matchobj): |
Guido van Rossum | e9625e8 | 1998-04-02 01:32:24 +0000 | [diff] [blame] | 400 | .... if matchobj.group(0) == '-': return ' ' |
| 401 | .... else: return '-' |
Barry Warsaw | 4552f3d | 1997-11-20 00:15:13 +0000 | [diff] [blame] | 402 | >>> re.sub('-{1,2}', dashrepl, 'pro----gram-files') |
| 403 | 'pro--gram files' |
Fred Drake | 1947991 | 1998-02-13 06:58:54 +0000 | [diff] [blame] | 404 | \end{verbatim} |
Barry Warsaw | 4552f3d | 1997-11-20 00:15:13 +0000 | [diff] [blame] | 405 | % |
Guido van Rossum | 0b33410 | 1997-12-08 17:33:40 +0000 | [diff] [blame] | 406 | The pattern may be a string or a |
Guido van Rossum | 48d0437 | 1997-12-11 20:19:08 +0000 | [diff] [blame] | 407 | regex object; if you need to specify |
| 408 | regular expression flags, you must use a regex object, or use |
| 409 | embedded modifiers in a pattern; e.g. |
Fred Drake | 013ad98 | 1998-03-08 07:38:27 +0000 | [diff] [blame] | 410 | \samp{sub("(?i)b+", "x", "bbbb BBBB")} returns \code{'x x'}. |
Fred Drake | 023f87f | 1998-01-12 19:16:24 +0000 | [diff] [blame] | 411 | |
Guido van Rossum | 1acceb0 | 1997-08-14 23:12:18 +0000 | [diff] [blame] | 412 | The optional argument \var{count} is the maximum number of pattern |
| 413 | occurrences to be replaced; count must be a non-negative integer, and |
| 414 | the default value of 0 means to replace all occurrences. |
| 415 | |
| 416 | Empty matches for the pattern are replaced only when not adjacent to a |
Fred Drake | 013ad98 | 1998-03-08 07:38:27 +0000 | [diff] [blame] | 417 | previous match, so \samp{sub('x*', '-', 'abc')} returns \code{'-a-b-c-'}. |
Guido van Rossum | e9625e8 | 1998-04-02 01:32:24 +0000 | [diff] [blame] | 418 | |
| 419 | If \var{repl} is a string, any backslash escapes in it are processed. |
| 420 | That is, \samp{\e n} is converted to a single newline character, |
| 421 | \samp{\e r} is converted to a linefeed, and so forth. Unknown escapes |
Guido van Rossum | 791468f | 1998-04-03 20:07:37 +0000 | [diff] [blame] | 422 | such as \samp{\e j} are left alone. Backreferences, such as \samp{\e 6} are |
Guido van Rossum | e9625e8 | 1998-04-02 01:32:24 +0000 | [diff] [blame] | 423 | replaced with the substring matched by group 6 in the pattern. |
| 424 | |
| 425 | In addition to character escapes and backreferences as described |
| 426 | above, \samp{\e g<name>} will use the substring matched by the group |
| 427 | named \samp{name}, as defined by the \samp{(?P<name>...)} syntax. |
| 428 | \samp{\e g<number>} uses the corresponding group number; \samp{\e |
| 429 | g<2>} is therefore equivalent to \samp{\e 2}, but isn't ambiguous in a |
| 430 | replacement such as \samp{\e g<2>0}. \samp{\e 20} would be |
| 431 | interpreted as a reference to group 20, not a reference to group 2 |
| 432 | followed by the literal character \samp{0}. |
Guido van Rossum | 1acceb0 | 1997-08-14 23:12:18 +0000 | [diff] [blame] | 433 | \end{funcdesc} |
| 434 | |
Fred Drake | 013ad98 | 1998-03-08 07:38:27 +0000 | [diff] [blame] | 435 | \begin{funcdesc}{subn}{pattern, repl, string\optional{, count\code{ = 0}}} |
Fred Drake | 20e0196 | 1998-02-19 15:09:35 +0000 | [diff] [blame] | 436 | Perform the same operation as \function{sub()}, but return a tuple |
Fred Drake | 023f87f | 1998-01-12 19:16:24 +0000 | [diff] [blame] | 437 | \code{(\var{new_string}, \var{number_of_subs_made})}. |
Guido van Rossum | 1acceb0 | 1997-08-14 23:12:18 +0000 | [diff] [blame] | 438 | \end{funcdesc} |
| 439 | |
| 440 | \begin{excdesc}{error} |
| 441 | Exception raised when a string passed to one of the functions here |
| 442 | is not a valid regular expression (e.g., unmatched parentheses) or |
Fred Drake | 013ad98 | 1998-03-08 07:38:27 +0000 | [diff] [blame] | 443 | when some other error occurs during compilation or matching. It is |
| 444 | never an error if a string contains no match for a pattern. |
Guido van Rossum | 1acceb0 | 1997-08-14 23:12:18 +0000 | [diff] [blame] | 445 | \end{excdesc} |
| 446 | |
| 447 | \subsection{Regular Expression Objects} |
| 448 | Compiled regular expression objects support the following methods and |
| 449 | attributes: |
| 450 | |
Fred Drake | 76547c5 | 1998-04-03 05:59:05 +0000 | [diff] [blame] | 451 | \begin{methoddesc}[RegexObject]{match}{string\optional{, pos}\optional{, |
| 452 | endpos}} |
Guido van Rossum | eb53ae4 | 1997-10-05 18:54:07 +0000 | [diff] [blame] | 453 | If zero or more characters at the beginning of \var{string} match |
| 454 | this regular expression, return a corresponding |
Fred Drake | 20e0196 | 1998-02-19 15:09:35 +0000 | [diff] [blame] | 455 | \class{MatchObject} instance. Return \code{None} if the string does not |
Guido van Rossum | eb53ae4 | 1997-10-05 18:54:07 +0000 | [diff] [blame] | 456 | match the pattern; note that this is different from a zero-length |
| 457 | match. |
Guido van Rossum | 1acceb0 | 1997-08-14 23:12:18 +0000 | [diff] [blame] | 458 | |
| 459 | The optional second parameter \var{pos} gives an index in the string |
Guido van Rossum | 48d0437 | 1997-12-11 20:19:08 +0000 | [diff] [blame] | 460 | where the search is to start; it defaults to \code{0}. The |
Fred Drake | 20e0196 | 1998-02-19 15:09:35 +0000 | [diff] [blame] | 461 | \samp{\^} pattern character will match at the index where the |
Guido van Rossum | 48d0437 | 1997-12-11 20:19:08 +0000 | [diff] [blame] | 462 | search is to start. |
Guido van Rossum | 0b33410 | 1997-12-08 17:33:40 +0000 | [diff] [blame] | 463 | |
| 464 | The optional parameter \var{endpos} limits how far the string will |
| 465 | be searched; it will be as if the string is \var{endpos} characters |
| 466 | long, so only the characters from \var{pos} to \var{endpos} will be |
| 467 | searched for a match. |
Fred Drake | 76547c5 | 1998-04-03 05:59:05 +0000 | [diff] [blame] | 468 | \end{methoddesc} |
Guido van Rossum | 1acceb0 | 1997-08-14 23:12:18 +0000 | [diff] [blame] | 469 | |
Fred Drake | 76547c5 | 1998-04-03 05:59:05 +0000 | [diff] [blame] | 470 | \begin{methoddesc}[RegexObject]{search}{string\optional{, pos}\optional{, |
| 471 | endpos}} |
Guido van Rossum | eb53ae4 | 1997-10-05 18:54:07 +0000 | [diff] [blame] | 472 | Scan through \var{string} looking for a location where this regular |
| 473 | expression produces a match. Return \code{None} if no |
| 474 | position in the string matches the pattern; note that this is |
| 475 | different from finding a zero-length match at some point in the string. |
Guido van Rossum | 1acceb0 | 1997-08-14 23:12:18 +0000 | [diff] [blame] | 476 | |
Guido van Rossum | 48d0437 | 1997-12-11 20:19:08 +0000 | [diff] [blame] | 477 | The optional \var{pos} and \var{endpos} parameters have the same |
Fred Drake | 20e0196 | 1998-02-19 15:09:35 +0000 | [diff] [blame] | 478 | meaning as for the \method{match()} method. |
Fred Drake | 76547c5 | 1998-04-03 05:59:05 +0000 | [diff] [blame] | 479 | \end{methoddesc} |
Guido van Rossum | 1acceb0 | 1997-08-14 23:12:18 +0000 | [diff] [blame] | 480 | |
Fred Drake | 76547c5 | 1998-04-03 05:59:05 +0000 | [diff] [blame] | 481 | \begin{methoddesc}[RegexObject]{split}{string, \optional{, |
| 482 | maxsplit\code{ = 0}}} |
Fred Drake | 20e0196 | 1998-02-19 15:09:35 +0000 | [diff] [blame] | 483 | Identical to the \function{split()} function, using the compiled pattern. |
Fred Drake | 76547c5 | 1998-04-03 05:59:05 +0000 | [diff] [blame] | 484 | \end{methoddesc} |
Guido van Rossum | 1acceb0 | 1997-08-14 23:12:18 +0000 | [diff] [blame] | 485 | |
Fred Drake | 76547c5 | 1998-04-03 05:59:05 +0000 | [diff] [blame] | 486 | \begin{methoddesc}[RegexObject]{sub}{repl, string\optional{, count\code{ = 0}}} |
Fred Drake | 20e0196 | 1998-02-19 15:09:35 +0000 | [diff] [blame] | 487 | Identical to the \function{sub()} function, using the compiled pattern. |
Fred Drake | 76547c5 | 1998-04-03 05:59:05 +0000 | [diff] [blame] | 488 | \end{methoddesc} |
Guido van Rossum | 1acceb0 | 1997-08-14 23:12:18 +0000 | [diff] [blame] | 489 | |
Fred Drake | 76547c5 | 1998-04-03 05:59:05 +0000 | [diff] [blame] | 490 | \begin{methoddesc}[RegexObject]{subn}{repl, string\optional{, |
| 491 | count\code{ = 0}}} |
Fred Drake | 20e0196 | 1998-02-19 15:09:35 +0000 | [diff] [blame] | 492 | Identical to the \function{subn()} function, using the compiled pattern. |
Fred Drake | 76547c5 | 1998-04-03 05:59:05 +0000 | [diff] [blame] | 493 | \end{methoddesc} |
Guido van Rossum | 1acceb0 | 1997-08-14 23:12:18 +0000 | [diff] [blame] | 494 | |
Guido van Rossum | 1acceb0 | 1997-08-14 23:12:18 +0000 | [diff] [blame] | 495 | |
Fred Drake | 76547c5 | 1998-04-03 05:59:05 +0000 | [diff] [blame] | 496 | \begin{memberdesc}[RegexObject]{flags} |
Fred Drake | 013ad98 | 1998-03-08 07:38:27 +0000 | [diff] [blame] | 497 | The flags argument used when the regex object was compiled, or |
| 498 | \code{0} if no flags were provided. |
Fred Drake | 76547c5 | 1998-04-03 05:59:05 +0000 | [diff] [blame] | 499 | \end{memberdesc} |
Guido van Rossum | 1acceb0 | 1997-08-14 23:12:18 +0000 | [diff] [blame] | 500 | |
Fred Drake | 76547c5 | 1998-04-03 05:59:05 +0000 | [diff] [blame] | 501 | \begin{memberdesc}[RegexObject]{groupindex} |
Fred Drake | 013ad98 | 1998-03-08 07:38:27 +0000 | [diff] [blame] | 502 | A dictionary mapping any symbolic group names defined by |
| 503 | \code{(?P<\var{id}>)} to group numbers. The dictionary is empty if no |
Guido van Rossum | 1acceb0 | 1997-08-14 23:12:18 +0000 | [diff] [blame] | 504 | symbolic groups were used in the pattern. |
Fred Drake | 76547c5 | 1998-04-03 05:59:05 +0000 | [diff] [blame] | 505 | \end{memberdesc} |
Guido van Rossum | 1acceb0 | 1997-08-14 23:12:18 +0000 | [diff] [blame] | 506 | |
Fred Drake | 76547c5 | 1998-04-03 05:59:05 +0000 | [diff] [blame] | 507 | \begin{memberdesc}[RegexObject]{pattern} |
Guido van Rossum | 1acceb0 | 1997-08-14 23:12:18 +0000 | [diff] [blame] | 508 | The pattern string from which the regex object was compiled. |
Fred Drake | 76547c5 | 1998-04-03 05:59:05 +0000 | [diff] [blame] | 509 | \end{memberdesc} |
Guido van Rossum | 1acceb0 | 1997-08-14 23:12:18 +0000 | [diff] [blame] | 510 | |
Fred Drake | 023f87f | 1998-01-12 19:16:24 +0000 | [diff] [blame] | 511 | \subsection{Match Objects} |
| 512 | |
Fred Drake | 20e0196 | 1998-02-19 15:09:35 +0000 | [diff] [blame] | 513 | \class{MatchObject} instances support the following methods and attributes: |
Guido van Rossum | 1acceb0 | 1997-08-14 23:12:18 +0000 | [diff] [blame] | 514 | |
Fred Drake | 76547c5 | 1998-04-03 05:59:05 +0000 | [diff] [blame] | 515 | \begin{methoddesc}[MatchObject]{group}{\optional{group1, group2, ...}} |
Guido van Rossum | 4650392 | 1998-01-19 23:14:17 +0000 | [diff] [blame] | 516 | Returns one or more subgroups of the match. If there is a single |
| 517 | argument, the result is a single string; if there are |
Guido van Rossum | 48d0437 | 1997-12-11 20:19:08 +0000 | [diff] [blame] | 518 | multiple arguments, the result is a tuple with one item per argument. |
Guido van Rossum | 4650392 | 1998-01-19 23:14:17 +0000 | [diff] [blame] | 519 | Without arguments, \var{group1} defaults to zero (i.e. the whole match |
| 520 | is returned). |
| 521 | If a \var{groupN} argument is zero, the corresponding return value is the |
Guido van Rossum | 48d0437 | 1997-12-11 20:19:08 +0000 | [diff] [blame] | 522 | entire matching string; if it is in the inclusive range [1..99], it is |
Guido van Rossum | 791468f | 1998-04-03 20:07:37 +0000 | [diff] [blame] | 523 | the string matching the the corresponding parenthesized group. If a |
| 524 | group number is negative or larger than the number of groups defined |
| 525 | in the pattern, an \exception{IndexError} exception is raised. |
| 526 | If a group is contained in a part of the pattern that did not match, |
| 527 | the corresponding result is \code{None}. If a group is contained in a |
| 528 | part of the pattern that matched multiple times, the last match is |
| 529 | returned. |
Guido van Rossum | 1acceb0 | 1997-08-14 23:12:18 +0000 | [diff] [blame] | 530 | |
Guido van Rossum | 0b33410 | 1997-12-08 17:33:40 +0000 | [diff] [blame] | 531 | If the regular expression uses the \code{(?P<\var{name}>...)} syntax, |
Guido van Rossum | 4650392 | 1998-01-19 23:14:17 +0000 | [diff] [blame] | 532 | the \var{groupN} arguments may also be strings identifying groups by |
Guido van Rossum | 791468f | 1998-04-03 20:07:37 +0000 | [diff] [blame] | 533 | their group name. If a string argument is not used as a group name in |
| 534 | the pattern, an \exception{IndexError} exception is raised. |
Guido van Rossum | e4eb223 | 1997-12-17 00:23:39 +0000 | [diff] [blame] | 535 | |
| 536 | A moderately complicated example: |
Fred Drake | 023f87f | 1998-01-12 19:16:24 +0000 | [diff] [blame] | 537 | |
| 538 | \begin{verbatim} |
Guido van Rossum | e4eb223 | 1997-12-17 00:23:39 +0000 | [diff] [blame] | 539 | m = re.match(r"(?P<int>\d+)\.(\d*)", '3.14') |
Fred Drake | 023f87f | 1998-01-12 19:16:24 +0000 | [diff] [blame] | 540 | \end{verbatim} |
| 541 | |
| 542 | After performing this match, \code{m.group(1)} is \code{'3'}, as is |
Guido van Rossum | 4650392 | 1998-01-19 23:14:17 +0000 | [diff] [blame] | 543 | \code{m.group('int')}, and \code{m.group(2)} is \code{'14'}. |
Fred Drake | 76547c5 | 1998-04-03 05:59:05 +0000 | [diff] [blame] | 544 | \end{methoddesc} |
Guido van Rossum | 1acceb0 | 1997-08-14 23:12:18 +0000 | [diff] [blame] | 545 | |
Fred Drake | 76547c5 | 1998-04-03 05:59:05 +0000 | [diff] [blame] | 546 | \begin{methoddesc}[MatchObject]{groups}{} |
Guido van Rossum | 48d0437 | 1997-12-11 20:19:08 +0000 | [diff] [blame] | 547 | Return a tuple containing all the subgroups of the match, from 1 up to |
| 548 | however many groups are in the pattern. Groups that did not |
Guido van Rossum | 9754639 | 1998-01-12 18:58:53 +0000 | [diff] [blame] | 549 | participate in the match have values of \code{None}. (Incompatibility |
| 550 | note: in the original Python 1.5 release, if the tuple was one element |
| 551 | long, a string would be returned instead. In later versions, a |
| 552 | singleton tuple is returned in such cases.) |
Fred Drake | 76547c5 | 1998-04-03 05:59:05 +0000 | [diff] [blame] | 553 | \end{methoddesc} |
Guido van Rossum | 48d0437 | 1997-12-11 20:19:08 +0000 | [diff] [blame] | 554 | |
Fred Drake | 76547c5 | 1998-04-03 05:59:05 +0000 | [diff] [blame] | 555 | \begin{methoddesc}[MatchObject]{start}{\optional{group}} |
Fred Drake | 013ad98 | 1998-03-08 07:38:27 +0000 | [diff] [blame] | 556 | \funcline{end}{\optional{group}} |
Guido van Rossum | e4eb223 | 1997-12-17 00:23:39 +0000 | [diff] [blame] | 557 | Return the indices of the start and end of the substring |
Guido van Rossum | 4650392 | 1998-01-19 23:14:17 +0000 | [diff] [blame] | 558 | matched by \var{group}; \var{group} defaults to zero (meaning the whole |
| 559 | matched substring). |
| 560 | Return \code{None} if \var{group} exists but |
Guido van Rossum | e4eb223 | 1997-12-17 00:23:39 +0000 | [diff] [blame] | 561 | did not contribute to the match. For a match object |
Fred Drake | 023f87f | 1998-01-12 19:16:24 +0000 | [diff] [blame] | 562 | \var{m}, and a group \var{g} that did contribute to the match, the |
| 563 | substring matched by group \var{g} (equivalent to |
| 564 | \code{\var{m}.group(\var{g})}) is |
| 565 | |
| 566 | \begin{verbatim} |
| 567 | m.string[m.start(g):m.end(g)] |
| 568 | \end{verbatim} |
| 569 | |
Guido van Rossum | e4eb223 | 1997-12-17 00:23:39 +0000 | [diff] [blame] | 570 | Note that |
| 571 | \code{m.start(\var{group})} will equal \code{m.end(\var{group})} if |
Fred Drake | 023f87f | 1998-01-12 19:16:24 +0000 | [diff] [blame] | 572 | \var{group} matched a null string. For example, after \code{\var{m} = |
| 573 | re.search('b(c?)', 'cba')}, \code{\var{m}.start(0)} is 1, |
| 574 | \code{\var{m}.end(0)} is 2, \code{\var{m}.start(1)} and |
| 575 | \code{\var{m}.end(1)} are both 2, and \code{\var{m}.start(2)} raises |
Fred Drake | 20e0196 | 1998-02-19 15:09:35 +0000 | [diff] [blame] | 576 | an \exception{IndexError} exception. |
Fred Drake | 76547c5 | 1998-04-03 05:59:05 +0000 | [diff] [blame] | 577 | \end{methoddesc} |
Guido van Rossum | e4eb223 | 1997-12-17 00:23:39 +0000 | [diff] [blame] | 578 | |
Fred Drake | 76547c5 | 1998-04-03 05:59:05 +0000 | [diff] [blame] | 579 | \begin{methoddesc}[MatchObject]{span}{\optional{group}} |
Fred Drake | 20e0196 | 1998-02-19 15:09:35 +0000 | [diff] [blame] | 580 | For \class{MatchObject} \var{m}, return the 2-tuple |
Fred Drake | 023f87f | 1998-01-12 19:16:24 +0000 | [diff] [blame] | 581 | \code{(\var{m}.start(\var{group}), \var{m}.end(\var{group}))}. |
Guido van Rossum | e4eb223 | 1997-12-17 00:23:39 +0000 | [diff] [blame] | 582 | Note that if \var{group} did not contribute to the match, this is |
Guido van Rossum | 4650392 | 1998-01-19 23:14:17 +0000 | [diff] [blame] | 583 | \code{(None, None)}. Again, \var{group} defaults to zero. |
Fred Drake | 76547c5 | 1998-04-03 05:59:05 +0000 | [diff] [blame] | 584 | \end{methoddesc} |
Guido van Rossum | e4eb223 | 1997-12-17 00:23:39 +0000 | [diff] [blame] | 585 | |
Fred Drake | 76547c5 | 1998-04-03 05:59:05 +0000 | [diff] [blame] | 586 | \begin{memberdesc}[MatchObject]{pos} |
Guido van Rossum | 0b33410 | 1997-12-08 17:33:40 +0000 | [diff] [blame] | 587 | The value of \var{pos} which was passed to the |
Fred Drake | 20e0196 | 1998-02-19 15:09:35 +0000 | [diff] [blame] | 588 | \function{search()} or \function{match()} function. This is the index into |
Fred Drake | 023f87f | 1998-01-12 19:16:24 +0000 | [diff] [blame] | 589 | the string at which the regex engine started looking for a match. |
Fred Drake | 76547c5 | 1998-04-03 05:59:05 +0000 | [diff] [blame] | 590 | \end{memberdesc} |
Guido van Rossum | 0b33410 | 1997-12-08 17:33:40 +0000 | [diff] [blame] | 591 | |
Fred Drake | 76547c5 | 1998-04-03 05:59:05 +0000 | [diff] [blame] | 592 | \begin{memberdesc}[MatchObject]{endpos} |
Guido van Rossum | 0b33410 | 1997-12-08 17:33:40 +0000 | [diff] [blame] | 593 | The value of \var{endpos} which was passed to the |
Fred Drake | 20e0196 | 1998-02-19 15:09:35 +0000 | [diff] [blame] | 594 | \function{search()} or \function{match()} function. This is the index into |
Fred Drake | 023f87f | 1998-01-12 19:16:24 +0000 | [diff] [blame] | 595 | the string beyond which the regex engine will not go. |
Fred Drake | 76547c5 | 1998-04-03 05:59:05 +0000 | [diff] [blame] | 596 | \end{memberdesc} |
Guido van Rossum | 1acceb0 | 1997-08-14 23:12:18 +0000 | [diff] [blame] | 597 | |
Fred Drake | 76547c5 | 1998-04-03 05:59:05 +0000 | [diff] [blame] | 598 | \begin{memberdesc}[MatchObject]{re} |
Fred Drake | 20e0196 | 1998-02-19 15:09:35 +0000 | [diff] [blame] | 599 | The regular expression object whose \method{match()} or |
| 600 | \method{search()} method produced this \class{MatchObject} instance. |
Fred Drake | 76547c5 | 1998-04-03 05:59:05 +0000 | [diff] [blame] | 601 | \end{memberdesc} |
Guido van Rossum | 1acceb0 | 1997-08-14 23:12:18 +0000 | [diff] [blame] | 602 | |
Fred Drake | 76547c5 | 1998-04-03 05:59:05 +0000 | [diff] [blame] | 603 | \begin{memberdesc}[MatchObject]{string} |
Fred Drake | 20e0196 | 1998-02-19 15:09:35 +0000 | [diff] [blame] | 604 | The string passed to \function{match()} or \function{search()}. |
Fred Drake | 76547c5 | 1998-04-03 05:59:05 +0000 | [diff] [blame] | 605 | \end{memberdesc} |
Guido van Rossum | 1acceb0 | 1997-08-14 23:12:18 +0000 | [diff] [blame] | 606 | |
Guido van Rossum | 1acceb0 | 1997-08-14 23:12:18 +0000 | [diff] [blame] | 607 | \begin{seealso} |
Fred Drake | f995181 | 1997-12-29 16:37:04 +0000 | [diff] [blame] | 608 | \seetext{Jeffrey Friedl, \emph{Mastering Regular Expressions}, |
Fred Drake | 023f87f | 1998-01-12 19:16:24 +0000 | [diff] [blame] | 609 | O'Reilly. The Python material in this book dates from before the |
Fred Drake | 20e0196 | 1998-02-19 15:09:35 +0000 | [diff] [blame] | 610 | \module{re} module, but it covers writing good regular expression |
Fred Drake | 023f87f | 1998-01-12 19:16:24 +0000 | [diff] [blame] | 611 | patterns in great detail.} |
Guido van Rossum | 1acceb0 | 1997-08-14 23:12:18 +0000 | [diff] [blame] | 612 | \end{seealso} |