Guido van Rossum | bf9d353 | 1997-10-06 14:45:17 +0000 | [diff] [blame] | 1 | import sys |
Guido van Rossum | 5ca1b71 | 1997-07-10 21:00:31 +0000 | [diff] [blame] | 2 | import string |
Guido van Rossum | bf9d353 | 1997-10-06 14:45:17 +0000 | [diff] [blame] | 3 | from pcre import * |
Guido van Rossum | 5ca1b71 | 1997-07-10 21:00:31 +0000 | [diff] [blame] | 4 | |
Guido van Rossum | bf9d353 | 1997-10-06 14:45:17 +0000 | [diff] [blame] | 5 | # |
| 6 | # First, the public part of the interface: |
| 7 | # |
| 8 | |
| 9 | # pcre.error and re.error should be the same, since exceptions can be |
Guido van Rossum | dfa6790 | 1997-12-08 17:12:06 +0000 | [diff] [blame] | 10 | # raised from either module. |
Guido van Rossum | 5ca1b71 | 1997-07-10 21:00:31 +0000 | [diff] [blame] | 11 | |
| 12 | # compilation flags |
| 13 | |
Guido van Rossum | bf9d353 | 1997-10-06 14:45:17 +0000 | [diff] [blame] | 14 | I = IGNORECASE |
Guido van Rossum | dfa6790 | 1997-12-08 17:12:06 +0000 | [diff] [blame] | 15 | L = LOCALE |
Guido van Rossum | bf9d353 | 1997-10-06 14:45:17 +0000 | [diff] [blame] | 16 | M = MULTILINE |
| 17 | S = DOTALL |
| 18 | X = VERBOSE |
Guido van Rossum | 09bcfd6 | 1997-07-15 15:38:20 +0000 | [diff] [blame] | 19 | |
| 20 | # |
| 21 | # |
| 22 | # |
| 23 | |
Guido van Rossum | 26d80e6 | 1997-07-15 18:59:04 +0000 | [diff] [blame] | 24 | _cache = {} |
| 25 | _MAXCACHE = 20 |
Guido van Rossum | a0e4c1b | 1997-07-17 14:52:48 +0000 | [diff] [blame] | 26 | |
Guido van Rossum | 9e18ec7 | 1997-07-17 22:39:13 +0000 | [diff] [blame] | 27 | def _cachecompile(pattern, flags=0): |
Guido van Rossum | 26d80e6 | 1997-07-15 18:59:04 +0000 | [diff] [blame] | 28 | key = (pattern, flags) |
| 29 | try: |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 30 | return _cache[key] |
Guido van Rossum | 26d80e6 | 1997-07-15 18:59:04 +0000 | [diff] [blame] | 31 | except KeyError: |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 32 | pass |
Guido van Rossum | 26d80e6 | 1997-07-15 18:59:04 +0000 | [diff] [blame] | 33 | value = compile(pattern, flags) |
| 34 | if len(_cache) >= _MAXCACHE: |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 35 | _cache.clear() |
Guido van Rossum | 26d80e6 | 1997-07-15 18:59:04 +0000 | [diff] [blame] | 36 | _cache[key] = value |
| 37 | return value |
| 38 | |
Guido van Rossum | 5ca1b71 | 1997-07-10 21:00:31 +0000 | [diff] [blame] | 39 | def match(pattern, string, flags=0): |
Guido van Rossum | 26d80e6 | 1997-07-15 18:59:04 +0000 | [diff] [blame] | 40 | return _cachecompile(pattern, flags).match(string) |
Guido van Rossum | a0e4c1b | 1997-07-17 14:52:48 +0000 | [diff] [blame] | 41 | |
Guido van Rossum | 5ca1b71 | 1997-07-10 21:00:31 +0000 | [diff] [blame] | 42 | def search(pattern, string, flags=0): |
Guido van Rossum | 26d80e6 | 1997-07-15 18:59:04 +0000 | [diff] [blame] | 43 | return _cachecompile(pattern, flags).search(string) |
Guido van Rossum | a0e4c1b | 1997-07-17 14:52:48 +0000 | [diff] [blame] | 44 | |
Guido van Rossum | 5ca1b71 | 1997-07-10 21:00:31 +0000 | [diff] [blame] | 45 | def sub(pattern, repl, string, count=0): |
Guido van Rossum | 9e18ec7 | 1997-07-17 22:39:13 +0000 | [diff] [blame] | 46 | if type(pattern) == type(''): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 47 | pattern = _cachecompile(pattern) |
Guido van Rossum | 9e18ec7 | 1997-07-17 22:39:13 +0000 | [diff] [blame] | 48 | return pattern.sub(repl, string, count) |
Guido van Rossum | 5ca1b71 | 1997-07-10 21:00:31 +0000 | [diff] [blame] | 49 | |
| 50 | def subn(pattern, repl, string, count=0): |
Guido van Rossum | 9e18ec7 | 1997-07-17 22:39:13 +0000 | [diff] [blame] | 51 | if type(pattern) == type(''): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 52 | pattern = _cachecompile(pattern) |
Guido van Rossum | 9e18ec7 | 1997-07-17 22:39:13 +0000 | [diff] [blame] | 53 | return pattern.subn(repl, string, count) |
Guido van Rossum | a0e4c1b | 1997-07-17 14:52:48 +0000 | [diff] [blame] | 54 | |
Guido van Rossum | 8a9a4a2 | 1997-07-11 20:48:25 +0000 | [diff] [blame] | 55 | def split(pattern, string, maxsplit=0): |
Guido van Rossum | 9e18ec7 | 1997-07-17 22:39:13 +0000 | [diff] [blame] | 56 | if type(pattern) == type(''): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 57 | pattern = _cachecompile(pattern) |
Guido van Rossum | 9e18ec7 | 1997-07-17 22:39:13 +0000 | [diff] [blame] | 58 | return pattern.split(string, maxsplit) |
Guido van Rossum | 5ca1b71 | 1997-07-10 21:00:31 +0000 | [diff] [blame] | 59 | |
Guido van Rossum | be0b62c | 1998-06-29 20:29:08 +0000 | [diff] [blame] | 60 | def findall(pattern, string): |
| 61 | if type(pattern) == type(''): |
| 62 | pattern = _cachecompile(pattern) |
| 63 | return pattern.findall(string) |
| 64 | |
Guido van Rossum | dfa6790 | 1997-12-08 17:12:06 +0000 | [diff] [blame] | 65 | def escape(pattern): |
| 66 | "Escape all non-alphanumeric characters in pattern." |
Guido van Rossum | 3a8d1f5 | 1998-07-20 15:46:13 +0000 | [diff] [blame] | 67 | result = list(pattern) |
Guido van Rossum | dfa6790 | 1997-12-08 17:12:06 +0000 | [diff] [blame] | 68 | alphanum=string.letters+'_'+string.digits |
Guido van Rossum | 3a8d1f5 | 1998-07-20 15:46:13 +0000 | [diff] [blame] | 69 | for i in range(len(pattern)): |
| 70 | char = pattern[i] |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 71 | if char not in alphanum: |
Guido van Rossum | 3a8d1f5 | 1998-07-20 15:46:13 +0000 | [diff] [blame] | 72 | if char=='\000': result[i] = '\\000' |
| 73 | else: result[i] = '\\'+char |
Guido van Rossum | dfa6790 | 1997-12-08 17:12:06 +0000 | [diff] [blame] | 74 | return string.join(result, '') |
| 75 | |
| 76 | def compile(pattern, flags=0): |
| 77 | "Compile a regular expression pattern, returning a RegexObject." |
| 78 | groupindex={} |
| 79 | code=pcre_compile(pattern, flags, groupindex) |
| 80 | return RegexObject(pattern, flags, code, groupindex) |
| 81 | |
| 82 | |
Guido van Rossum | 5ca1b71 | 1997-07-10 21:00:31 +0000 | [diff] [blame] | 83 | # |
Guido van Rossum | dfa6790 | 1997-12-08 17:12:06 +0000 | [diff] [blame] | 84 | # Class definitions |
Guido van Rossum | 5ca1b71 | 1997-07-10 21:00:31 +0000 | [diff] [blame] | 85 | # |
| 86 | |
| 87 | class RegexObject: |
Guido van Rossum | be0b62c | 1998-06-29 20:29:08 +0000 | [diff] [blame] | 88 | |
Guido van Rossum | bf9d353 | 1997-10-06 14:45:17 +0000 | [diff] [blame] | 89 | def __init__(self, pattern, flags, code, groupindex): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 90 | self.code = code |
| 91 | self.flags = flags |
| 92 | self.pattern = pattern |
| 93 | self.groupindex = groupindex |
Guido van Rossum | dfa6790 | 1997-12-08 17:12:06 +0000 | [diff] [blame] | 94 | |
| 95 | def search(self, string, pos=0, endpos=None): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 96 | """Scan through string looking for a match to the pattern, returning |
| 97 | a MatchObject instance, or None if no match was found.""" |
Guido van Rossum | dfa6790 | 1997-12-08 17:12:06 +0000 | [diff] [blame] | 98 | |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 99 | if endpos is None or endpos>len(string): |
| 100 | endpos=len(string) |
| 101 | if endpos<pos: endpos=pos |
| 102 | regs = self.code.match(string, pos, endpos, 0) |
| 103 | if regs is None: |
| 104 | return None |
| 105 | self._num_regs=len(regs) |
| 106 | |
| 107 | return MatchObject(self, |
| 108 | string, |
| 109 | pos, endpos, |
| 110 | regs) |
Guido van Rossum | a0e4c1b | 1997-07-17 14:52:48 +0000 | [diff] [blame] | 111 | |
Guido van Rossum | dfa6790 | 1997-12-08 17:12:06 +0000 | [diff] [blame] | 112 | def match(self, string, pos=0, endpos=None): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 113 | """Try to apply the pattern at the start of the string, returning |
| 114 | a MatchObject instance, or None if no match was found.""" |
Guido van Rossum | dfa6790 | 1997-12-08 17:12:06 +0000 | [diff] [blame] | 115 | |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 116 | if endpos is None or endpos>len(string): |
| 117 | endpos=len(string) |
| 118 | if endpos<pos: endpos=pos |
| 119 | regs = self.code.match(string, pos, endpos, ANCHORED) |
| 120 | if regs is None: |
| 121 | return None |
| 122 | self._num_regs=len(regs) |
| 123 | return MatchObject(self, |
| 124 | string, |
| 125 | pos, endpos, |
| 126 | regs) |
Guido van Rossum | a0e4c1b | 1997-07-17 14:52:48 +0000 | [diff] [blame] | 127 | |
Guido van Rossum | 8a9a4a2 | 1997-07-11 20:48:25 +0000 | [diff] [blame] | 128 | def sub(self, repl, string, count=0): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 129 | """Return the string obtained by replacing the leftmost |
| 130 | non-overlapping occurrences of the pattern in string by the |
| 131 | replacement repl""" |
Guido van Rossum | dfa6790 | 1997-12-08 17:12:06 +0000 | [diff] [blame] | 132 | |
Guido van Rossum | 71fa97c | 1997-07-18 04:26:03 +0000 | [diff] [blame] | 133 | return self.subn(repl, string, count)[0] |
Guido van Rossum | a0e4c1b | 1997-07-17 14:52:48 +0000 | [diff] [blame] | 134 | |
Guido van Rossum | dfa6790 | 1997-12-08 17:12:06 +0000 | [diff] [blame] | 135 | def subn(self, repl, source, count=0): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 136 | """Return a 2-tuple containing (new_string, number). |
| 137 | new_string is the string obtained by replacing the leftmost |
Guido van Rossum | 8430c58 | 1998-04-03 21:47:12 +0000 | [diff] [blame] | 138 | non-overlapping occurrences of the pattern in the source |
| 139 | string by the replacement repl. number is the number of |
| 140 | substitutions that were made.""" |
Guido van Rossum | 0e5ab17 | 1998-07-17 20:18:49 +0000 | [diff] [blame] | 141 | |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 142 | if count < 0: |
| 143 | raise error, "negative substitution count" |
| 144 | if count == 0: |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 145 | count = sys.maxint |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 146 | n = 0 # Number of matches |
| 147 | pos = 0 # Where to start searching |
| 148 | lastmatch = -1 # End of last match |
| 149 | results = [] # Substrings making up the result |
| 150 | end = len(source) |
Guido van Rossum | 0e5ab17 | 1998-07-17 20:18:49 +0000 | [diff] [blame] | 151 | |
| 152 | if type(repl) is type(''): |
| 153 | # See if repl contains group references |
| 154 | try: |
| 155 | repl = pcre_expand(_Dummy, repl) |
| 156 | except: |
| 157 | m = MatchObject(self, source, 0, end, []) |
| 158 | repl = lambda m, repl=repl, expand=pcre_expand: expand(m, repl) |
| 159 | else: |
| 160 | m = None |
| 161 | else: |
| 162 | m = MatchObject(self, source, 0, end, []) |
| 163 | |
| 164 | match = self.code.match |
| 165 | append = results.append |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 166 | while n < count and pos <= end: |
Guido van Rossum | 0e5ab17 | 1998-07-17 20:18:49 +0000 | [diff] [blame] | 167 | regs = match(source, pos, end, 0) |
| 168 | if not regs: |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 169 | break |
Andrew M. Kuchling | 9a80e00 | 1998-08-21 18:39:38 +0000 | [diff] [blame] | 170 | self._num_regs = len(regs) |
Guido van Rossum | 0e5ab17 | 1998-07-17 20:18:49 +0000 | [diff] [blame] | 171 | i, j = regs[0] |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 172 | if i == j == lastmatch: |
| 173 | # Empty match adjacent to previous match |
| 174 | pos = pos + 1 |
Guido van Rossum | 0e5ab17 | 1998-07-17 20:18:49 +0000 | [diff] [blame] | 175 | append(source[lastmatch:pos]) |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 176 | continue |
| 177 | if pos < i: |
Guido van Rossum | 0e5ab17 | 1998-07-17 20:18:49 +0000 | [diff] [blame] | 178 | append(source[pos:i]) |
| 179 | if m: |
| 180 | m.pos = pos |
| 181 | m.regs = regs |
| 182 | append(repl(m)) |
| 183 | else: |
| 184 | append(repl) |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 185 | pos = lastmatch = j |
| 186 | if i == j: |
| 187 | # Last match was empty; don't try here again |
| 188 | pos = pos + 1 |
Guido van Rossum | 0e5ab17 | 1998-07-17 20:18:49 +0000 | [diff] [blame] | 189 | append(source[lastmatch:pos]) |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 190 | n = n + 1 |
Guido van Rossum | 0e5ab17 | 1998-07-17 20:18:49 +0000 | [diff] [blame] | 191 | append(source[pos:]) |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 192 | return (string.join(results, ''), n) |
| 193 | |
Guido van Rossum | 9e18ec7 | 1997-07-17 22:39:13 +0000 | [diff] [blame] | 194 | def split(self, source, maxsplit=0): |
Guido van Rossum | be0b62c | 1998-06-29 20:29:08 +0000 | [diff] [blame] | 195 | """Split the source string by the occurrences of the pattern, |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 196 | returning a list containing the resulting substrings.""" |
Guido van Rossum | dfa6790 | 1997-12-08 17:12:06 +0000 | [diff] [blame] | 197 | |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 198 | if maxsplit < 0: |
| 199 | raise error, "negative split count" |
| 200 | if maxsplit == 0: |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 201 | maxsplit = sys.maxint |
| 202 | n = 0 |
| 203 | pos = 0 |
| 204 | lastmatch = 0 |
| 205 | results = [] |
| 206 | end = len(source) |
Guido van Rossum | 0e5ab17 | 1998-07-17 20:18:49 +0000 | [diff] [blame] | 207 | match = self.code.match |
| 208 | append = results.append |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 209 | while n < maxsplit: |
Guido van Rossum | 0e5ab17 | 1998-07-17 20:18:49 +0000 | [diff] [blame] | 210 | regs = match(source, pos, end, 0) |
| 211 | if not regs: |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 212 | break |
Guido van Rossum | 0e5ab17 | 1998-07-17 20:18:49 +0000 | [diff] [blame] | 213 | i, j = regs[0] |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 214 | if i == j: |
| 215 | # Empty match |
| 216 | if pos >= end: |
| 217 | break |
| 218 | pos = pos+1 |
| 219 | continue |
Guido van Rossum | 0e5ab17 | 1998-07-17 20:18:49 +0000 | [diff] [blame] | 220 | append(source[lastmatch:i]) |
| 221 | rest = regs[1:] |
| 222 | if rest: |
| 223 | for a, b in rest: |
| 224 | if a == -1 or b == -1: |
| 225 | group = None |
| 226 | else: |
| 227 | group = source[a:b] |
| 228 | append(group) |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 229 | pos = lastmatch = j |
| 230 | n = n + 1 |
Guido van Rossum | 0e5ab17 | 1998-07-17 20:18:49 +0000 | [diff] [blame] | 231 | append(source[lastmatch:]) |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 232 | return results |
Guido van Rossum | 9e18ec7 | 1997-07-17 22:39:13 +0000 | [diff] [blame] | 233 | |
Guido van Rossum | 0e5ab17 | 1998-07-17 20:18:49 +0000 | [diff] [blame] | 234 | def findall(self, source): |
Guido van Rossum | be0b62c | 1998-06-29 20:29:08 +0000 | [diff] [blame] | 235 | """Return a list of all non-overlapping matches in the string. |
| 236 | |
| 237 | If one or more groups are present in the pattern, return a |
| 238 | list of groups; this will be a list of tuples if the pattern |
| 239 | has more than one group. |
| 240 | |
| 241 | Empty matches are included in the result. |
| 242 | |
| 243 | """ |
| 244 | pos = 0 |
Guido van Rossum | 0e5ab17 | 1998-07-17 20:18:49 +0000 | [diff] [blame] | 245 | end = len(source) |
| 246 | results = [] |
| 247 | match = self.code.match |
| 248 | append = results.append |
| 249 | while pos <= end: |
| 250 | regs = match(source, pos, end, 0) |
| 251 | if not regs: |
Guido van Rossum | be0b62c | 1998-06-29 20:29:08 +0000 | [diff] [blame] | 252 | break |
Guido van Rossum | 0e5ab17 | 1998-07-17 20:18:49 +0000 | [diff] [blame] | 253 | i, j = regs[0] |
| 254 | rest = regs[1:] |
| 255 | if not rest: |
| 256 | gr = source[i:j] |
| 257 | elif len(rest) == 1: |
| 258 | a, b = rest[0] |
| 259 | gr = source[a:b] |
| 260 | else: |
| 261 | gr = [] |
| 262 | for (a, b) in rest: |
| 263 | gr.append(source[a:b]) |
| 264 | gr = tuple(gr) |
| 265 | append(gr) |
| 266 | pos = max(j, pos+1) |
| 267 | return results |
Guido van Rossum | be0b62c | 1998-06-29 20:29:08 +0000 | [diff] [blame] | 268 | |
Guido van Rossum | dfa6790 | 1997-12-08 17:12:06 +0000 | [diff] [blame] | 269 | # The following 3 functions were contributed by Mike Fletcher, and |
| 270 | # allow pickling and unpickling of RegexObject instances. |
| 271 | def __getinitargs__(self): |
| 272 | return (None,None,None,None) # any 4 elements, to work around |
| 273 | # problems with the |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 274 | # pickle/cPickle modules not yet |
| 275 | # ignoring the __init__ function |
Guido van Rossum | dfa6790 | 1997-12-08 17:12:06 +0000 | [diff] [blame] | 276 | def __getstate__(self): |
| 277 | return self.pattern, self.flags, self.groupindex |
| 278 | def __setstate__(self, statetuple): |
| 279 | self.pattern = statetuple[0] |
| 280 | self.flags = statetuple[1] |
| 281 | self.groupindex = statetuple[2] |
| 282 | self.code = apply(pcre_compile, statetuple) |
| 283 | |
Guido van Rossum | 0e5ab17 | 1998-07-17 20:18:49 +0000 | [diff] [blame] | 284 | class _Dummy: |
| 285 | # Dummy class used by _subn_string(). Has 'group' to avoid core dump. |
| 286 | group = None |
| 287 | |
Guido van Rossum | 5ca1b71 | 1997-07-10 21:00:31 +0000 | [diff] [blame] | 288 | class MatchObject: |
Guido van Rossum | be0b62c | 1998-06-29 20:29:08 +0000 | [diff] [blame] | 289 | |
Guido van Rossum | dfa6790 | 1997-12-08 17:12:06 +0000 | [diff] [blame] | 290 | def __init__(self, re, string, pos, endpos, regs): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 291 | self.re = re |
| 292 | self.string = string |
| 293 | self.pos = pos |
| 294 | self.endpos = endpos |
| 295 | self.regs = regs |
| 296 | |
Guido van Rossum | dfa6790 | 1997-12-08 17:12:06 +0000 | [diff] [blame] | 297 | def start(self, g = 0): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 298 | "Return the start of the substring matched by group g" |
| 299 | if type(g) == type(''): |
| 300 | try: |
| 301 | g = self.re.groupindex[g] |
| 302 | except (KeyError, TypeError): |
Guido van Rossum | be0b62c | 1998-06-29 20:29:08 +0000 | [diff] [blame] | 303 | raise IndexError, 'group %s is undefined' % `g` |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 304 | return self.regs[g][0] |
Guido van Rossum | a0e4c1b | 1997-07-17 14:52:48 +0000 | [diff] [blame] | 305 | |
Guido van Rossum | dfa6790 | 1997-12-08 17:12:06 +0000 | [diff] [blame] | 306 | def end(self, g = 0): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 307 | "Return the end of the substring matched by group g" |
| 308 | if type(g) == type(''): |
| 309 | try: |
| 310 | g = self.re.groupindex[g] |
| 311 | except (KeyError, TypeError): |
Guido van Rossum | be0b62c | 1998-06-29 20:29:08 +0000 | [diff] [blame] | 312 | raise IndexError, 'group %s is undefined' % `g` |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 313 | return self.regs[g][1] |
Guido van Rossum | a0e4c1b | 1997-07-17 14:52:48 +0000 | [diff] [blame] | 314 | |
Guido van Rossum | dfa6790 | 1997-12-08 17:12:06 +0000 | [diff] [blame] | 315 | def span(self, g = 0): |
Guido van Rossum | be0b62c | 1998-06-29 20:29:08 +0000 | [diff] [blame] | 316 | "Return (start, end) of the substring matched by group g" |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 317 | if type(g) == type(''): |
| 318 | try: |
| 319 | g = self.re.groupindex[g] |
| 320 | except (KeyError, TypeError): |
Guido van Rossum | be0b62c | 1998-06-29 20:29:08 +0000 | [diff] [blame] | 321 | raise IndexError, 'group %s is undefined' % `g` |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 322 | return self.regs[g] |
Guido van Rossum | a0e4c1b | 1997-07-17 14:52:48 +0000 | [diff] [blame] | 323 | |
Guido van Rossum | be0b62c | 1998-06-29 20:29:08 +0000 | [diff] [blame] | 324 | def groups(self, default=None): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 325 | "Return a tuple containing all subgroups of the match object" |
| 326 | result = [] |
| 327 | for g in range(1, self.re._num_regs): |
Guido van Rossum | be0b62c | 1998-06-29 20:29:08 +0000 | [diff] [blame] | 328 | a, b = self.regs[g] |
| 329 | if a == -1 or b == -1: |
| 330 | result.append(default) |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 331 | else: |
Guido van Rossum | be0b62c | 1998-06-29 20:29:08 +0000 | [diff] [blame] | 332 | result.append(self.string[a:b]) |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 333 | return tuple(result) |
Guido van Rossum | dfa6790 | 1997-12-08 17:12:06 +0000 | [diff] [blame] | 334 | |
Guido van Rossum | 8a9a4a2 | 1997-07-11 20:48:25 +0000 | [diff] [blame] | 335 | def group(self, *groups): |
Guido van Rossum | be0b62c | 1998-06-29 20:29:08 +0000 | [diff] [blame] | 336 | "Return one or more groups of the match" |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 337 | if len(groups) == 0: |
| 338 | groups = (0,) |
| 339 | result = [] |
| 340 | for g in groups: |
| 341 | if type(g) == type(''): |
| 342 | try: |
| 343 | g = self.re.groupindex[g] |
| 344 | except (KeyError, TypeError): |
Guido van Rossum | be0b62c | 1998-06-29 20:29:08 +0000 | [diff] [blame] | 345 | raise IndexError, 'group %s is undefined' % `g` |
| 346 | if g >= len(self.regs): |
| 347 | raise IndexError, 'group %s is undefined' % `g` |
| 348 | a, b = self.regs[g] |
| 349 | if a == -1 or b == -1: |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 350 | result.append(None) |
| 351 | else: |
Guido van Rossum | be0b62c | 1998-06-29 20:29:08 +0000 | [diff] [blame] | 352 | result.append(self.string[a:b]) |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 353 | if len(result) > 1: |
| 354 | return tuple(result) |
| 355 | elif len(result) == 1: |
| 356 | return result[0] |
| 357 | else: |
| 358 | return () |
Guido van Rossum | be0b62c | 1998-06-29 20:29:08 +0000 | [diff] [blame] | 359 | |
| 360 | def groupdict(self, default=None): |
| 361 | "Return a dictionary containing all named subgroups of the match" |
| 362 | dict = {} |
| 363 | for name, index in self.re.groupindex.items(): |
| 364 | a, b = self.regs[index] |
| 365 | if a == -1 or b == -1: |
| 366 | dict[name] = default |
| 367 | else: |
| 368 | dict[name] = self.string[a:b] |
| 369 | return dict |