Guido van Rossum | 7627c0d | 2000-03-31 14:58:54 +0000 | [diff] [blame] | 1 | # |
| 2 | # Secret Labs' Regular Expression Engine |
Guido van Rossum | 7627c0d | 2000-03-31 14:58:54 +0000 | [diff] [blame] | 3 | # |
| 4 | # re-compatible interface for the sre matching engine |
| 5 | # |
Fredrik Lundh | 770617b | 2001-01-14 15:06:11 +0000 | [diff] [blame] | 6 | # Copyright (c) 1998-2001 by Secret Labs AB. All rights reserved. |
Guido van Rossum | 7627c0d | 2000-03-31 14:58:54 +0000 | [diff] [blame] | 7 | # |
Fredrik Lundh | 29c4ba9 | 2000-08-01 18:20:07 +0000 | [diff] [blame] | 8 | # This version of the SRE library can be redistributed under CNRI's |
| 9 | # Python 1.6 license. For any other use, please contact Secret Labs |
| 10 | # AB (info@pythonware.com). |
| 11 | # |
Guido van Rossum | 7627c0d | 2000-03-31 14:58:54 +0000 | [diff] [blame] | 12 | # Portions of this engine have been developed in cooperation with |
Fredrik Lundh | 29c4ba9 | 2000-08-01 18:20:07 +0000 | [diff] [blame] | 13 | # CNRI. Hewlett-Packard provided funding for 1.6 integration and |
Guido van Rossum | 7627c0d | 2000-03-31 14:58:54 +0000 | [diff] [blame] | 14 | # other compatibility work. |
| 15 | # |
| 16 | |
Fred Drake | 9f5b822 | 2001-09-04 19:20:06 +0000 | [diff] [blame] | 17 | r"""Support for regular expressions (RE). |
Fred Drake | b8f2274 | 2001-09-04 19:10:20 +0000 | [diff] [blame] | 18 | |
| 19 | This module provides regular expression matching operations similar to |
| 20 | those found in Perl. It's 8-bit clean: the strings being processed may |
| 21 | contain both null bytes and characters whose high bit is set. Regular |
| 22 | expression pattern strings may not contain null bytes, but can specify |
| 23 | the null byte using the \\number notation. Characters with the high |
| 24 | bit set may be included. |
| 25 | |
| 26 | Regular expressions can contain both special and ordinary |
| 27 | characters. Most ordinary characters, like "A", "a", or "0", are the |
| 28 | simplest regular expressions; they simply match themselves. You can |
| 29 | concatenate ordinary characters, so last matches the string 'last'. |
| 30 | |
| 31 | The special characters are: |
| 32 | "." Matches any character except a newline. |
| 33 | "^" Matches the start of the string. |
| 34 | "$" Matches the end of the string. |
| 35 | "*" Matches 0 or more (greedy) repetitions of the preceding RE. |
| 36 | Greedy means that it will match as many repetitions as possible. |
| 37 | "+" Matches 1 or more (greedy) repetitions of the preceding RE. |
| 38 | "?" Matches 0 or 1 (greedy) of the preceding RE. |
| 39 | *?,+?,?? Non-greedy versions of the previous three special characters. |
| 40 | {m,n} Matches from m to n repetitions of the preceding RE. |
| 41 | {m,n}? Non-greedy version of the above. |
| 42 | "\\" Either escapes special characters or signals a special sequence. |
| 43 | [] Indicates a set of characters. |
| 44 | A "^" as the first character indicates a complementing set. |
| 45 | "|" A|B, creates an RE that will match either A or B. |
| 46 | (...) Matches the RE inside the parentheses. |
| 47 | The contents can be retrieved or matched later in the string. |
Fredrik Lundh | 21009b9 | 2001-09-18 18:47:09 +0000 | [diff] [blame] | 48 | (?iLmsx) Set the I, L, M, S, or X flag for the RE (see below). |
Fred Drake | b8f2274 | 2001-09-04 19:10:20 +0000 | [diff] [blame] | 49 | (?:...) Non-grouping version of regular parentheses. |
| 50 | (?P<name>...) The substring matched by the group is accessible by name. |
| 51 | (?P=name) Matches the text matched earlier by the group named name. |
| 52 | (?#...) A comment; ignored. |
| 53 | (?=...) Matches if ... matches next, but doesn't consume the string. |
| 54 | (?!...) Matches if ... doesn't match next. |
| 55 | |
| 56 | The special sequences consist of "\\" and a character from the list |
| 57 | below. If the ordinary character is not on the list, then the |
| 58 | resulting RE will match the second character. |
Fred Drake | 9f5b822 | 2001-09-04 19:20:06 +0000 | [diff] [blame] | 59 | \number Matches the contents of the group of the same number. |
| 60 | \A Matches only at the start of the string. |
| 61 | \Z Matches only at the end of the string. |
| 62 | \b Matches the empty string, but only at the start or end of a word. |
| 63 | \B Matches the empty string, but not at the start or end of a word. |
| 64 | \d Matches any decimal digit; equivalent to the set [0-9]. |
| 65 | \D Matches any non-digit character; equivalent to the set [^0-9]. |
| 66 | \s Matches any whitespace character; equivalent to [ \t\n\r\f\v]. |
| 67 | \S Matches any non-whitespace character; equiv. to [^ \t\n\r\f\v]. |
| 68 | \w Matches any alphanumeric character; equivalent to [a-zA-Z0-9_]. |
Fred Drake | b8f2274 | 2001-09-04 19:10:20 +0000 | [diff] [blame] | 69 | With LOCALE, it will match the set [0-9_] plus characters defined |
| 70 | as letters for the current locale. |
Fred Drake | 9f5b822 | 2001-09-04 19:20:06 +0000 | [diff] [blame] | 71 | \W Matches the complement of \w. |
| 72 | \\ Matches a literal backslash. |
Fred Drake | b8f2274 | 2001-09-04 19:10:20 +0000 | [diff] [blame] | 73 | |
| 74 | This module exports the following functions: |
| 75 | match Match a regular expression pattern to the beginning of a string. |
| 76 | search Search a string for the presence of a pattern. |
| 77 | sub Substitute occurrences of a pattern found in a string. |
| 78 | subn Same as sub, but also return the number of substitutions made. |
| 79 | split Split a string by the occurrences of a pattern. |
| 80 | findall Find all occurrences of a pattern in a string. |
| 81 | compile Compile a pattern into a RegexObject. |
| 82 | purge Clear the regular expression cache. |
Fred Drake | b8f2274 | 2001-09-04 19:10:20 +0000 | [diff] [blame] | 83 | escape Backslash all non-alphanumerics in a string. |
| 84 | |
| 85 | Some of the functions in this module takes flags as optional parameters: |
| 86 | I IGNORECASE Perform case-insensitive matching. |
| 87 | L LOCALE Make \w, \W, \b, \B, dependent on the current locale. |
| 88 | M MULTILINE "^" matches the beginning of lines as well as the string. |
| 89 | "$" matches the end of lines as well as the string. |
| 90 | S DOTALL "." matches any character at all, including the newline. |
| 91 | X VERBOSE Ignore whitespace and comments for nicer looking RE's. |
Fredrik Lundh | 21009b9 | 2001-09-18 18:47:09 +0000 | [diff] [blame] | 92 | U UNICODE Make \w, \W, \b, \B, dependent on the Unicode locale. |
Fred Drake | b8f2274 | 2001-09-04 19:10:20 +0000 | [diff] [blame] | 93 | |
| 94 | This module also defines an exception 'error'. |
| 95 | |
| 96 | """ |
Fredrik Lundh | 21009b9 | 2001-09-18 18:47:09 +0000 | [diff] [blame] | 97 | |
Guido van Rossum | 7627c0d | 2000-03-31 14:58:54 +0000 | [diff] [blame] | 98 | import sre_compile |
Fredrik Lundh | 436c3d5 | 2000-06-29 08:58:44 +0000 | [diff] [blame] | 99 | import sre_parse |
Guido van Rossum | 7627c0d | 2000-03-31 14:58:54 +0000 | [diff] [blame] | 100 | |
Fredrik Lundh | f2989b2 | 2001-02-18 12:05:16 +0000 | [diff] [blame] | 101 | # public symbols |
| 102 | __all__ = [ "match", "search", "sub", "subn", "split", "findall", |
| 103 | "compile", "purge", "template", "escape", "I", "L", "M", "S", "X", |
| 104 | "U", "IGNORECASE", "LOCALE", "MULTILINE", "DOTALL", "VERBOSE", |
| 105 | "UNICODE", "error" ] |
| 106 | |
Fredrik Lundh | 397a654 | 2001-10-18 19:30:16 +0000 | [diff] [blame] | 107 | __version__ = "2.2.0" |
Fredrik Lundh | b25e1ad | 2001-03-22 15:50:10 +0000 | [diff] [blame] | 108 | |
Fredrik Lundh | f2989b2 | 2001-02-18 12:05:16 +0000 | [diff] [blame] | 109 | # this module works under 1.5.2 and later. don't use string methods |
| 110 | import string |
Skip Montanaro | 0de6580 | 2001-02-15 22:15:14 +0000 | [diff] [blame] | 111 | |
Jeremy Hylton | b1aa195 | 2000-06-01 17:39:12 +0000 | [diff] [blame] | 112 | # flags |
Fredrik Lundh | 770617b | 2001-01-14 15:06:11 +0000 | [diff] [blame] | 113 | I = IGNORECASE = sre_compile.SRE_FLAG_IGNORECASE # ignore case |
| 114 | L = LOCALE = sre_compile.SRE_FLAG_LOCALE # assume current 8-bit locale |
| 115 | U = UNICODE = sre_compile.SRE_FLAG_UNICODE # assume unicode locale |
| 116 | M = MULTILINE = sre_compile.SRE_FLAG_MULTILINE # make anchors look for newline |
| 117 | S = DOTALL = sre_compile.SRE_FLAG_DOTALL # make dot match newline |
| 118 | X = VERBOSE = sre_compile.SRE_FLAG_VERBOSE # ignore whitespace and comments |
Jeremy Hylton | b1aa195 | 2000-06-01 17:39:12 +0000 | [diff] [blame] | 119 | |
Fredrik Lundh | 770617b | 2001-01-14 15:06:11 +0000 | [diff] [blame] | 120 | # sre extensions (experimental, don't rely on these) |
| 121 | T = TEMPLATE = sre_compile.SRE_FLAG_TEMPLATE # disable backtracking |
| 122 | DEBUG = sre_compile.SRE_FLAG_DEBUG # dump pattern after compilation |
Fredrik Lundh | 436c3d5 | 2000-06-29 08:58:44 +0000 | [diff] [blame] | 123 | |
| 124 | # sre exception |
Fredrik Lundh | be2211e | 2000-06-29 16:57:40 +0000 | [diff] [blame] | 125 | error = sre_compile.error |
Fredrik Lundh | 436c3d5 | 2000-06-29 08:58:44 +0000 | [diff] [blame] | 126 | |
Guido van Rossum | 7627c0d | 2000-03-31 14:58:54 +0000 | [diff] [blame] | 127 | # -------------------------------------------------------------------- |
| 128 | # public interface |
| 129 | |
Guido van Rossum | 7627c0d | 2000-03-31 14:58:54 +0000 | [diff] [blame] | 130 | def match(pattern, string, flags=0): |
Fredrik Lundh | 770617b | 2001-01-14 15:06:11 +0000 | [diff] [blame] | 131 | """Try to apply the pattern at the start of the string, returning |
| 132 | a match object, or None if no match was found.""" |
Jeremy Hylton | b1aa195 | 2000-06-01 17:39:12 +0000 | [diff] [blame] | 133 | return _compile(pattern, flags).match(string) |
Guido van Rossum | 7627c0d | 2000-03-31 14:58:54 +0000 | [diff] [blame] | 134 | |
| 135 | def search(pattern, string, flags=0): |
Fredrik Lundh | 770617b | 2001-01-14 15:06:11 +0000 | [diff] [blame] | 136 | """Scan through string looking for a match to the pattern, returning |
| 137 | a match object, or None if no match was found.""" |
Jeremy Hylton | b1aa195 | 2000-06-01 17:39:12 +0000 | [diff] [blame] | 138 | return _compile(pattern, flags).search(string) |
Guido van Rossum | 7627c0d | 2000-03-31 14:58:54 +0000 | [diff] [blame] | 139 | |
Jeremy Hylton | b1aa195 | 2000-06-01 17:39:12 +0000 | [diff] [blame] | 140 | def sub(pattern, repl, string, count=0): |
Fredrik Lundh | 770617b | 2001-01-14 15:06:11 +0000 | [diff] [blame] | 141 | """Return the string obtained by replacing the leftmost |
| 142 | non-overlapping occurrences of the pattern in string by the |
| 143 | replacement repl""" |
Fredrik Lundh | 7898c3e | 2000-08-07 20:59:04 +0000 | [diff] [blame] | 144 | return _compile(pattern, 0).sub(repl, string, count) |
Jeremy Hylton | b1aa195 | 2000-06-01 17:39:12 +0000 | [diff] [blame] | 145 | |
| 146 | def subn(pattern, repl, string, count=0): |
Fredrik Lundh | 770617b | 2001-01-14 15:06:11 +0000 | [diff] [blame] | 147 | """Return a 2-tuple containing (new_string, number). |
| 148 | new_string is the string obtained by replacing the leftmost |
| 149 | non-overlapping occurrences of the pattern in the source |
| 150 | string by the replacement repl. number is the number of |
| 151 | substitutions that were made.""" |
Fredrik Lundh | 7898c3e | 2000-08-07 20:59:04 +0000 | [diff] [blame] | 152 | return _compile(pattern, 0).subn(repl, string, count) |
Jeremy Hylton | b1aa195 | 2000-06-01 17:39:12 +0000 | [diff] [blame] | 153 | |
| 154 | def split(pattern, string, maxsplit=0): |
Fredrik Lundh | 770617b | 2001-01-14 15:06:11 +0000 | [diff] [blame] | 155 | """Split the source string by the occurrences of the pattern, |
| 156 | returning a list containing the resulting substrings.""" |
Fredrik Lundh | 7898c3e | 2000-08-07 20:59:04 +0000 | [diff] [blame] | 157 | return _compile(pattern, 0).split(string, maxsplit) |
Jeremy Hylton | b1aa195 | 2000-06-01 17:39:12 +0000 | [diff] [blame] | 158 | |
Fredrik Lundh | e06cbb8 | 2001-07-06 20:56:10 +0000 | [diff] [blame] | 159 | def findall(pattern, string): |
Fredrik Lundh | 770617b | 2001-01-14 15:06:11 +0000 | [diff] [blame] | 160 | """Return a list of all non-overlapping matches in the string. |
| 161 | |
| 162 | If one or more groups are present in the pattern, return a |
| 163 | list of groups; this will be a list of tuples if the pattern |
| 164 | has more than one group. |
| 165 | |
| 166 | Empty matches are included in the result.""" |
Fredrik Lundh | e06cbb8 | 2001-07-06 20:56:10 +0000 | [diff] [blame] | 167 | return _compile(pattern, 0).findall(string) |
Jeremy Hylton | b1aa195 | 2000-06-01 17:39:12 +0000 | [diff] [blame] | 168 | |
| 169 | def compile(pattern, flags=0): |
Fredrik Lundh | 770617b | 2001-01-14 15:06:11 +0000 | [diff] [blame] | 170 | "Compile a regular expression pattern, returning a pattern object." |
Jeremy Hylton | b1aa195 | 2000-06-01 17:39:12 +0000 | [diff] [blame] | 171 | return _compile(pattern, flags) |
| 172 | |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 173 | def purge(): |
Fredrik Lundh | 770617b | 2001-01-14 15:06:11 +0000 | [diff] [blame] | 174 | "Clear the regular expression cache" |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 175 | _cache.clear() |
Fredrik Lundh | b25e1ad | 2001-03-22 15:50:10 +0000 | [diff] [blame] | 176 | _cache_repl.clear() |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 177 | |
Fredrik Lundh | 436c3d5 | 2000-06-29 08:58:44 +0000 | [diff] [blame] | 178 | def template(pattern, flags=0): |
Fredrik Lundh | 770617b | 2001-01-14 15:06:11 +0000 | [diff] [blame] | 179 | "Compile a template pattern, returning a pattern object" |
Fredrik Lundh | 436c3d5 | 2000-06-29 08:58:44 +0000 | [diff] [blame] | 180 | return _compile(pattern, flags|T) |
| 181 | |
Jeremy Hylton | b1aa195 | 2000-06-01 17:39:12 +0000 | [diff] [blame] | 182 | def escape(pattern): |
Fredrik Lundh | 770617b | 2001-01-14 15:06:11 +0000 | [diff] [blame] | 183 | "Escape all non-alphanumeric characters in pattern." |
Jeremy Hylton | b1aa195 | 2000-06-01 17:39:12 +0000 | [diff] [blame] | 184 | s = list(pattern) |
| 185 | for i in range(len(pattern)): |
| 186 | c = pattern[i] |
| 187 | if not ("a" <= c <= "z" or "A" <= c <= "Z" or "0" <= c <= "9"): |
| 188 | if c == "\000": |
| 189 | s[i] = "\\000" |
| 190 | else: |
| 191 | s[i] = "\\" + c |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 192 | return _join(s, pattern) |
Guido van Rossum | 7627c0d | 2000-03-31 14:58:54 +0000 | [diff] [blame] | 193 | |
| 194 | # -------------------------------------------------------------------- |
Jeremy Hylton | b1aa195 | 2000-06-01 17:39:12 +0000 | [diff] [blame] | 195 | # internals |
Guido van Rossum | 7627c0d | 2000-03-31 14:58:54 +0000 | [diff] [blame] | 196 | |
Jeremy Hylton | b1aa195 | 2000-06-01 17:39:12 +0000 | [diff] [blame] | 197 | _cache = {} |
Fredrik Lundh | b25e1ad | 2001-03-22 15:50:10 +0000 | [diff] [blame] | 198 | _cache_repl = {} |
| 199 | |
Fredrik Lundh | 397a654 | 2001-10-18 19:30:16 +0000 | [diff] [blame] | 200 | _pattern_type = type(sre_compile.compile("", 0)) |
| 201 | |
Jeremy Hylton | b1aa195 | 2000-06-01 17:39:12 +0000 | [diff] [blame] | 202 | _MAXCACHE = 100 |
Guido van Rossum | 7627c0d | 2000-03-31 14:58:54 +0000 | [diff] [blame] | 203 | |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 204 | def _join(seq, sep): |
| 205 | # internal: join into string having the same type as sep |
Fredrik Lundh | f2989b2 | 2001-02-18 12:05:16 +0000 | [diff] [blame] | 206 | return string.join(seq, sep[:0]) |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 207 | |
Fredrik Lundh | 7898c3e | 2000-08-07 20:59:04 +0000 | [diff] [blame] | 208 | def _compile(*key): |
Jeremy Hylton | b1aa195 | 2000-06-01 17:39:12 +0000 | [diff] [blame] | 209 | # internal: compile pattern |
Fredrik Lundh | 7898c3e | 2000-08-07 20:59:04 +0000 | [diff] [blame] | 210 | p = _cache.get(key) |
| 211 | if p is not None: |
| 212 | return p |
| 213 | pattern, flags = key |
Fredrik Lundh | 397a654 | 2001-10-18 19:30:16 +0000 | [diff] [blame] | 214 | if type(pattern) is _pattern_type: |
Jeremy Hylton | b1aa195 | 2000-06-01 17:39:12 +0000 | [diff] [blame] | 215 | return pattern |
Fredrik Lundh | 397a654 | 2001-10-18 19:30:16 +0000 | [diff] [blame] | 216 | if type(pattern) not in sre_compile.STRING_TYPES: |
| 217 | raise TypeError, "first argument must be string or compiled pattern" |
Fredrik Lundh | e186983 | 2000-08-01 22:47:49 +0000 | [diff] [blame] | 218 | try: |
| 219 | p = sre_compile.compile(pattern, flags) |
| 220 | except error, v: |
| 221 | raise error, v # invalid expression |
Jeremy Hylton | b1aa195 | 2000-06-01 17:39:12 +0000 | [diff] [blame] | 222 | if len(_cache) >= _MAXCACHE: |
| 223 | _cache.clear() |
| 224 | _cache[key] = p |
| 225 | return p |
| 226 | |
Fredrik Lundh | b25e1ad | 2001-03-22 15:50:10 +0000 | [diff] [blame] | 227 | def _compile_repl(*key): |
| 228 | # internal: compile replacement pattern |
| 229 | p = _cache_repl.get(key) |
| 230 | if p is not None: |
| 231 | return p |
| 232 | repl, pattern = key |
| 233 | try: |
| 234 | p = sre_parse.parse_template(repl, pattern) |
| 235 | except error, v: |
| 236 | raise error, v # invalid expression |
| 237 | if len(_cache_repl) >= _MAXCACHE: |
| 238 | _cache_repl.clear() |
| 239 | _cache_repl[key] = p |
| 240 | return p |
| 241 | |
Fredrik Lundh | 5644b7f | 2000-09-21 17:03:25 +0000 | [diff] [blame] | 242 | def _expand(pattern, match, template): |
| 243 | # internal: match.expand implementation hook |
| 244 | template = sre_parse.parse_template(template, pattern) |
| 245 | return sre_parse.expand_template(template, match) |
| 246 | |
Fredrik Lundh | 2d96f11 | 2001-07-08 13:26:57 +0000 | [diff] [blame] | 247 | def _sub(pattern, template, text, count=0): |
Jeremy Hylton | b1aa195 | 2000-06-01 17:39:12 +0000 | [diff] [blame] | 248 | # internal: pattern.sub implementation hook |
Fredrik Lundh | 2d96f11 | 2001-07-08 13:26:57 +0000 | [diff] [blame] | 249 | return _subn(pattern, template, text, count, 1)[0] |
Jeremy Hylton | b1aa195 | 2000-06-01 17:39:12 +0000 | [diff] [blame] | 250 | |
Fredrik Lundh | 2d96f11 | 2001-07-08 13:26:57 +0000 | [diff] [blame] | 251 | def _subn(pattern, template, text, count=0, sub=0): |
Jeremy Hylton | b1aa195 | 2000-06-01 17:39:12 +0000 | [diff] [blame] | 252 | # internal: pattern.subn implementation hook |
| 253 | if callable(template): |
Andrew M. Kuchling | e8d52af | 2000-06-18 20:27:10 +0000 | [diff] [blame] | 254 | filter = template |
Jeremy Hylton | b1aa195 | 2000-06-01 17:39:12 +0000 | [diff] [blame] | 255 | else: |
Fredrik Lundh | b25e1ad | 2001-03-22 15:50:10 +0000 | [diff] [blame] | 256 | template = _compile_repl(template, pattern) |
Fredrik Lundh | 2d96f11 | 2001-07-08 13:26:57 +0000 | [diff] [blame] | 257 | literals = template[1] |
Fredrik Lundh | 59b6865 | 2001-09-18 20:55:24 +0000 | [diff] [blame] | 258 | if sub and not count: |
| 259 | literal = pattern._getliteral() |
| 260 | if literal and "\\" in literal: |
| 261 | literal = None # may contain untranslated escapes |
| 262 | if literal is not None and len(literals) == 1 and literals[0]: |
| 263 | # shortcut: both pattern and string are literals |
| 264 | return string.replace(text, pattern.pattern, literals[0]), 0 |
Jeremy Hylton | b1aa195 | 2000-06-01 17:39:12 +0000 | [diff] [blame] | 265 | def filter(match, template=template): |
Fredrik Lundh | 436c3d5 | 2000-06-29 08:58:44 +0000 | [diff] [blame] | 266 | return sre_parse.expand_template(template, match) |
Jeremy Hylton | b1aa195 | 2000-06-01 17:39:12 +0000 | [diff] [blame] | 267 | n = i = 0 |
| 268 | s = [] |
| 269 | append = s.append |
Fredrik Lundh | 2d96f11 | 2001-07-08 13:26:57 +0000 | [diff] [blame] | 270 | c = pattern.scanner(text) |
Jeremy Hylton | b1aa195 | 2000-06-01 17:39:12 +0000 | [diff] [blame] | 271 | while not count or n < count: |
| 272 | m = c.search() |
| 273 | if not m: |
| 274 | break |
Fredrik Lundh | 90a0791 | 2000-06-30 07:50:59 +0000 | [diff] [blame] | 275 | b, e = m.span() |
Fredrik Lundh | 01016fe | 2000-06-30 00:27:46 +0000 | [diff] [blame] | 276 | if i < b: |
Fredrik Lundh | 2d96f11 | 2001-07-08 13:26:57 +0000 | [diff] [blame] | 277 | append(text[i:b]) |
Fredrik Lundh | 21009b9 | 2001-09-18 18:47:09 +0000 | [diff] [blame] | 278 | elif i == b == e and n: |
| 279 | append(text[i:b]) |
| 280 | continue # ignore empty match at previous position |
Jeremy Hylton | b1aa195 | 2000-06-01 17:39:12 +0000 | [diff] [blame] | 281 | append(filter(m)) |
Fredrik Lundh | 90a0791 | 2000-06-30 07:50:59 +0000 | [diff] [blame] | 282 | i = e |
Jeremy Hylton | b1aa195 | 2000-06-01 17:39:12 +0000 | [diff] [blame] | 283 | n = n + 1 |
Fredrik Lundh | 2d96f11 | 2001-07-08 13:26:57 +0000 | [diff] [blame] | 284 | append(text[i:]) |
| 285 | return _join(s, text[:0]), n |
Jeremy Hylton | b1aa195 | 2000-06-01 17:39:12 +0000 | [diff] [blame] | 286 | |
Fredrik Lundh | 2d96f11 | 2001-07-08 13:26:57 +0000 | [diff] [blame] | 287 | def _split(pattern, text, maxsplit=0): |
Jeremy Hylton | b1aa195 | 2000-06-01 17:39:12 +0000 | [diff] [blame] | 288 | # internal: pattern.split implementation hook |
| 289 | n = i = 0 |
| 290 | s = [] |
| 291 | append = s.append |
Fredrik Lundh | be2211e | 2000-06-29 16:57:40 +0000 | [diff] [blame] | 292 | extend = s.extend |
Fredrik Lundh | 2d96f11 | 2001-07-08 13:26:57 +0000 | [diff] [blame] | 293 | c = pattern.scanner(text) |
Fredrik Lundh | 01016fe | 2000-06-30 00:27:46 +0000 | [diff] [blame] | 294 | g = pattern.groups |
Jeremy Hylton | b1aa195 | 2000-06-01 17:39:12 +0000 | [diff] [blame] | 295 | while not maxsplit or n < maxsplit: |
| 296 | m = c.search() |
| 297 | if not m: |
| 298 | break |
Fredrik Lundh | 90a0791 | 2000-06-30 07:50:59 +0000 | [diff] [blame] | 299 | b, e = m.span() |
| 300 | if b == e: |
Fredrik Lundh | 2d96f11 | 2001-07-08 13:26:57 +0000 | [diff] [blame] | 301 | if i >= len(text): |
Fredrik Lundh | 90a0791 | 2000-06-30 07:50:59 +0000 | [diff] [blame] | 302 | break |
| 303 | continue |
Fredrik Lundh | 2d96f11 | 2001-07-08 13:26:57 +0000 | [diff] [blame] | 304 | append(text[i:b]) |
Fredrik Lundh | 90a0791 | 2000-06-30 07:50:59 +0000 | [diff] [blame] | 305 | if g and b != e: |
Fredrik Lundh | 1c5aa69 | 2001-01-16 07:37:30 +0000 | [diff] [blame] | 306 | extend(list(m.groups())) |
Fredrik Lundh | 90a0791 | 2000-06-30 07:50:59 +0000 | [diff] [blame] | 307 | i = e |
Jeremy Hylton | b1aa195 | 2000-06-01 17:39:12 +0000 | [diff] [blame] | 308 | n = n + 1 |
Fredrik Lundh | 2d96f11 | 2001-07-08 13:26:57 +0000 | [diff] [blame] | 309 | append(text[i:]) |
Jeremy Hylton | b1aa195 | 2000-06-01 17:39:12 +0000 | [diff] [blame] | 310 | return s |
Fredrik Lundh | 0640e11 | 2000-06-30 13:55:15 +0000 | [diff] [blame] | 311 | |
| 312 | # register myself for pickling |
| 313 | |
| 314 | import copy_reg |
| 315 | |
| 316 | def _pickle(p): |
| 317 | return _compile, (p.pattern, p.flags) |
| 318 | |
Fredrik Lundh | 397a654 | 2001-10-18 19:30:16 +0000 | [diff] [blame] | 319 | copy_reg.pickle(_pattern_type, _pickle, _compile) |
Fredrik Lundh | 7cafe4d | 2000-07-02 17:33:27 +0000 | [diff] [blame] | 320 | |
| 321 | # -------------------------------------------------------------------- |
| 322 | # experimental stuff (see python-dev discussions for details) |
| 323 | |
| 324 | class Scanner: |
| 325 | def __init__(self, lexicon): |
Fredrik Lundh | 29c4ba9 | 2000-08-01 18:20:07 +0000 | [diff] [blame] | 326 | from sre_constants import BRANCH, SUBPATTERN |
Fredrik Lundh | 7cafe4d | 2000-07-02 17:33:27 +0000 | [diff] [blame] | 327 | self.lexicon = lexicon |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 328 | # combine phrases into a compound pattern |
Fredrik Lundh | 7cafe4d | 2000-07-02 17:33:27 +0000 | [diff] [blame] | 329 | p = [] |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 330 | s = sre_parse.Pattern() |
Fredrik Lundh | 7cafe4d | 2000-07-02 17:33:27 +0000 | [diff] [blame] | 331 | for phrase, action in lexicon: |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 332 | p.append(sre_parse.SubPattern(s, [ |
Fredrik Lundh | 29c4ba9 | 2000-08-01 18:20:07 +0000 | [diff] [blame] | 333 | (SUBPATTERN, (len(p), sre_parse.parse(phrase))), |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 334 | ])) |
| 335 | p = sre_parse.SubPattern(s, [(BRANCH, (None, p))]) |
| 336 | s.groups = len(p) |
| 337 | self.scanner = sre_compile.compile(p) |
Fredrik Lundh | 7cafe4d | 2000-07-02 17:33:27 +0000 | [diff] [blame] | 338 | def scan(self, string): |
| 339 | result = [] |
| 340 | append = result.append |
| 341 | match = self.scanner.match |
| 342 | i = 0 |
| 343 | while 1: |
| 344 | m = match(string, i) |
| 345 | if not m: |
| 346 | break |
| 347 | j = m.end() |
| 348 | if i == j: |
| 349 | break |
Fredrik Lundh | 019bcb5 | 2000-07-02 22:59:57 +0000 | [diff] [blame] | 350 | action = self.lexicon[m.lastindex][1] |
Fredrik Lundh | 7cafe4d | 2000-07-02 17:33:27 +0000 | [diff] [blame] | 351 | if callable(action): |
Fredrik Lundh | 770617b | 2001-01-14 15:06:11 +0000 | [diff] [blame] | 352 | self.match = m |
Fredrik Lundh | 7cafe4d | 2000-07-02 17:33:27 +0000 | [diff] [blame] | 353 | action = action(self, m.group()) |
| 354 | if action is not None: |
| 355 | append(action) |
| 356 | i = j |
| 357 | return result, string[i:] |