At the release of 1.0.1.
[python/dscho.git] / Lib / regexp.py
blob613681488b1bda29ef81277000b15c90efaacc60
1 # Provide backward compatibility for module "regexp" using "regex".
3 import regex
4 from regex_syntax import *
6 class 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 def match(self, *args):
14 if len(args) == 2:
15 str, offset = args
16 elif len(args) == 1:
17 str, offset = args[0], 0
18 else:
19 raise TypeError, 'wrong argument count'
20 if self.prog.search(str, offset) < 0:
21 return ()
22 regs = self.prog.regs
23 i = len(regs)
24 while i > 0 and regs[i-1] == (-1, -1):
25 i = i-1
26 return regs[:i]
28 def compile(pat):
29 return Prog(pat)
31 cache_pat = None
32 cache_prog = None
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)