Guido van Rossum | 7627c0d | 2000-03-31 14:58:54 +0000 | [diff] [blame] | 1 | # |
| 2 | # Secret Labs' Regular Expression Engine |
| 3 | # $Id$ |
| 4 | # |
| 5 | # re-compatible interface for the sre matching engine |
| 6 | # |
| 7 | # Copyright (c) 1998-2000 by Secret Labs AB. All rights reserved. |
| 8 | # |
Guido van Rossum | 7627c0d | 2000-03-31 14:58:54 +0000 | [diff] [blame] | 9 | # Portions of this engine have been developed in cooperation with |
| 10 | # CNRI. Hewlett-Packard provided funding for 1.6 integration and |
| 11 | # other compatibility work. |
| 12 | # |
| 13 | |
Guido van Rossum | 7627c0d | 2000-03-31 14:58:54 +0000 | [diff] [blame] | 14 | import sre_compile |
Fredrik Lundh | 436c3d58 | 2000-06-29 08:58:44 +0000 | [diff] [blame] | 15 | import sre_parse |
Guido van Rossum | 7627c0d | 2000-03-31 14:58:54 +0000 | [diff] [blame] | 16 | |
Jeremy Hylton | b1aa195 | 2000-06-01 17:39:12 +0000 | [diff] [blame] | 17 | # flags |
| 18 | I = IGNORECASE = sre_compile.SRE_FLAG_IGNORECASE |
| 19 | L = LOCALE = sre_compile.SRE_FLAG_LOCALE |
| 20 | M = MULTILINE = sre_compile.SRE_FLAG_MULTILINE |
| 21 | S = DOTALL = sre_compile.SRE_FLAG_DOTALL |
| 22 | X = VERBOSE = sre_compile.SRE_FLAG_VERBOSE |
| 23 | |
Fredrik Lundh | 436c3d58 | 2000-06-29 08:58:44 +0000 | [diff] [blame] | 24 | # sre extensions (may or may not be in 1.6 final) |
| 25 | T = TEMPLATE = sre_compile.SRE_FLAG_TEMPLATE |
| 26 | U = UNICODE = sre_compile.SRE_FLAG_UNICODE |
| 27 | |
| 28 | # sre exception |
Fredrik Lundh | be2211e | 2000-06-29 16:57:40 +0000 | [diff] [blame^] | 29 | error = sre_compile.error |
Fredrik Lundh | 436c3d58 | 2000-06-29 08:58:44 +0000 | [diff] [blame] | 30 | |
Guido van Rossum | 7627c0d | 2000-03-31 14:58:54 +0000 | [diff] [blame] | 31 | # -------------------------------------------------------------------- |
| 32 | # public interface |
| 33 | |
Jeremy Hylton | b1aa195 | 2000-06-01 17:39:12 +0000 | [diff] [blame] | 34 | # FIXME: add docstrings |
Guido van Rossum | 7627c0d | 2000-03-31 14:58:54 +0000 | [diff] [blame] | 35 | |
| 36 | def match(pattern, string, flags=0): |
Jeremy Hylton | b1aa195 | 2000-06-01 17:39:12 +0000 | [diff] [blame] | 37 | return _compile(pattern, flags).match(string) |
Guido van Rossum | 7627c0d | 2000-03-31 14:58:54 +0000 | [diff] [blame] | 38 | |
| 39 | def search(pattern, string, flags=0): |
Jeremy Hylton | b1aa195 | 2000-06-01 17:39:12 +0000 | [diff] [blame] | 40 | return _compile(pattern, flags).search(string) |
Guido van Rossum | 7627c0d | 2000-03-31 14:58:54 +0000 | [diff] [blame] | 41 | |
Jeremy Hylton | b1aa195 | 2000-06-01 17:39:12 +0000 | [diff] [blame] | 42 | def sub(pattern, repl, string, count=0): |
| 43 | return _compile(pattern).sub(repl, string, count) |
| 44 | |
| 45 | def subn(pattern, repl, string, count=0): |
| 46 | return _compile(pattern).subn(repl, string, count) |
| 47 | |
| 48 | def split(pattern, string, maxsplit=0): |
| 49 | return _compile(pattern).split(string, maxsplit) |
| 50 | |
| 51 | def findall(pattern, string, maxsplit=0): |
| 52 | return _compile(pattern).findall(string, maxsplit) |
| 53 | |
| 54 | def compile(pattern, flags=0): |
| 55 | return _compile(pattern, flags) |
| 56 | |
Fredrik Lundh | 436c3d58 | 2000-06-29 08:58:44 +0000 | [diff] [blame] | 57 | def template(pattern, flags=0): |
| 58 | return _compile(pattern, flags|T) |
| 59 | |
Jeremy Hylton | b1aa195 | 2000-06-01 17:39:12 +0000 | [diff] [blame] | 60 | def escape(pattern): |
| 61 | s = list(pattern) |
| 62 | for i in range(len(pattern)): |
| 63 | c = pattern[i] |
| 64 | if not ("a" <= c <= "z" or "A" <= c <= "Z" or "0" <= c <= "9"): |
| 65 | if c == "\000": |
| 66 | s[i] = "\\000" |
| 67 | else: |
| 68 | s[i] = "\\" + c |
| 69 | return pattern[:0].join(s) |
Guido van Rossum | 7627c0d | 2000-03-31 14:58:54 +0000 | [diff] [blame] | 70 | |
| 71 | # -------------------------------------------------------------------- |
Jeremy Hylton | b1aa195 | 2000-06-01 17:39:12 +0000 | [diff] [blame] | 72 | # internals |
Guido van Rossum | 7627c0d | 2000-03-31 14:58:54 +0000 | [diff] [blame] | 73 | |
Jeremy Hylton | b1aa195 | 2000-06-01 17:39:12 +0000 | [diff] [blame] | 74 | _cache = {} |
| 75 | _MAXCACHE = 100 |
Guido van Rossum | 7627c0d | 2000-03-31 14:58:54 +0000 | [diff] [blame] | 76 | |
Jeremy Hylton | b1aa195 | 2000-06-01 17:39:12 +0000 | [diff] [blame] | 77 | def _compile(pattern, flags=0): |
| 78 | # internal: compile pattern |
| 79 | tp = type(pattern) |
| 80 | if tp not in (type(""), type(u"")): |
| 81 | return pattern |
| 82 | key = (tp, pattern, flags) |
| 83 | try: |
| 84 | return _cache[key] |
| 85 | except KeyError: |
| 86 | pass |
| 87 | p = sre_compile.compile(pattern, flags) |
| 88 | if len(_cache) >= _MAXCACHE: |
| 89 | _cache.clear() |
| 90 | _cache[key] = p |
| 91 | return p |
| 92 | |
| 93 | def _sub(pattern, template, string, count=0): |
| 94 | # internal: pattern.sub implementation hook |
| 95 | return _subn(pattern, template, string, count)[0] |
| 96 | |
Jeremy Hylton | b1aa195 | 2000-06-01 17:39:12 +0000 | [diff] [blame] | 97 | def _subn(pattern, template, string, count=0): |
| 98 | # internal: pattern.subn implementation hook |
| 99 | if callable(template): |
Andrew M. Kuchling | e8d52af | 2000-06-18 20:27:10 +0000 | [diff] [blame] | 100 | filter = template |
Jeremy Hylton | b1aa195 | 2000-06-01 17:39:12 +0000 | [diff] [blame] | 101 | else: |
Fredrik Lundh | 436c3d58 | 2000-06-29 08:58:44 +0000 | [diff] [blame] | 102 | template = sre_parse.parse_template(template, pattern) |
Jeremy Hylton | b1aa195 | 2000-06-01 17:39:12 +0000 | [diff] [blame] | 103 | def filter(match, template=template): |
Fredrik Lundh | 436c3d58 | 2000-06-29 08:58:44 +0000 | [diff] [blame] | 104 | return sre_parse.expand_template(template, match) |
Jeremy Hylton | b1aa195 | 2000-06-01 17:39:12 +0000 | [diff] [blame] | 105 | n = i = 0 |
| 106 | s = [] |
| 107 | append = s.append |
Fredrik Lundh | be2211e | 2000-06-29 16:57:40 +0000 | [diff] [blame^] | 108 | c = pattern.scanner(string) |
Jeremy Hylton | b1aa195 | 2000-06-01 17:39:12 +0000 | [diff] [blame] | 109 | while not count or n < count: |
| 110 | m = c.search() |
| 111 | if not m: |
| 112 | break |
| 113 | j = m.start() |
| 114 | if j > i: |
| 115 | append(string[i:j]) |
| 116 | append(filter(m)) |
| 117 | i = m.end() |
Fredrik Lundh | 436c3d58 | 2000-06-29 08:58:44 +0000 | [diff] [blame] | 118 | if i <= j: |
| 119 | break |
Jeremy Hylton | b1aa195 | 2000-06-01 17:39:12 +0000 | [diff] [blame] | 120 | n = n + 1 |
| 121 | if i < len(string): |
| 122 | append(string[i:]) |
| 123 | return string[:0].join(s), n |
| 124 | |
| 125 | def _split(pattern, string, maxsplit=0): |
| 126 | # internal: pattern.split implementation hook |
| 127 | n = i = 0 |
| 128 | s = [] |
| 129 | append = s.append |
Fredrik Lundh | be2211e | 2000-06-29 16:57:40 +0000 | [diff] [blame^] | 130 | extend = s.extend |
| 131 | c = pattern.scanner(string) |
| 132 | g = c.groups |
Jeremy Hylton | b1aa195 | 2000-06-01 17:39:12 +0000 | [diff] [blame] | 133 | while not maxsplit or n < maxsplit: |
| 134 | m = c.search() |
| 135 | if not m: |
| 136 | break |
Fredrik Lundh | be2211e | 2000-06-29 16:57:40 +0000 | [diff] [blame^] | 137 | b, e = m.span() |
| 138 | if e == i: |
| 139 | continue |
| 140 | append(string[i:b]) |
| 141 | if g and b != e: |
| 142 | extend(m.groups()) |
| 143 | i = e |
Jeremy Hylton | b1aa195 | 2000-06-01 17:39:12 +0000 | [diff] [blame] | 144 | n = n + 1 |
| 145 | if i < len(string): |
| 146 | append(string[i:]) |
| 147 | return s |