Guido van Rossum | 1acceb0 | 1997-08-14 23:12:18 +0000 | [diff] [blame] | 1 | \section{Built-in Module \sectcode{re}} |
| 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\$]} |
| 129 | will match any of the characters 'a', 'k', 'm', or '\$'; \code{[a-z]} |
| 130 | will match any lowercase letter and \code{[a-zA-Z0-9]} matches any |
| 131 | letter or digit. Character classes such as \code{\e w} or \code {\e |
| 132 | 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] | 133 | 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] | 134 | backslash. |
Guido van Rossum | 1acceb0 | 1997-08-14 23:12:18 +0000 | [diff] [blame] | 135 | |
| 136 | Characters \emph{not} within a range can be matched by including a |
| 137 | \code{\^} as the first character of the set; \code{\^} elsewhere will |
Fred Drake | 20e0196 | 1998-02-19 15:09:35 +0000 | [diff] [blame] | 138 | simply match the \samp{\^} character. |
Guido van Rossum | 1acceb0 | 1997-08-14 23:12:18 +0000 | [diff] [blame] | 139 | % |
| 140 | \item[\code{|}]\code{A|B}, where A and B can be arbitrary REs, |
| 141 | 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] | 142 | 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] | 143 | use \code{\e|}, or enclose it inside a character class, like \code{[|]}. |
| 144 | % |
Guido van Rossum | 48d0437 | 1997-12-11 20:19:08 +0000 | [diff] [blame] | 145 | \item[\code{(...)}] Matches whatever regular expression is inside the |
| 146 | parentheses, and indicates the start and end of a group; the contents |
| 147 | of a group can be retrieved after a match has been performed, and can |
| 148 | be matched later in the string with the \code{\e \var{number}} special |
| 149 | sequence, described below. To match the literals '(' or ')', |
Guido van Rossum | 1acceb0 | 1997-08-14 23:12:18 +0000 | [diff] [blame] | 150 | use \code{\e(} or \code{\e)}, or enclose them inside a character |
| 151 | class: \code{[(] [)]}. |
| 152 | % |
Guido van Rossum | 0b33410 | 1997-12-08 17:33:40 +0000 | [diff] [blame] | 153 | \item[\code{(?...)}] This is an extension notation (a '?' following a |
| 154 | '(' is not meaningful otherwise). The first character after the '?' |
| 155 | determines what the meaning and further syntax of the construct is. |
| 156 | Following are the currently supported extensions. |
| 157 | % |
Fred Drake | 20e0196 | 1998-02-19 15:09:35 +0000 | [diff] [blame] | 158 | \item[\code{(?iLmsx)}] (One or more letters from the set \samp{i}, |
| 159 | \samp{L}, \samp{m}, \samp{s}, \samp{x}.) The group matches |
Fred Drake | 023f87f | 1998-01-12 19:16:24 +0000 | [diff] [blame] | 160 | the empty string; the letters set the corresponding flags |
Fred Drake | 20e0196 | 1998-02-19 15:09:35 +0000 | [diff] [blame] | 161 | (\constant{re.I}, \constant{re.L}, \constant{re.M}, \constant{re.S}, |
| 162 | \constant{re.X}) for the entire regular expression. This is useful if |
| 163 | you wish include the flags as part of the regular expression, instead |
| 164 | of passing a \var{flag} argument to the \function{compile()} function. |
Guido van Rossum | 0b33410 | 1997-12-08 17:33:40 +0000 | [diff] [blame] | 165 | % |
Guido van Rossum | 1acceb0 | 1997-08-14 23:12:18 +0000 | [diff] [blame] | 166 | \item[\code{(?:...)}] A non-grouping version of regular parentheses. |
| 167 | Matches whatever's inside the parentheses, but the text matched by the |
| 168 | group \emph{cannot} be retrieved after performing a match or |
| 169 | referenced later in the pattern. |
| 170 | % |
| 171 | \item[\code{(?P<\var{name}>...)}] Similar to regular parentheses, but |
| 172 | the text matched by the group is accessible via the symbolic group |
| 173 | name \var{name}. Group names must be valid Python identifiers. A |
| 174 | symbolic group is also a numbered group, just as if the group were not |
| 175 | named. So the group named 'id' in the example above can also be |
| 176 | referenced as the numbered group 1. |
| 177 | |
Guido van Rossum | 48d0437 | 1997-12-11 20:19:08 +0000 | [diff] [blame] | 178 | For example, if the pattern is |
| 179 | \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] | 180 | 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] | 181 | or \code{m.end('id')}, and also by name in pattern text |
| 182 | (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] | 183 | % |
Fred Drake | 023f87f | 1998-01-12 19:16:24 +0000 | [diff] [blame] | 184 | \item[\code{(?P=\var{name})}] Matches whatever text was matched by the |
| 185 | earlier group named \var{name}. |
Guido van Rossum | 48d0437 | 1997-12-11 20:19:08 +0000 | [diff] [blame] | 186 | % |
Fred Drake | 023f87f | 1998-01-12 19:16:24 +0000 | [diff] [blame] | 187 | \item[\code{(?\#...)}] A comment; the contents of the parentheses are |
| 188 | simply ignored. |
Guido van Rossum | 1acceb0 | 1997-08-14 23:12:18 +0000 | [diff] [blame] | 189 | % |
Fred Drake | 023f87f | 1998-01-12 19:16:24 +0000 | [diff] [blame] | 190 | \item[\code{(?=...)}] Matches if \code{...} matches next, but doesn't |
| 191 | consume any of the string. This is called a lookahead assertion. For |
| 192 | example, \code{Isaac (?=Asimov)} will match 'Isaac~' only if it's |
| 193 | followed by 'Asimov'. |
Guido van Rossum | 1acceb0 | 1997-08-14 23:12:18 +0000 | [diff] [blame] | 194 | % |
Fred Drake | 023f87f | 1998-01-12 19:16:24 +0000 | [diff] [blame] | 195 | \item[\code{(?!...)}] Matches if \code{...} doesn't match next. This |
| 196 | is a negative lookahead assertion. For example, |
| 197 | \code{Isaac (?!Asimov)} will match 'Isaac~' only if it's \emph{not} |
| 198 | followed by 'Asimov'. |
Guido van Rossum | 0b33410 | 1997-12-08 17:33:40 +0000 | [diff] [blame] | 199 | |
Fred Drake | 2705e80 | 1998-02-16 21:21:13 +0000 | [diff] [blame] | 200 | \end{list} |
Guido van Rossum | 1acceb0 | 1997-08-14 23:12:18 +0000 | [diff] [blame] | 201 | |
Fred Drake | 20e0196 | 1998-02-19 15:09:35 +0000 | [diff] [blame] | 202 | 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] | 203 | list below. If the ordinary character is not on the list, then the |
| 204 | resulting RE will match the second character. For example, |
Fred Drake | 20e0196 | 1998-02-19 15:09:35 +0000 | [diff] [blame] | 205 | \code{\e\$} matches the character \samp{\$}. |
Guido van Rossum | 1acceb0 | 1997-08-14 23:12:18 +0000 | [diff] [blame] | 206 | |
Fred Drake | 2705e80 | 1998-02-16 21:21:13 +0000 | [diff] [blame] | 207 | \begin{list}{}{\leftmargin \MyLeftMargin \labelwidth \MyLabelWidth} |
Guido van Rossum | 1acceb0 | 1997-08-14 23:12:18 +0000 | [diff] [blame] | 208 | |
| 209 | % |
| 210 | \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] | 211 | same number. Groups are numbered starting from 1. For example, |
| 212 | \code{(.+) \e 1} matches 'the the' or '55 55', but not 'the end' (note |
| 213 | the space after the group). This special sequence can only be used to |
| 214 | match one of the first 99 groups. If the first digit of \var{number} |
| 215 | is 0, or \var{number} is 3 octal digits long, it will not be interpreted |
| 216 | as a group match, but as the character with octal value \var{number}. |
Guido van Rossum | 1acceb0 | 1997-08-14 23:12:18 +0000 | [diff] [blame] | 217 | % |
| 218 | \item[\code{\e A}] Matches only at the start of the string. |
| 219 | % |
| 220 | \item[\code{\e b}] Matches the empty string, but only at the |
| 221 | beginning or end of a word. A word is defined as a sequence of |
| 222 | alphanumeric characters, so the end of a word is indicated by |
Guido van Rossum | 48d0437 | 1997-12-11 20:19:08 +0000 | [diff] [blame] | 223 | whitespace or a non-alphanumeric character. Inside a character range, |
| 224 | \code{\e b} represents the backspace character, for compatibility with |
| 225 | Python's string literals. |
Guido van Rossum | 1acceb0 | 1997-08-14 23:12:18 +0000 | [diff] [blame] | 226 | % |
Guido van Rossum | 0b33410 | 1997-12-08 17:33:40 +0000 | [diff] [blame] | 227 | \item[\code{\e B}] Matches the empty string, but only when it is |
| 228 | \emph{not} at the beginning or end of a word. |
Guido van Rossum | 1acceb0 | 1997-08-14 23:12:18 +0000 | [diff] [blame] | 229 | % |
| 230 | \item[\code{\e d}]Matches any decimal digit; this is |
| 231 | equivalent to the set \code{[0-9]}. |
| 232 | % |
| 233 | \item[\code{\e D}]Matches any non-digit character; this is |
Fred Drake | 20e0196 | 1998-02-19 15:09:35 +0000 | [diff] [blame] | 234 | equivalent to the set \code{[\^0-9]}. |
Guido van Rossum | 1acceb0 | 1997-08-14 23:12:18 +0000 | [diff] [blame] | 235 | % |
| 236 | \item[\code{\e s}]Matches any whitespace character; this is |
| 237 | equivalent to the set \code{[ \e t\e n\e r\e f\e v]}. |
| 238 | % |
| 239 | \item[\code{\e S}]Matches any non-whitespace character; this is |
Guido van Rossum | f5370f4 | 1998-02-11 22:52:47 +0000 | [diff] [blame] | 240 | 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] | 241 | % |
Fred Drake | 20e0196 | 1998-02-19 15:09:35 +0000 | [diff] [blame] | 242 | \item[\code{\e w}]When the \constant{LOCALE} flag is not specified, |
Fred Drake | 023f87f | 1998-01-12 19:16:24 +0000 | [diff] [blame] | 243 | matches any alphanumeric character; this is equivalent to the set |
Fred Drake | 20e0196 | 1998-02-19 15:09:35 +0000 | [diff] [blame] | 244 | \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] | 245 | \code{[0-9_]} plus whatever characters are defined as letters for the |
| 246 | current locale. |
Guido van Rossum | 1acceb0 | 1997-08-14 23:12:18 +0000 | [diff] [blame] | 247 | % |
Fred Drake | 20e0196 | 1998-02-19 15:09:35 +0000 | [diff] [blame] | 248 | \item[\code{\e W}]When the \constant{LOCALE} flag is not specified, |
Fred Drake | 023f87f | 1998-01-12 19:16:24 +0000 | [diff] [blame] | 249 | matches any non-alphanumeric character; this is equivalent to the set |
Fred Drake | 20e0196 | 1998-02-19 15:09:35 +0000 | [diff] [blame] | 250 | \code{[\^a-zA-Z0-9_]}. With \constant{LOCALE}, it will match any |
Fred Drake | 023f87f | 1998-01-12 19:16:24 +0000 | [diff] [blame] | 251 | 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] | 252 | for the current locale. |
Guido van Rossum | 1acceb0 | 1997-08-14 23:12:18 +0000 | [diff] [blame] | 253 | |
| 254 | \item[\code{\e Z}]Matches only at the end of the string. |
| 255 | % |
| 256 | |
| 257 | \item[\code{\e \e}] Matches a literal backslash. |
| 258 | |
Fred Drake | 2705e80 | 1998-02-16 21:21:13 +0000 | [diff] [blame] | 259 | \end{list} |
Guido van Rossum | 1acceb0 | 1997-08-14 23:12:18 +0000 | [diff] [blame] | 260 | |
| 261 | \subsection{Module Contents} |
Fred Drake | 78f8e98 | 1997-12-29 21:39:39 +0000 | [diff] [blame] | 262 | \nodename{Contents of Module re} |
Guido van Rossum | 1acceb0 | 1997-08-14 23:12:18 +0000 | [diff] [blame] | 263 | |
| 264 | The module defines the following functions and constants, and an exception: |
| 265 | |
Fred Drake | 1947991 | 1998-02-13 06:58:54 +0000 | [diff] [blame] | 266 | \setindexsubitem{(in module re)} |
Guido van Rossum | 1acceb0 | 1997-08-14 23:12:18 +0000 | [diff] [blame] | 267 | |
Fred Drake | 013ad98 | 1998-03-08 07:38:27 +0000 | [diff] [blame^] | 268 | \begin{funcdesc}{compile}{pattern\optional{, flags}} |
Guido van Rossum | 1acceb0 | 1997-08-14 23:12:18 +0000 | [diff] [blame] | 269 | Compile a regular expression pattern into a regular expression |
Fred Drake | 20e0196 | 1998-02-19 15:09:35 +0000 | [diff] [blame] | 270 | object, which can be used for matching using its \function{match()} and |
| 271 | \function{search()} methods, described below. |
Guido van Rossum | 1acceb0 | 1997-08-14 23:12:18 +0000 | [diff] [blame] | 272 | |
Guido van Rossum | 0b33410 | 1997-12-08 17:33:40 +0000 | [diff] [blame] | 273 | The expression's behaviour can be modified by specifying a |
| 274 | \var{flags} value. Values can be any of the following variables, |
| 275 | combined using bitwise OR (the \code{|} operator). |
| 276 | |
Fred Drake | 013ad98 | 1998-03-08 07:38:27 +0000 | [diff] [blame^] | 277 | \begin{datadesc}{I} |
| 278 | \dataline{IGNORECASE} |
Fred Drake | 78f8e98 | 1997-12-29 21:39:39 +0000 | [diff] [blame] | 279 | Perform case-insensitive matching; expressions like \code{[A-Z]} will match |
Guido van Rossum | 48d0437 | 1997-12-11 20:19:08 +0000 | [diff] [blame] | 280 | lowercase letters, too. This is not affected by the current locale. |
Fred Drake | 013ad98 | 1998-03-08 07:38:27 +0000 | [diff] [blame^] | 281 | \end{datadesc} |
Guido van Rossum | 0b33410 | 1997-12-08 17:33:40 +0000 | [diff] [blame] | 282 | |
Fred Drake | 013ad98 | 1998-03-08 07:38:27 +0000 | [diff] [blame^] | 283 | \begin{datadesc}{L} |
| 284 | \dataline{LOCALE} |
Fred Drake | 78f8e98 | 1997-12-29 21:39:39 +0000 | [diff] [blame] | 285 | Make \code{\e w}, \code{\e W}, \code{\e b}, |
Guido van Rossum | 48d0437 | 1997-12-11 20:19:08 +0000 | [diff] [blame] | 286 | \code{\e B}, dependent on the current locale. |
Fred Drake | 013ad98 | 1998-03-08 07:38:27 +0000 | [diff] [blame^] | 287 | \end{datadesc} |
Guido van Rossum | a42c178 | 1997-12-09 20:41:47 +0000 | [diff] [blame] | 288 | |
Fred Drake | 013ad98 | 1998-03-08 07:38:27 +0000 | [diff] [blame^] | 289 | \begin{datadesc}{M} |
| 290 | \dataline{MULTILINE} |
Fred Drake | 78f8e98 | 1997-12-29 21:39:39 +0000 | [diff] [blame] | 291 | When specified, the pattern character \code{\^} matches at the |
Fred Drake | 023f87f | 1998-01-12 19:16:24 +0000 | [diff] [blame] | 292 | beginning of the string and at the beginning of each line |
| 293 | (immediately following each newline); and the pattern character |
Guido van Rossum | 48d0437 | 1997-12-11 20:19:08 +0000 | [diff] [blame] | 294 | \code{\$} matches at the end of the string and at the end of each line |
| 295 | (immediately preceding each newline). |
Guido van Rossum | 0b33410 | 1997-12-08 17:33:40 +0000 | [diff] [blame] | 296 | By default, \code{\^} matches only at the beginning of the string, and |
| 297 | \code{\$} only at the end of the string and immediately before the |
| 298 | newline (if any) at the end of the string. |
Fred Drake | 013ad98 | 1998-03-08 07:38:27 +0000 | [diff] [blame^] | 299 | \end{datadesc} |
Guido van Rossum | 0b33410 | 1997-12-08 17:33:40 +0000 | [diff] [blame] | 300 | |
Fred Drake | 013ad98 | 1998-03-08 07:38:27 +0000 | [diff] [blame^] | 301 | \begin{datadesc}{S} |
| 302 | \dataline{DOTALL} |
Fred Drake | 78f8e98 | 1997-12-29 21:39:39 +0000 | [diff] [blame] | 303 | Make the \code{.} special character any character at all, including a |
Guido van Rossum | 48d0437 | 1997-12-11 20:19:08 +0000 | [diff] [blame] | 304 | newline; without this flag, \code{.} will match anything \emph{except} |
Fred Drake | 78f8e98 | 1997-12-29 21:39:39 +0000 | [diff] [blame] | 305 | a newline. |
Fred Drake | 013ad98 | 1998-03-08 07:38:27 +0000 | [diff] [blame^] | 306 | \end{datadesc} |
Guido van Rossum | 48d0437 | 1997-12-11 20:19:08 +0000 | [diff] [blame] | 307 | |
Fred Drake | 013ad98 | 1998-03-08 07:38:27 +0000 | [diff] [blame^] | 308 | \begin{datadesc}{X} |
| 309 | \dataline{VERBOSE} |
Fred Drake | 78f8e98 | 1997-12-29 21:39:39 +0000 | [diff] [blame] | 310 | Ignore whitespace within the pattern |
Guido van Rossum | 48d0437 | 1997-12-11 20:19:08 +0000 | [diff] [blame] | 311 | except when in a character class or preceded by an unescaped |
| 312 | backslash, and, when a line contains a \code{\#} neither in a character |
| 313 | class or preceded by an unescaped backslash, all characters from the |
Fred Drake | 78f8e98 | 1997-12-29 21:39:39 +0000 | [diff] [blame] | 314 | leftmost such \code{\#} through the end of the line are ignored. |
Fred Drake | 013ad98 | 1998-03-08 07:38:27 +0000 | [diff] [blame^] | 315 | \end{datadesc} |
Guido van Rossum | 0b33410 | 1997-12-08 17:33:40 +0000 | [diff] [blame] | 316 | |
Guido van Rossum | 0b33410 | 1997-12-08 17:33:40 +0000 | [diff] [blame] | 317 | |
Fred Drake | 78f8e98 | 1997-12-29 21:39:39 +0000 | [diff] [blame] | 318 | The sequence |
Fred Drake | 013ad98 | 1998-03-08 07:38:27 +0000 | [diff] [blame^] | 319 | |
Fred Drake | 1947991 | 1998-02-13 06:58:54 +0000 | [diff] [blame] | 320 | \begin{verbatim} |
Guido van Rossum | 1acceb0 | 1997-08-14 23:12:18 +0000 | [diff] [blame] | 321 | prog = re.compile(pat) |
| 322 | result = prog.match(str) |
Fred Drake | 1947991 | 1998-02-13 06:58:54 +0000 | [diff] [blame] | 323 | \end{verbatim} |
Fred Drake | 013ad98 | 1998-03-08 07:38:27 +0000 | [diff] [blame^] | 324 | |
Guido van Rossum | 1acceb0 | 1997-08-14 23:12:18 +0000 | [diff] [blame] | 325 | is equivalent to |
Fred Drake | 023f87f | 1998-01-12 19:16:24 +0000 | [diff] [blame] | 326 | |
| 327 | \begin{verbatim} |
Guido van Rossum | 1acceb0 | 1997-08-14 23:12:18 +0000 | [diff] [blame] | 328 | result = re.match(pat, str) |
Fred Drake | 023f87f | 1998-01-12 19:16:24 +0000 | [diff] [blame] | 329 | \end{verbatim} |
| 330 | |
Fred Drake | 20e0196 | 1998-02-19 15:09:35 +0000 | [diff] [blame] | 331 | but the version using \function{compile()} is more efficient when the |
Guido van Rossum | 48d0437 | 1997-12-11 20:19:08 +0000 | [diff] [blame] | 332 | expression will be used several times in a single program. |
Fred Drake | 20e0196 | 1998-02-19 15:09:35 +0000 | [diff] [blame] | 333 | %(The compiled version of the last pattern passed to |
| 334 | %\function{regex.match()} or \function{regex.search()} is cached, so |
| 335 | %programs that use only a single regular expression at a time needn't |
| 336 | %worry about compiling regular expressions.) |
Guido van Rossum | 1acceb0 | 1997-08-14 23:12:18 +0000 | [diff] [blame] | 337 | \end{funcdesc} |
| 338 | |
| 339 | \begin{funcdesc}{escape}{string} |
Guido van Rossum | 48d0437 | 1997-12-11 20:19:08 +0000 | [diff] [blame] | 340 | Return \var{string} with all non-alphanumerics backslashed; this is |
| 341 | useful if you want to match an arbitrary literal string that may have |
| 342 | regular expression metacharacters in it. |
Guido van Rossum | 1acceb0 | 1997-08-14 23:12:18 +0000 | [diff] [blame] | 343 | \end{funcdesc} |
| 344 | |
Fred Drake | 013ad98 | 1998-03-08 07:38:27 +0000 | [diff] [blame^] | 345 | \begin{funcdesc}{match}{pattern, string\optional{, flags}} |
Guido van Rossum | 1acceb0 | 1997-08-14 23:12:18 +0000 | [diff] [blame] | 346 | If zero or more characters at the beginning of \var{string} match |
| 347 | the regular expression \var{pattern}, return a corresponding |
Fred Drake | 20e0196 | 1998-02-19 15:09:35 +0000 | [diff] [blame] | 348 | \class{MatchObject} instance. Return \code{None} if the string does not |
Guido van Rossum | 1acceb0 | 1997-08-14 23:12:18 +0000 | [diff] [blame] | 349 | match the pattern; note that this is different from a zero-length |
| 350 | match. |
| 351 | \end{funcdesc} |
| 352 | |
Fred Drake | 013ad98 | 1998-03-08 07:38:27 +0000 | [diff] [blame^] | 353 | \begin{funcdesc}{search}{pattern, string\optional{, flags}} |
Guido van Rossum | 1acceb0 | 1997-08-14 23:12:18 +0000 | [diff] [blame] | 354 | Scan through \var{string} looking for a location where the regular |
Fred Drake | 023f87f | 1998-01-12 19:16:24 +0000 | [diff] [blame] | 355 | expression \var{pattern} produces a match, and return a |
Fred Drake | 20e0196 | 1998-02-19 15:09:35 +0000 | [diff] [blame] | 356 | corresponding \class{MatchObject} instance. |
Guido van Rossum | 0148bbf | 1997-12-22 22:41:40 +0000 | [diff] [blame] | 357 | Return \code{None} if no |
Guido van Rossum | 1acceb0 | 1997-08-14 23:12:18 +0000 | [diff] [blame] | 358 | position in the string matches the pattern; note that this is |
| 359 | different from finding a zero-length match at some point in the string. |
| 360 | \end{funcdesc} |
| 361 | |
Fred Drake | 013ad98 | 1998-03-08 07:38:27 +0000 | [diff] [blame^] | 362 | \begin{funcdesc}{split}{pattern, string, \optional{, maxsplit\code{ = 0}}} |
Guido van Rossum | 1acceb0 | 1997-08-14 23:12:18 +0000 | [diff] [blame] | 363 | Split \var{string} by the occurrences of \var{pattern}. If |
| 364 | capturing parentheses are used in pattern, then occurrences of |
| 365 | patterns or subpatterns are also returned. |
Guido van Rossum | 9754639 | 1998-01-12 18:58:53 +0000 | [diff] [blame] | 366 | If \var{maxsplit} is nonzero, at most \var{maxsplit} splits |
| 367 | occur, and the remainder of the string is returned as the final |
| 368 | element of the list. (Incompatibility note: in the original Python |
| 369 | 1.5 release, \var{maxsplit} was ignored. This has been fixed in |
| 370 | later releases.) |
Guido van Rossum | 1acceb0 | 1997-08-14 23:12:18 +0000 | [diff] [blame] | 371 | % |
Fred Drake | 1947991 | 1998-02-13 06:58:54 +0000 | [diff] [blame] | 372 | \begin{verbatim} |
Guido van Rossum | 1acceb0 | 1997-08-14 23:12:18 +0000 | [diff] [blame] | 373 | >>> re.split('[\W]+', 'Words, words, words.') |
| 374 | ['Words', 'words', 'words', ''] |
| 375 | >>> re.split('([\W]+)', 'Words, words, words.') |
| 376 | ['Words', ', ', 'words', ', ', 'words', '.', ''] |
Guido van Rossum | 9754639 | 1998-01-12 18:58:53 +0000 | [diff] [blame] | 377 | >>> re.split('[\W]+', 'Words, words, words.', 1) |
| 378 | ['Words', 'words, words.'] |
Fred Drake | 1947991 | 1998-02-13 06:58:54 +0000 | [diff] [blame] | 379 | \end{verbatim} |
Guido van Rossum | 1acceb0 | 1997-08-14 23:12:18 +0000 | [diff] [blame] | 380 | % |
| 381 | This function combines and extends the functionality of |
Fred Drake | 20e0196 | 1998-02-19 15:09:35 +0000 | [diff] [blame] | 382 | the old \function{regsub.split()} and \function{regsub.splitx()}. |
Guido van Rossum | 1acceb0 | 1997-08-14 23:12:18 +0000 | [diff] [blame] | 383 | \end{funcdesc} |
| 384 | |
Fred Drake | 013ad98 | 1998-03-08 07:38:27 +0000 | [diff] [blame^] | 385 | \begin{funcdesc}{sub}{pattern, repl, string\optional{, count\code{ = 0}}} |
Guido van Rossum | 1acceb0 | 1997-08-14 23:12:18 +0000 | [diff] [blame] | 386 | Return the string obtained by replacing the leftmost non-overlapping |
| 387 | occurrences of \var{pattern} in \var{string} by the replacement |
Barry Warsaw | 4552f3d | 1997-11-20 00:15:13 +0000 | [diff] [blame] | 388 | \var{repl}. If the pattern isn't found, \var{string} is returned |
| 389 | unchanged. \var{repl} can be a string or a function; if a function, |
| 390 | it is called for every non-overlapping occurance of \var{pattern}. |
Guido van Rossum | 0b33410 | 1997-12-08 17:33:40 +0000 | [diff] [blame] | 391 | The function takes a single match object argument, and returns the |
| 392 | replacement string. For example: |
Barry Warsaw | 4552f3d | 1997-11-20 00:15:13 +0000 | [diff] [blame] | 393 | % |
Fred Drake | 1947991 | 1998-02-13 06:58:54 +0000 | [diff] [blame] | 394 | \begin{verbatim} |
Barry Warsaw | 4552f3d | 1997-11-20 00:15:13 +0000 | [diff] [blame] | 395 | >>> def dashrepl(matchobj): |
| 396 | ... if matchobj.group(0) == '-': return ' ' |
| 397 | ... else: return '-' |
| 398 | >>> re.sub('-{1,2}', dashrepl, 'pro----gram-files') |
| 399 | 'pro--gram files' |
Fred Drake | 1947991 | 1998-02-13 06:58:54 +0000 | [diff] [blame] | 400 | \end{verbatim} |
Barry Warsaw | 4552f3d | 1997-11-20 00:15:13 +0000 | [diff] [blame] | 401 | % |
Guido van Rossum | 0b33410 | 1997-12-08 17:33:40 +0000 | [diff] [blame] | 402 | The pattern may be a string or a |
Guido van Rossum | 48d0437 | 1997-12-11 20:19:08 +0000 | [diff] [blame] | 403 | regex object; if you need to specify |
| 404 | regular expression flags, you must use a regex object, or use |
| 405 | embedded modifiers in a pattern; e.g. |
Fred Drake | 013ad98 | 1998-03-08 07:38:27 +0000 | [diff] [blame^] | 406 | \samp{sub("(?i)b+", "x", "bbbb BBBB")} returns \code{'x x'}. |
Fred Drake | 023f87f | 1998-01-12 19:16:24 +0000 | [diff] [blame] | 407 | |
Guido van Rossum | 1acceb0 | 1997-08-14 23:12:18 +0000 | [diff] [blame] | 408 | The optional argument \var{count} is the maximum number of pattern |
| 409 | occurrences to be replaced; count must be a non-negative integer, and |
| 410 | the default value of 0 means to replace all occurrences. |
| 411 | |
| 412 | 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^] | 413 | previous match, so \samp{sub('x*', '-', 'abc')} returns \code{'-a-b-c-'}. |
Guido van Rossum | 1acceb0 | 1997-08-14 23:12:18 +0000 | [diff] [blame] | 414 | \end{funcdesc} |
| 415 | |
Fred Drake | 013ad98 | 1998-03-08 07:38:27 +0000 | [diff] [blame^] | 416 | \begin{funcdesc}{subn}{pattern, repl, string\optional{, count\code{ = 0}}} |
Fred Drake | 20e0196 | 1998-02-19 15:09:35 +0000 | [diff] [blame] | 417 | Perform the same operation as \function{sub()}, but return a tuple |
Fred Drake | 023f87f | 1998-01-12 19:16:24 +0000 | [diff] [blame] | 418 | \code{(\var{new_string}, \var{number_of_subs_made})}. |
Guido van Rossum | 1acceb0 | 1997-08-14 23:12:18 +0000 | [diff] [blame] | 419 | \end{funcdesc} |
| 420 | |
| 421 | \begin{excdesc}{error} |
| 422 | Exception raised when a string passed to one of the functions here |
| 423 | is not a valid regular expression (e.g., unmatched parentheses) or |
Fred Drake | 013ad98 | 1998-03-08 07:38:27 +0000 | [diff] [blame^] | 424 | when some other error occurs during compilation or matching. It is |
| 425 | 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] | 426 | \end{excdesc} |
| 427 | |
| 428 | \subsection{Regular Expression Objects} |
| 429 | Compiled regular expression objects support the following methods and |
| 430 | attributes: |
| 431 | |
Fred Drake | 1947991 | 1998-02-13 06:58:54 +0000 | [diff] [blame] | 432 | \setindexsubitem{(re method)} |
Fred Drake | 013ad98 | 1998-03-08 07:38:27 +0000 | [diff] [blame^] | 433 | \begin{funcdesc}{match}{string\optional{, pos}\optional{, endpos}} |
Guido van Rossum | eb53ae4 | 1997-10-05 18:54:07 +0000 | [diff] [blame] | 434 | If zero or more characters at the beginning of \var{string} match |
| 435 | this regular expression, return a corresponding |
Fred Drake | 20e0196 | 1998-02-19 15:09:35 +0000 | [diff] [blame] | 436 | \class{MatchObject} instance. Return \code{None} if the string does not |
Guido van Rossum | eb53ae4 | 1997-10-05 18:54:07 +0000 | [diff] [blame] | 437 | match the pattern; note that this is different from a zero-length |
| 438 | match. |
Guido van Rossum | 1acceb0 | 1997-08-14 23:12:18 +0000 | [diff] [blame] | 439 | |
| 440 | 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] | 441 | where the search is to start; it defaults to \code{0}. The |
Fred Drake | 20e0196 | 1998-02-19 15:09:35 +0000 | [diff] [blame] | 442 | \samp{\^} pattern character will match at the index where the |
Guido van Rossum | 48d0437 | 1997-12-11 20:19:08 +0000 | [diff] [blame] | 443 | search is to start. |
Guido van Rossum | 0b33410 | 1997-12-08 17:33:40 +0000 | [diff] [blame] | 444 | |
| 445 | The optional parameter \var{endpos} limits how far the string will |
| 446 | be searched; it will be as if the string is \var{endpos} characters |
| 447 | long, so only the characters from \var{pos} to \var{endpos} will be |
| 448 | searched for a match. |
Guido van Rossum | 1acceb0 | 1997-08-14 23:12:18 +0000 | [diff] [blame] | 449 | \end{funcdesc} |
| 450 | |
Fred Drake | 013ad98 | 1998-03-08 07:38:27 +0000 | [diff] [blame^] | 451 | \begin{funcdesc}{search}{string\optional{, pos}\optional{, endpos}} |
Guido van Rossum | eb53ae4 | 1997-10-05 18:54:07 +0000 | [diff] [blame] | 452 | Scan through \var{string} looking for a location where this regular |
| 453 | expression produces a match. Return \code{None} if no |
| 454 | position in the string matches the pattern; note that this is |
| 455 | 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] | 456 | |
Guido van Rossum | 48d0437 | 1997-12-11 20:19:08 +0000 | [diff] [blame] | 457 | The optional \var{pos} and \var{endpos} parameters have the same |
Fred Drake | 20e0196 | 1998-02-19 15:09:35 +0000 | [diff] [blame] | 458 | meaning as for the \method{match()} method. |
Guido van Rossum | 1acceb0 | 1997-08-14 23:12:18 +0000 | [diff] [blame] | 459 | \end{funcdesc} |
| 460 | |
Fred Drake | 013ad98 | 1998-03-08 07:38:27 +0000 | [diff] [blame^] | 461 | \begin{funcdesc}{split}{string, \optional{, maxsplit\code{ = 0}}} |
Fred Drake | 20e0196 | 1998-02-19 15:09:35 +0000 | [diff] [blame] | 462 | Identical to the \function{split()} function, using the compiled pattern. |
Guido van Rossum | 1acceb0 | 1997-08-14 23:12:18 +0000 | [diff] [blame] | 463 | \end{funcdesc} |
| 464 | |
Fred Drake | 013ad98 | 1998-03-08 07:38:27 +0000 | [diff] [blame^] | 465 | \begin{funcdesc}{sub}{repl, string\optional{, count\code{ = 0}}} |
Fred Drake | 20e0196 | 1998-02-19 15:09:35 +0000 | [diff] [blame] | 466 | Identical to the \function{sub()} function, using the compiled pattern. |
Guido van Rossum | 1acceb0 | 1997-08-14 23:12:18 +0000 | [diff] [blame] | 467 | \end{funcdesc} |
| 468 | |
Fred Drake | 013ad98 | 1998-03-08 07:38:27 +0000 | [diff] [blame^] | 469 | \begin{funcdesc}{subn}{repl, string\optional{, count\code{ = 0}}} |
Fred Drake | 20e0196 | 1998-02-19 15:09:35 +0000 | [diff] [blame] | 470 | Identical to the \function{subn()} function, using the compiled pattern. |
Guido van Rossum | 1acceb0 | 1997-08-14 23:12:18 +0000 | [diff] [blame] | 471 | \end{funcdesc} |
| 472 | |
Fred Drake | 1947991 | 1998-02-13 06:58:54 +0000 | [diff] [blame] | 473 | \setindexsubitem{(regex attribute)} |
Guido van Rossum | 1acceb0 | 1997-08-14 23:12:18 +0000 | [diff] [blame] | 474 | |
| 475 | \begin{datadesc}{flags} |
Fred Drake | 013ad98 | 1998-03-08 07:38:27 +0000 | [diff] [blame^] | 476 | The flags argument used when the regex object was compiled, or |
| 477 | \code{0} if no flags were provided. |
Guido van Rossum | 1acceb0 | 1997-08-14 23:12:18 +0000 | [diff] [blame] | 478 | \end{datadesc} |
| 479 | |
| 480 | \begin{datadesc}{groupindex} |
Fred Drake | 013ad98 | 1998-03-08 07:38:27 +0000 | [diff] [blame^] | 481 | A dictionary mapping any symbolic group names defined by |
| 482 | \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] | 483 | symbolic groups were used in the pattern. |
| 484 | \end{datadesc} |
| 485 | |
| 486 | \begin{datadesc}{pattern} |
| 487 | The pattern string from which the regex object was compiled. |
| 488 | \end{datadesc} |
| 489 | |
Fred Drake | 023f87f | 1998-01-12 19:16:24 +0000 | [diff] [blame] | 490 | \subsection{Match Objects} |
| 491 | |
Fred Drake | 20e0196 | 1998-02-19 15:09:35 +0000 | [diff] [blame] | 492 | \class{MatchObject} instances support the following methods and attributes: |
Guido van Rossum | 1acceb0 | 1997-08-14 23:12:18 +0000 | [diff] [blame] | 493 | |
Guido van Rossum | 4650392 | 1998-01-19 23:14:17 +0000 | [diff] [blame] | 494 | \begin{funcdesc}{group}{\optional{group1, group2, ...}} |
| 495 | Returns one or more subgroups of the match. If there is a single |
| 496 | argument, the result is a single string; if there are |
Guido van Rossum | 48d0437 | 1997-12-11 20:19:08 +0000 | [diff] [blame] | 497 | 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] | 498 | Without arguments, \var{group1} defaults to zero (i.e. the whole match |
| 499 | is returned). |
| 500 | 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] | 501 | entire matching string; if it is in the inclusive range [1..99], it is |
| 502 | the string matching the the corresponding parenthesized group. If no |
| 503 | such group exists, the corresponding result is |
| 504 | \code{None}. |
Guido van Rossum | 1acceb0 | 1997-08-14 23:12:18 +0000 | [diff] [blame] | 505 | |
Guido van Rossum | 0b33410 | 1997-12-08 17:33:40 +0000 | [diff] [blame] | 506 | If the regular expression uses the \code{(?P<\var{name}>...)} syntax, |
Guido van Rossum | 4650392 | 1998-01-19 23:14:17 +0000 | [diff] [blame] | 507 | the \var{groupN} arguments may also be strings identifying groups by |
Guido van Rossum | 0b33410 | 1997-12-08 17:33:40 +0000 | [diff] [blame] | 508 | their group name. |
Guido van Rossum | e4eb223 | 1997-12-17 00:23:39 +0000 | [diff] [blame] | 509 | |
| 510 | A moderately complicated example: |
Fred Drake | 023f87f | 1998-01-12 19:16:24 +0000 | [diff] [blame] | 511 | |
| 512 | \begin{verbatim} |
Guido van Rossum | e4eb223 | 1997-12-17 00:23:39 +0000 | [diff] [blame] | 513 | m = re.match(r"(?P<int>\d+)\.(\d*)", '3.14') |
Fred Drake | 023f87f | 1998-01-12 19:16:24 +0000 | [diff] [blame] | 514 | \end{verbatim} |
| 515 | |
| 516 | 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] | 517 | \code{m.group('int')}, and \code{m.group(2)} is \code{'14'}. |
Guido van Rossum | 1acceb0 | 1997-08-14 23:12:18 +0000 | [diff] [blame] | 518 | \end{funcdesc} |
| 519 | |
Guido van Rossum | 48d0437 | 1997-12-11 20:19:08 +0000 | [diff] [blame] | 520 | \begin{funcdesc}{groups}{} |
| 521 | Return a tuple containing all the subgroups of the match, from 1 up to |
| 522 | however many groups are in the pattern. Groups that did not |
Guido van Rossum | 9754639 | 1998-01-12 18:58:53 +0000 | [diff] [blame] | 523 | participate in the match have values of \code{None}. (Incompatibility |
| 524 | note: in the original Python 1.5 release, if the tuple was one element |
| 525 | long, a string would be returned instead. In later versions, a |
| 526 | singleton tuple is returned in such cases.) |
Guido van Rossum | 48d0437 | 1997-12-11 20:19:08 +0000 | [diff] [blame] | 527 | \end{funcdesc} |
| 528 | |
Guido van Rossum | 4650392 | 1998-01-19 23:14:17 +0000 | [diff] [blame] | 529 | \begin{funcdesc}{start}{\optional{group}} |
Fred Drake | 013ad98 | 1998-03-08 07:38:27 +0000 | [diff] [blame^] | 530 | \funcline{end}{\optional{group}} |
Guido van Rossum | e4eb223 | 1997-12-17 00:23:39 +0000 | [diff] [blame] | 531 | Return the indices of the start and end of the substring |
Guido van Rossum | 4650392 | 1998-01-19 23:14:17 +0000 | [diff] [blame] | 532 | matched by \var{group}; \var{group} defaults to zero (meaning the whole |
| 533 | matched substring). |
| 534 | Return \code{None} if \var{group} exists but |
Guido van Rossum | e4eb223 | 1997-12-17 00:23:39 +0000 | [diff] [blame] | 535 | did not contribute to the match. For a match object |
Fred Drake | 023f87f | 1998-01-12 19:16:24 +0000 | [diff] [blame] | 536 | \var{m}, and a group \var{g} that did contribute to the match, the |
| 537 | substring matched by group \var{g} (equivalent to |
| 538 | \code{\var{m}.group(\var{g})}) is |
| 539 | |
| 540 | \begin{verbatim} |
| 541 | m.string[m.start(g):m.end(g)] |
| 542 | \end{verbatim} |
| 543 | |
Guido van Rossum | e4eb223 | 1997-12-17 00:23:39 +0000 | [diff] [blame] | 544 | Note that |
| 545 | \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] | 546 | \var{group} matched a null string. For example, after \code{\var{m} = |
| 547 | re.search('b(c?)', 'cba')}, \code{\var{m}.start(0)} is 1, |
| 548 | \code{\var{m}.end(0)} is 2, \code{\var{m}.start(1)} and |
| 549 | \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] | 550 | an \exception{IndexError} exception. |
Guido van Rossum | e4eb223 | 1997-12-17 00:23:39 +0000 | [diff] [blame] | 551 | |
| 552 | \end{funcdesc} |
| 553 | |
Guido van Rossum | 4650392 | 1998-01-19 23:14:17 +0000 | [diff] [blame] | 554 | \begin{funcdesc}{span}{\optional{group}} |
Fred Drake | 20e0196 | 1998-02-19 15:09:35 +0000 | [diff] [blame] | 555 | For \class{MatchObject} \var{m}, return the 2-tuple |
Fred Drake | 023f87f | 1998-01-12 19:16:24 +0000 | [diff] [blame] | 556 | \code{(\var{m}.start(\var{group}), \var{m}.end(\var{group}))}. |
Guido van Rossum | e4eb223 | 1997-12-17 00:23:39 +0000 | [diff] [blame] | 557 | 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] | 558 | \code{(None, None)}. Again, \var{group} defaults to zero. |
Guido van Rossum | e4eb223 | 1997-12-17 00:23:39 +0000 | [diff] [blame] | 559 | \end{funcdesc} |
| 560 | |
Guido van Rossum | 1acceb0 | 1997-08-14 23:12:18 +0000 | [diff] [blame] | 561 | \begin{datadesc}{pos} |
Guido van Rossum | 0b33410 | 1997-12-08 17:33:40 +0000 | [diff] [blame] | 562 | The value of \var{pos} which was passed to the |
Fred Drake | 20e0196 | 1998-02-19 15:09:35 +0000 | [diff] [blame] | 563 | \function{search()} or \function{match()} function. This is the index into |
Fred Drake | 023f87f | 1998-01-12 19:16:24 +0000 | [diff] [blame] | 564 | the string at which the regex engine started looking for a match. |
Guido van Rossum | 0b33410 | 1997-12-08 17:33:40 +0000 | [diff] [blame] | 565 | \end{datadesc} |
| 566 | |
| 567 | \begin{datadesc}{endpos} |
| 568 | The value of \var{endpos} which was passed to the |
Fred Drake | 20e0196 | 1998-02-19 15:09:35 +0000 | [diff] [blame] | 569 | \function{search()} or \function{match()} function. This is the index into |
Fred Drake | 023f87f | 1998-01-12 19:16:24 +0000 | [diff] [blame] | 570 | the string beyond which the regex engine will not go. |
Guido van Rossum | 1acceb0 | 1997-08-14 23:12:18 +0000 | [diff] [blame] | 571 | \end{datadesc} |
| 572 | |
| 573 | \begin{datadesc}{re} |
Fred Drake | 20e0196 | 1998-02-19 15:09:35 +0000 | [diff] [blame] | 574 | The regular expression object whose \method{match()} or |
| 575 | \method{search()} method produced this \class{MatchObject} instance. |
Guido van Rossum | 1acceb0 | 1997-08-14 23:12:18 +0000 | [diff] [blame] | 576 | \end{datadesc} |
| 577 | |
| 578 | \begin{datadesc}{string} |
Fred Drake | 20e0196 | 1998-02-19 15:09:35 +0000 | [diff] [blame] | 579 | The string passed to \function{match()} or \function{search()}. |
Guido van Rossum | 1acceb0 | 1997-08-14 23:12:18 +0000 | [diff] [blame] | 580 | \end{datadesc} |
| 581 | |
Guido van Rossum | 1acceb0 | 1997-08-14 23:12:18 +0000 | [diff] [blame] | 582 | \begin{seealso} |
Fred Drake | f995181 | 1997-12-29 16:37:04 +0000 | [diff] [blame] | 583 | \seetext{Jeffrey Friedl, \emph{Mastering Regular Expressions}, |
Fred Drake | 023f87f | 1998-01-12 19:16:24 +0000 | [diff] [blame] | 584 | O'Reilly. The Python material in this book dates from before the |
Fred Drake | 20e0196 | 1998-02-19 15:09:35 +0000 | [diff] [blame] | 585 | \module{re} module, but it covers writing good regular expression |
Fred Drake | 023f87f | 1998-01-12 19:16:24 +0000 | [diff] [blame] | 586 | patterns in great detail.} |
Guido van Rossum | 1acceb0 | 1997-08-14 23:12:18 +0000 | [diff] [blame] | 587 | \end{seealso} |