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. |
| 48 | (?iLmsx) Set the I, L, M, S, or X flag for the RE. |
| 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. |
| 83 | template Compile a template pattern, returning a pattern object. |
| 84 | escape Backslash all non-alphanumerics in a string. |
| 85 | |
| 86 | Some of the functions in this module takes flags as optional parameters: |
| 87 | I IGNORECASE Perform case-insensitive matching. |
| 88 | L LOCALE Make \w, \W, \b, \B, dependent on the current locale. |
| 89 | M MULTILINE "^" matches the beginning of lines as well as the string. |
| 90 | "$" matches the end of lines as well as the string. |
| 91 | S DOTALL "." matches any character at all, including the newline. |
| 92 | X VERBOSE Ignore whitespace and comments for nicer looking RE's. |
| 93 | U UNICODE Use unicode locale. |
| 94 | |
| 95 | This module also defines an exception 'error'. |
| 96 | |
| 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 | b25e1ad | 2001-03-22 15:50:10 +0000 | [diff] [blame] | 107 | __version__ = "2.1b2" |
| 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 | |
Jeremy Hylton | b1aa195 | 2000-06-01 17:39:12 +0000 | [diff] [blame] | 200 | _MAXCACHE = 100 |
Guido van Rossum | 7627c0d | 2000-03-31 14:58:54 +0000 | [diff] [blame] | 201 | |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 202 | def _join(seq, sep): |
| 203 | # internal: join into string having the same type as sep |
Fredrik Lundh | f2989b2 | 2001-02-18 12:05:16 +0000 | [diff] [blame] | 204 | return string.join(seq, sep[:0]) |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 205 | |
Fredrik Lundh | 7898c3e | 2000-08-07 20:59:04 +0000 | [diff] [blame] | 206 | def _compile(*key): |
Jeremy Hylton | b1aa195 | 2000-06-01 17:39:12 +0000 | [diff] [blame] | 207 | # internal: compile pattern |
Fredrik Lundh | 7898c3e | 2000-08-07 20:59:04 +0000 | [diff] [blame] | 208 | p = _cache.get(key) |
| 209 | if p is not None: |
| 210 | return p |
| 211 | pattern, flags = key |
| 212 | if type(pattern) not in sre_compile.STRING_TYPES: |
Jeremy Hylton | b1aa195 | 2000-06-01 17:39:12 +0000 | [diff] [blame] | 213 | return pattern |
Fredrik Lundh | e186983 | 2000-08-01 22:47:49 +0000 | [diff] [blame] | 214 | try: |
| 215 | p = sre_compile.compile(pattern, flags) |
| 216 | except error, v: |
| 217 | raise error, v # invalid expression |
Jeremy Hylton | b1aa195 | 2000-06-01 17:39:12 +0000 | [diff] [blame] | 218 | if len(_cache) >= _MAXCACHE: |
| 219 | _cache.clear() |
| 220 | _cache[key] = p |
| 221 | return p |
| 222 | |
Fredrik Lundh | b25e1ad | 2001-03-22 15:50:10 +0000 | [diff] [blame] | 223 | def _compile_repl(*key): |
| 224 | # internal: compile replacement pattern |
| 225 | p = _cache_repl.get(key) |
| 226 | if p is not None: |
| 227 | return p |
| 228 | repl, pattern = key |
| 229 | try: |
| 230 | p = sre_parse.parse_template(repl, pattern) |
| 231 | except error, v: |
| 232 | raise error, v # invalid expression |
| 233 | if len(_cache_repl) >= _MAXCACHE: |
| 234 | _cache_repl.clear() |
| 235 | _cache_repl[key] = p |
| 236 | return p |
| 237 | |
Fredrik Lundh | 5644b7f | 2000-09-21 17:03:25 +0000 | [diff] [blame] | 238 | def _expand(pattern, match, template): |
| 239 | # internal: match.expand implementation hook |
| 240 | template = sre_parse.parse_template(template, pattern) |
| 241 | return sre_parse.expand_template(template, match) |
| 242 | |
Fredrik Lundh | 2d96f11 | 2001-07-08 13:26:57 +0000 | [diff] [blame] | 243 | def _sub(pattern, template, text, count=0): |
Jeremy Hylton | b1aa195 | 2000-06-01 17:39:12 +0000 | [diff] [blame] | 244 | # internal: pattern.sub implementation hook |
Fredrik Lundh | 2d96f11 | 2001-07-08 13:26:57 +0000 | [diff] [blame] | 245 | return _subn(pattern, template, text, count, 1)[0] |
Jeremy Hylton | b1aa195 | 2000-06-01 17:39:12 +0000 | [diff] [blame] | 246 | |
Fredrik Lundh | 2d96f11 | 2001-07-08 13:26:57 +0000 | [diff] [blame] | 247 | def _subn(pattern, template, text, count=0, sub=0): |
Jeremy Hylton | b1aa195 | 2000-06-01 17:39:12 +0000 | [diff] [blame] | 248 | # internal: pattern.subn implementation hook |
| 249 | if callable(template): |
Andrew M. Kuchling | e8d52af | 2000-06-18 20:27:10 +0000 | [diff] [blame] | 250 | filter = template |
Jeremy Hylton | b1aa195 | 2000-06-01 17:39:12 +0000 | [diff] [blame] | 251 | else: |
Fredrik Lundh | b25e1ad | 2001-03-22 15:50:10 +0000 | [diff] [blame] | 252 | template = _compile_repl(template, pattern) |
Fredrik Lundh | 2d96f11 | 2001-07-08 13:26:57 +0000 | [diff] [blame] | 253 | literals = template[1] |
Guido van Rossum | 315cd29 | 2001-08-10 14:56:54 +0000 | [diff] [blame] | 254 | sub = 0 # temporarly disabled, see bug #449000 |
Fredrik Lundh | 2d96f11 | 2001-07-08 13:26:57 +0000 | [diff] [blame] | 255 | if (sub and not count and pattern._isliteral() and |
| 256 | len(literals) == 1 and literals[0]): |
| 257 | # shortcut: both pattern and string are literals |
| 258 | return string.replace(text, pattern.pattern, literals[0]), 0 |
Jeremy Hylton | b1aa195 | 2000-06-01 17:39:12 +0000 | [diff] [blame] | 259 | def filter(match, template=template): |
Fredrik Lundh | 436c3d5 | 2000-06-29 08:58:44 +0000 | [diff] [blame] | 260 | return sre_parse.expand_template(template, match) |
Jeremy Hylton | b1aa195 | 2000-06-01 17:39:12 +0000 | [diff] [blame] | 261 | n = i = 0 |
| 262 | s = [] |
| 263 | append = s.append |
Fredrik Lundh | 2d96f11 | 2001-07-08 13:26:57 +0000 | [diff] [blame] | 264 | c = pattern.scanner(text) |
Jeremy Hylton | b1aa195 | 2000-06-01 17:39:12 +0000 | [diff] [blame] | 265 | while not count or n < count: |
| 266 | m = c.search() |
| 267 | if not m: |
| 268 | break |
Fredrik Lundh | 90a0791 | 2000-06-30 07:50:59 +0000 | [diff] [blame] | 269 | b, e = m.span() |
Fredrik Lundh | 01016fe | 2000-06-30 00:27:46 +0000 | [diff] [blame] | 270 | if i < b: |
Fredrik Lundh | 2d96f11 | 2001-07-08 13:26:57 +0000 | [diff] [blame] | 271 | append(text[i:b]) |
Jeremy Hylton | b1aa195 | 2000-06-01 17:39:12 +0000 | [diff] [blame] | 272 | append(filter(m)) |
Fredrik Lundh | 90a0791 | 2000-06-30 07:50:59 +0000 | [diff] [blame] | 273 | i = e |
Jeremy Hylton | b1aa195 | 2000-06-01 17:39:12 +0000 | [diff] [blame] | 274 | n = n + 1 |
Fredrik Lundh | 2d96f11 | 2001-07-08 13:26:57 +0000 | [diff] [blame] | 275 | append(text[i:]) |
| 276 | return _join(s, text[:0]), n |
Jeremy Hylton | b1aa195 | 2000-06-01 17:39:12 +0000 | [diff] [blame] | 277 | |
Fredrik Lundh | 2d96f11 | 2001-07-08 13:26:57 +0000 | [diff] [blame] | 278 | def _split(pattern, text, maxsplit=0): |
Jeremy Hylton | b1aa195 | 2000-06-01 17:39:12 +0000 | [diff] [blame] | 279 | # internal: pattern.split implementation hook |
| 280 | n = i = 0 |
| 281 | s = [] |
| 282 | append = s.append |
Fredrik Lundh | be2211e | 2000-06-29 16:57:40 +0000 | [diff] [blame] | 283 | extend = s.extend |
Fredrik Lundh | 2d96f11 | 2001-07-08 13:26:57 +0000 | [diff] [blame] | 284 | c = pattern.scanner(text) |
Fredrik Lundh | 01016fe | 2000-06-30 00:27:46 +0000 | [diff] [blame] | 285 | g = pattern.groups |
Jeremy Hylton | b1aa195 | 2000-06-01 17:39:12 +0000 | [diff] [blame] | 286 | while not maxsplit or n < maxsplit: |
| 287 | m = c.search() |
| 288 | if not m: |
| 289 | break |
Fredrik Lundh | 90a0791 | 2000-06-30 07:50:59 +0000 | [diff] [blame] | 290 | b, e = m.span() |
| 291 | if b == e: |
Fredrik Lundh | 2d96f11 | 2001-07-08 13:26:57 +0000 | [diff] [blame] | 292 | if i >= len(text): |
Fredrik Lundh | 90a0791 | 2000-06-30 07:50:59 +0000 | [diff] [blame] | 293 | break |
| 294 | continue |
Fredrik Lundh | 2d96f11 | 2001-07-08 13:26:57 +0000 | [diff] [blame] | 295 | append(text[i:b]) |
Fredrik Lundh | 90a0791 | 2000-06-30 07:50:59 +0000 | [diff] [blame] | 296 | if g and b != e: |
Fredrik Lundh | 1c5aa69 | 2001-01-16 07:37:30 +0000 | [diff] [blame] | 297 | extend(list(m.groups())) |
Fredrik Lundh | 90a0791 | 2000-06-30 07:50:59 +0000 | [diff] [blame] | 298 | i = e |
Jeremy Hylton | b1aa195 | 2000-06-01 17:39:12 +0000 | [diff] [blame] | 299 | n = n + 1 |
Fredrik Lundh | 2d96f11 | 2001-07-08 13:26:57 +0000 | [diff] [blame] | 300 | append(text[i:]) |
Jeremy Hylton | b1aa195 | 2000-06-01 17:39:12 +0000 | [diff] [blame] | 301 | return s |
Fredrik Lundh | 0640e11 | 2000-06-30 13:55:15 +0000 | [diff] [blame] | 302 | |
| 303 | # register myself for pickling |
| 304 | |
| 305 | import copy_reg |
| 306 | |
| 307 | def _pickle(p): |
| 308 | return _compile, (p.pattern, p.flags) |
| 309 | |
Fredrik Lundh | 7898c3e | 2000-08-07 20:59:04 +0000 | [diff] [blame] | 310 | copy_reg.pickle(type(_compile("", 0)), _pickle, _compile) |
Fredrik Lundh | 7cafe4d | 2000-07-02 17:33:27 +0000 | [diff] [blame] | 311 | |
| 312 | # -------------------------------------------------------------------- |
| 313 | # experimental stuff (see python-dev discussions for details) |
| 314 | |
| 315 | class Scanner: |
| 316 | def __init__(self, lexicon): |
Fredrik Lundh | 29c4ba9 | 2000-08-01 18:20:07 +0000 | [diff] [blame] | 317 | from sre_constants import BRANCH, SUBPATTERN |
Fredrik Lundh | 7cafe4d | 2000-07-02 17:33:27 +0000 | [diff] [blame] | 318 | self.lexicon = lexicon |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 319 | # combine phrases into a compound pattern |
Fredrik Lundh | 7cafe4d | 2000-07-02 17:33:27 +0000 | [diff] [blame] | 320 | p = [] |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 321 | s = sre_parse.Pattern() |
Fredrik Lundh | 7cafe4d | 2000-07-02 17:33:27 +0000 | [diff] [blame] | 322 | for phrase, action in lexicon: |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 323 | p.append(sre_parse.SubPattern(s, [ |
Fredrik Lundh | 29c4ba9 | 2000-08-01 18:20:07 +0000 | [diff] [blame] | 324 | (SUBPATTERN, (len(p), sre_parse.parse(phrase))), |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 325 | ])) |
| 326 | p = sre_parse.SubPattern(s, [(BRANCH, (None, p))]) |
| 327 | s.groups = len(p) |
| 328 | self.scanner = sre_compile.compile(p) |
Fredrik Lundh | 7cafe4d | 2000-07-02 17:33:27 +0000 | [diff] [blame] | 329 | def scan(self, string): |
| 330 | result = [] |
| 331 | append = result.append |
| 332 | match = self.scanner.match |
| 333 | i = 0 |
| 334 | while 1: |
| 335 | m = match(string, i) |
| 336 | if not m: |
| 337 | break |
| 338 | j = m.end() |
| 339 | if i == j: |
| 340 | break |
Fredrik Lundh | 019bcb5 | 2000-07-02 22:59:57 +0000 | [diff] [blame] | 341 | action = self.lexicon[m.lastindex][1] |
Fredrik Lundh | 7cafe4d | 2000-07-02 17:33:27 +0000 | [diff] [blame] | 342 | if callable(action): |
Fredrik Lundh | 770617b | 2001-01-14 15:06:11 +0000 | [diff] [blame] | 343 | self.match = m |
Fredrik Lundh | 7cafe4d | 2000-07-02 17:33:27 +0000 | [diff] [blame] | 344 | action = action(self, m.group()) |
| 345 | if action is not None: |
| 346 | append(action) |
| 347 | i = j |
| 348 | return result, string[i:] |