1997-07-10 18:00:31 -03:00
|
|
|
#!/usr/bin/env python
|
|
|
|
# -*- mode: python -*-
|
|
|
|
# $Id$
|
|
|
|
|
1997-08-13 00:25:34 -03:00
|
|
|
|
1997-10-06 11:45:17 -03:00
|
|
|
import sys
|
|
|
|
import string
|
|
|
|
from pcre import *
|
1997-07-10 18:00:31 -03:00
|
|
|
|
|
|
|
#
|
1997-10-06 11:45:17 -03:00
|
|
|
# First, the public part of the interface:
|
1997-07-10 18:00:31 -03:00
|
|
|
#
|
|
|
|
|
1997-10-06 11:45:17 -03:00
|
|
|
# pcre.error and re.error should be the same, since exceptions can be
|
|
|
|
# raised from either module.
|
|
|
|
|
|
|
|
# compilation flags
|
|
|
|
|
|
|
|
I = IGNORECASE
|
|
|
|
M = MULTILINE
|
|
|
|
S = DOTALL
|
|
|
|
X = VERBOSE
|
1997-07-15 12:38:20 -03:00
|
|
|
|
|
|
|
#
|
|
|
|
#
|
|
|
|
#
|
|
|
|
|
1997-07-15 15:59:04 -03:00
|
|
|
_cache = {}
|
|
|
|
_MAXCACHE = 20
|
1997-07-17 11:52:48 -03:00
|
|
|
|
1997-07-17 19:39:13 -03:00
|
|
|
def _cachecompile(pattern, flags=0):
|
1997-07-15 15:59:04 -03:00
|
|
|
key = (pattern, flags)
|
|
|
|
try:
|
|
|
|
return _cache[key]
|
|
|
|
except KeyError:
|
|
|
|
pass
|
|
|
|
value = compile(pattern, flags)
|
|
|
|
if len(_cache) >= _MAXCACHE:
|
|
|
|
_cache.clear()
|
|
|
|
_cache[key] = value
|
|
|
|
return value
|
|
|
|
|
1997-07-10 18:00:31 -03:00
|
|
|
def match(pattern, string, flags=0):
|
1997-07-15 15:59:04 -03:00
|
|
|
return _cachecompile(pattern, flags).match(string)
|
1997-07-17 11:52:48 -03:00
|
|
|
|
1997-07-10 18:00:31 -03:00
|
|
|
def search(pattern, string, flags=0):
|
1997-07-15 15:59:04 -03:00
|
|
|
return _cachecompile(pattern, flags).search(string)
|
1997-07-17 11:52:48 -03:00
|
|
|
|
1997-07-10 18:00:31 -03:00
|
|
|
def sub(pattern, repl, string, count=0):
|
1997-07-17 19:39:13 -03:00
|
|
|
if type(pattern) == type(''):
|
|
|
|
pattern = _cachecompile(pattern)
|
|
|
|
return pattern.sub(repl, string, count)
|
1997-07-10 18:00:31 -03:00
|
|
|
|
|
|
|
def subn(pattern, repl, string, count=0):
|
1997-07-17 19:39:13 -03:00
|
|
|
if type(pattern) == type(''):
|
|
|
|
pattern = _cachecompile(pattern)
|
|
|
|
return pattern.subn(repl, string, count)
|
1997-07-17 11:52:48 -03:00
|
|
|
|
1997-07-11 17:48:25 -03:00
|
|
|
def split(pattern, string, maxsplit=0):
|
1997-07-17 19:39:13 -03:00
|
|
|
if type(pattern) == type(''):
|
|
|
|
pattern = _cachecompile(pattern)
|
|
|
|
return pattern.split(string, maxsplit)
|
1997-07-10 18:00:31 -03:00
|
|
|
|
|
|
|
#
|
|
|
|
#
|
|
|
|
#
|
|
|
|
|
|
|
|
class RegexObject:
|
1997-10-06 11:45:17 -03:00
|
|
|
def __init__(self, pattern, flags, code, groupindex):
|
|
|
|
self.code = code
|
1997-07-10 18:00:31 -03:00
|
|
|
self.flags = flags
|
|
|
|
self.pattern = pattern
|
|
|
|
self.groupindex = groupindex
|
|
|
|
def search(self, string, pos=0):
|
1997-10-06 11:45:17 -03:00
|
|
|
regs = self.code.match(string, pos, 0)
|
1997-07-10 18:00:31 -03:00
|
|
|
if regs is None:
|
|
|
|
return None
|
1997-10-06 11:45:17 -03:00
|
|
|
self.num_regs=len(regs)
|
1997-07-17 11:52:48 -03:00
|
|
|
|
1997-07-10 18:00:31 -03:00
|
|
|
return MatchObject(self,
|
|
|
|
string,
|
|
|
|
pos,
|
|
|
|
regs)
|
1997-07-17 11:52:48 -03:00
|
|
|
|
1997-07-11 17:48:25 -03:00
|
|
|
def match(self, string, pos=0):
|
1997-10-06 11:45:17 -03:00
|
|
|
regs = self.code.match(string, pos, ANCHORED)
|
1997-07-11 17:48:25 -03:00
|
|
|
if regs is None:
|
|
|
|
return None
|
1997-10-06 11:45:17 -03:00
|
|
|
self.num_regs=len(regs)/2
|
1997-07-11 17:48:25 -03:00
|
|
|
return MatchObject(self,
|
|
|
|
string,
|
|
|
|
pos,
|
|
|
|
regs)
|
1997-07-17 11:52:48 -03:00
|
|
|
|
1997-07-11 17:48:25 -03:00
|
|
|
def sub(self, repl, string, count=0):
|
1997-07-18 01:26:03 -03:00
|
|
|
return self.subn(repl, string, count)[0]
|
1997-07-17 11:52:48 -03:00
|
|
|
|
1997-07-17 19:39:13 -03:00
|
|
|
def subn(self, repl, source, count=0):
|
1997-07-18 01:26:03 -03:00
|
|
|
if count < 0:
|
1997-10-06 11:45:17 -03:00
|
|
|
raise error, "negative substitution count"
|
1997-07-18 01:26:03 -03:00
|
|
|
if count == 0:
|
|
|
|
import sys
|
|
|
|
count = sys.maxint
|
1997-07-17 19:39:13 -03:00
|
|
|
if type(repl) == type(''):
|
|
|
|
if '\\' in repl:
|
1997-10-06 11:45:17 -03:00
|
|
|
repl = lambda m, r=repl: pcre_expand(m, r)
|
1997-07-17 19:39:13 -03:00
|
|
|
else:
|
|
|
|
repl = lambda m, r=repl: r
|
1997-07-18 01:26:03 -03:00
|
|
|
n = 0 # Number of matches
|
|
|
|
pos = 0 # Where to start searching
|
|
|
|
lastmatch = -1 # End of last match
|
|
|
|
results = [] # Substrings making up the result
|
1997-07-17 19:39:13 -03:00
|
|
|
end = len(source)
|
|
|
|
while n < count and pos <= end:
|
|
|
|
m = self.search(source, pos)
|
1997-07-18 01:26:03 -03:00
|
|
|
if not m:
|
|
|
|
break
|
1997-07-17 19:39:13 -03:00
|
|
|
i, j = m.span(0)
|
|
|
|
if i == j == lastmatch:
|
|
|
|
# Empty match adjacent to previous match
|
1997-07-18 01:26:03 -03:00
|
|
|
pos = pos + 1
|
1997-07-17 19:39:13 -03:00
|
|
|
results.append(source[lastmatch:pos])
|
|
|
|
continue
|
1997-07-18 01:26:03 -03:00
|
|
|
if pos < i:
|
|
|
|
results.append(source[pos:i])
|
1997-07-17 19:39:13 -03:00
|
|
|
results.append(repl(m))
|
|
|
|
pos = lastmatch = j
|
|
|
|
if i == j:
|
|
|
|
# Last match was empty; don't try here again
|
1997-07-18 01:26:03 -03:00
|
|
|
pos = pos + 1
|
1997-07-17 19:39:13 -03:00
|
|
|
results.append(source[lastmatch:pos])
|
1997-07-18 01:26:03 -03:00
|
|
|
n = n + 1
|
1997-07-17 19:39:13 -03:00
|
|
|
results.append(source[pos:])
|
|
|
|
return (string.join(results, ''), n)
|
1997-07-18 01:26:03 -03:00
|
|
|
|
1997-07-17 19:39:13 -03:00
|
|
|
def split(self, source, maxsplit=0):
|
1997-07-18 01:26:03 -03:00
|
|
|
if maxsplit < 0:
|
|
|
|
raise error, "negative split count"
|
|
|
|
if maxsplit == 0:
|
|
|
|
import sys
|
|
|
|
maxsplit = sys.maxint
|
1997-07-17 19:39:13 -03:00
|
|
|
n = 0
|
|
|
|
pos = 0
|
|
|
|
lastmatch = 0
|
|
|
|
results = []
|
|
|
|
end = len(source)
|
|
|
|
while n < maxsplit:
|
|
|
|
m = self.search(source, pos)
|
1997-07-18 01:26:03 -03:00
|
|
|
if not m:
|
|
|
|
break
|
1997-07-17 19:39:13 -03:00
|
|
|
i, j = m.span(0)
|
|
|
|
if i == j:
|
|
|
|
# Empty match
|
1997-07-18 01:26:03 -03:00
|
|
|
if pos >= end:
|
|
|
|
break
|
1997-07-17 19:39:13 -03:00
|
|
|
pos = pos+1
|
|
|
|
continue
|
|
|
|
results.append(source[lastmatch:i])
|
|
|
|
g = m.group()
|
|
|
|
if g:
|
|
|
|
results[len(results):] = list(g)
|
|
|
|
pos = lastmatch = j
|
|
|
|
results.append(source[lastmatch:])
|
|
|
|
return results
|
|
|
|
|
1997-07-10 18:00:31 -03:00
|
|
|
class MatchObject:
|
|
|
|
def __init__(self, re, string, pos, regs):
|
|
|
|
self.re = re
|
|
|
|
self.string = string
|
|
|
|
self.pos = pos
|
|
|
|
self.regs = regs
|
1997-07-17 11:52:48 -03:00
|
|
|
|
1997-07-11 17:48:25 -03:00
|
|
|
def start(self, g):
|
|
|
|
if type(g) == type(''):
|
1997-07-10 18:00:31 -03:00
|
|
|
try:
|
1997-07-11 17:48:25 -03:00
|
|
|
g = self.re.groupindex[g]
|
1997-07-10 18:00:31 -03:00
|
|
|
except (KeyError, TypeError):
|
1997-07-11 17:48:25 -03:00
|
|
|
raise IndexError, ('group "' + g + '" is undefined')
|
|
|
|
return self.regs[g][0]
|
1997-07-17 11:52:48 -03:00
|
|
|
|
1997-07-11 17:48:25 -03:00
|
|
|
def end(self, g):
|
|
|
|
if type(g) == type(''):
|
1997-07-10 18:00:31 -03:00
|
|
|
try:
|
1997-07-11 17:48:25 -03:00
|
|
|
g = self.re.groupindex[g]
|
1997-07-10 18:00:31 -03:00
|
|
|
except (KeyError, TypeError):
|
1997-07-11 17:48:25 -03:00
|
|
|
raise IndexError, ('group "' + g + '" is undefined')
|
|
|
|
return self.regs[g][1]
|
1997-07-17 11:52:48 -03:00
|
|
|
|
1997-07-11 17:48:25 -03:00
|
|
|
def span(self, g):
|
|
|
|
if type(g) == type(''):
|
1997-07-10 18:00:31 -03:00
|
|
|
try:
|
1997-07-11 17:48:25 -03:00
|
|
|
g = self.re.groupindex[g]
|
1997-07-10 18:00:31 -03:00
|
|
|
except (KeyError, TypeError):
|
1997-07-11 17:48:25 -03:00
|
|
|
raise IndexError, ('group "' + g + '" is undefined')
|
|
|
|
return self.regs[g]
|
1997-07-17 11:52:48 -03:00
|
|
|
|
1997-07-11 17:48:25 -03:00
|
|
|
def group(self, *groups):
|
|
|
|
if len(groups) == 0:
|
|
|
|
groups = range(1, self.re.num_regs)
|
1997-07-15 12:40:29 -03:00
|
|
|
use_all = 1
|
|
|
|
else:
|
|
|
|
use_all = 0
|
1997-07-11 17:48:25 -03:00
|
|
|
result = []
|
|
|
|
for g in groups:
|
|
|
|
if type(g) == type(''):
|
|
|
|
try:
|
|
|
|
g = self.re.groupindex[g]
|
|
|
|
except (KeyError, TypeError):
|
|
|
|
raise IndexError, ('group "' + g + '" is undefined')
|
1997-10-06 11:45:17 -03:00
|
|
|
if len(self.regs)<=g: raise IndexError, ('group "' + str(g) + '" is undefined')
|
|
|
|
elif (self.regs[g][0] == -1) or (self.regs[g][1] == -1):
|
1997-07-11 17:48:25 -03:00
|
|
|
result.append(None)
|
|
|
|
else:
|
|
|
|
result.append(self.string[self.regs[g][0]:self.regs[g][1]])
|
1997-07-15 12:40:29 -03:00
|
|
|
if use_all or len(result) > 1:
|
1997-07-11 17:48:25 -03:00
|
|
|
return tuple(result)
|
|
|
|
elif len(result) == 1:
|
|
|
|
return result[0]
|
|
|
|
else:
|
|
|
|
return ()
|
1997-07-10 18:00:31 -03:00
|
|
|
|
|
|
|
def escape(pattern):
|
1997-07-11 17:48:25 -03:00
|
|
|
result = []
|
1997-10-06 11:45:17 -03:00
|
|
|
alphanum=string.letters+'_'+string.digits
|
1997-07-10 18:00:31 -03:00
|
|
|
for char in pattern:
|
1997-10-06 11:45:17 -03:00
|
|
|
if char not in alphanum:
|
1997-07-11 17:48:25 -03:00
|
|
|
result.append('\\')
|
|
|
|
result.append(char)
|
|
|
|
return string.join(result, '')
|
1997-07-10 18:00:31 -03:00
|
|
|
|
1997-10-06 11:45:17 -03:00
|
|
|
def compile(pattern, flags=0):
|
|
|
|
groupindex={}
|
|
|
|
code=pcre_compile(pattern, flags, groupindex)
|
|
|
|
return RegexObject(pattern, flags, code, groupindex)
|
|
|
|
|
1997-07-15 11:38:13 -03:00
|
|
|
|