Guido van Rossum | 6c6b78d | 1991-12-30 16:03:05 +0000 | [diff] [blame] | 1 | # Provide backward compatibility for module "regexp" using "regex". |
| 2 | |
| 3 | import regex |
| 4 | from regex_syntax import * |
| 5 | |
| 6 | class Prog: |
Guido van Rossum | 7bc817d | 1993-12-17 15:25:27 +0000 | [diff] [blame^] | 7 | def __init__(self, pat): |
Guido van Rossum | 6c6b78d | 1991-12-30 16:03:05 +0000 | [diff] [blame] | 8 | save_syntax = regex.set_syntax(RE_SYNTAX_AWK) |
| 9 | try: |
| 10 | self.prog = regex.compile(pat) |
| 11 | finally: |
| 12 | xxx = regex.set_syntax(save_syntax) |
Guido van Rossum | fea2af1 | 1993-01-04 09:16:51 +0000 | [diff] [blame] | 13 | def match(self, *args): |
| 14 | if len(args) == 2: |
Guido van Rossum | 6c6b78d | 1991-12-30 16:03:05 +0000 | [diff] [blame] | 15 | str, offset = args |
Guido van Rossum | fea2af1 | 1993-01-04 09:16:51 +0000 | [diff] [blame] | 16 | elif len(args) == 1: |
| 17 | str, offset = args[0], 0 |
Guido van Rossum | 6c6b78d | 1991-12-30 16:03:05 +0000 | [diff] [blame] | 18 | else: |
Guido van Rossum | fea2af1 | 1993-01-04 09:16:51 +0000 | [diff] [blame] | 19 | raise TypeError, 'wrong argument count' |
Guido van Rossum | 6c6b78d | 1991-12-30 16:03:05 +0000 | [diff] [blame] | 20 | if self.prog.search(str, offset) < 0: |
| 21 | return () |
Guido van Rossum | 42d1f63 | 1992-01-01 19:33:02 +0000 | [diff] [blame] | 22 | regs = self.prog.regs |
Guido van Rossum | 6c6b78d | 1991-12-30 16:03:05 +0000 | [diff] [blame] | 23 | i = len(regs) |
Guido van Rossum | 42d1f63 | 1992-01-01 19:33:02 +0000 | [diff] [blame] | 24 | while i > 0 and regs[i-1] == (-1, -1): |
Guido van Rossum | 6c6b78d | 1991-12-30 16:03:05 +0000 | [diff] [blame] | 25 | i = i-1 |
| 26 | return regs[:i] |
| 27 | |
| 28 | def compile(pat): |
Guido van Rossum | 7bc817d | 1993-12-17 15:25:27 +0000 | [diff] [blame^] | 29 | return Prog(pat) |
Guido van Rossum | 6c6b78d | 1991-12-30 16:03:05 +0000 | [diff] [blame] | 30 | |
| 31 | cache_pat = None |
| 32 | cache_prog = None |
| 33 | |
| 34 | def match(pat, str): |
| 35 | global cache_pat, cache_prog |
| 36 | if pat <> cache_pat: |
| 37 | cache_pat, cache_prog = pat, compile(pat) |
| 38 | return cache_prog.match(str) |