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