blob: 755f65ad7f45804ce08a11ed4bba9b20f8492ea8 [file] [log] [blame]
Guido van Rossum6c6b78d1991-12-30 16:03:05 +00001# Provide backward compatibility for module "regexp" using "regex".
2
3import regex
4from regex_syntax import *
5
6class Prog:
7 def init(self, pat):
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)
13 return self
Guido van Rossumfea2af11993-01-04 09:16:51 +000014 def match(self, *args):
15 if len(args) == 2:
Guido van Rossum6c6b78d1991-12-30 16:03:05 +000016 str, offset = args
Guido van Rossumfea2af11993-01-04 09:16:51 +000017 elif len(args) == 1:
18 str, offset = args[0], 0
Guido van Rossum6c6b78d1991-12-30 16:03:05 +000019 else:
Guido van Rossumfea2af11993-01-04 09:16:51 +000020 raise TypeError, 'wrong argument count'
Guido van Rossum6c6b78d1991-12-30 16:03:05 +000021 if self.prog.search(str, offset) < 0:
22 return ()
Guido van Rossum42d1f631992-01-01 19:33:02 +000023 regs = self.prog.regs
Guido van Rossum6c6b78d1991-12-30 16:03:05 +000024 i = len(regs)
Guido van Rossum42d1f631992-01-01 19:33:02 +000025 while i > 0 and regs[i-1] == (-1, -1):
Guido van Rossum6c6b78d1991-12-30 16:03:05 +000026 i = i-1
27 return regs[:i]
28
29def compile(pat):
30 return Prog().init(pat)
31
32cache_pat = None
33cache_prog = None
34
35def match(pat, str):
36 global cache_pat, cache_prog
37 if pat <> cache_pat:
38 cache_pat, cache_prog = pat, compile(pat)
39 return cache_prog.match(str)