regexp.py 706 Bytes
Newer Older
Guido van Rossum's avatar
Guido van Rossum committed
1 2 3 4 5 6
# Provide backward compatibility for module "regexp" using "regex".

import regex
from regex_syntax import *

class Prog:
7
	def __init__(self, pat):
Guido van Rossum's avatar
Guido van Rossum committed
8 9 10 11 12
		save_syntax = regex.set_syntax(RE_SYNTAX_AWK)
		try:
			self.prog = regex.compile(pat)
		finally:
			xxx = regex.set_syntax(save_syntax)
13
	def match(self, str, offset = 0):
Guido van Rossum's avatar
Guido van Rossum committed
14 15
		if self.prog.search(str, offset) < 0:
			return ()
16
		regs = self.prog.regs
Guido van Rossum's avatar
Guido van Rossum committed
17
		i = len(regs)
18
		while i > 0 and regs[i-1] == (-1, -1):
Guido van Rossum's avatar
Guido van Rossum committed
19 20 21 22
			i = i-1
		return regs[:i]

def compile(pat):
23
	return Prog(pat)
Guido van Rossum's avatar
Guido van Rossum committed
24 25 26 27 28 29 30 31 32

cache_pat = None
cache_prog = None

def match(pat, str):
	global cache_pat, cache_prog
	if pat <> cache_pat:
		cache_pat, cache_prog = pat, compile(pat)
	return cache_prog.match(str)