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