home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / pyth_os2.zip / python-1.0.2 / Lib / regexp.py < prev    next >
Text File  |  1993-12-29  |  841b  |  39 lines

  1. # Provide backward compatibility for module "regexp" using "regex".
  2.  
  3. import regex
  4. from regex_syntax import *
  5.  
  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]
  27.  
  28. def compile(pat):
  29.     return Prog(pat)
  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)
  39.