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
|
1997-12-08 13:12:06 -04:00
|
|
|
# raised from either module.
|
1997-10-06 11:45:17 -03:00
|
|
|
|
|
|
|
# compilation flags
|
|
|
|
|
|
|
|
I = IGNORECASE
|
1997-12-08 13:12:06 -04:00
|
|
|
L = LOCALE
|
1997-10-06 11:45:17 -03:00
|
|
|
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:
|
1998-03-26 17:13:24 -04:00
|
|
|
return _cache[key]
|
1997-07-15 15:59:04 -03:00
|
|
|
except KeyError:
|
1998-03-26 17:13:24 -04:00
|
|
|
pass
|
1997-07-15 15:59:04 -03:00
|
|
|
value = compile(pattern, flags)
|
|
|
|
if len(_cache) >= _MAXCACHE:
|
1998-03-26 17:13:24 -04:00
|
|
|
_cache.clear()
|
1997-07-15 15:59:04 -03:00
|
|
|
_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(''):
|
1998-03-26 17:13:24 -04:00
|
|
|
pattern = _cachecompile(pattern)
|
1997-07-17 19:39:13 -03:00
|
|
|
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(''):
|
1998-03-26 17:13:24 -04:00
|
|
|
pattern = _cachecompile(pattern)
|
1997-07-17 19:39:13 -03:00
|
|
|
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(''):
|
1998-03-26 17:13:24 -04:00
|
|
|
pattern = _cachecompile(pattern)
|
1997-07-17 19:39:13 -03:00
|
|
|
return pattern.split(string, maxsplit)
|
1997-07-10 18:00:31 -03:00
|
|
|
|
1998-06-29 17:29:08 -03:00
|
|
|
def findall(pattern, string):
|
|
|
|
if type(pattern) == type(''):
|
|
|
|
pattern = _cachecompile(pattern)
|
|
|
|
return pattern.findall(string)
|
|
|
|
|
1997-12-08 13:12:06 -04:00
|
|
|
def escape(pattern):
|
|
|
|
"Escape all non-alphanumeric characters in pattern."
|
1998-07-20 12:46:13 -03:00
|
|
|
result = list(pattern)
|
1997-12-08 13:12:06 -04:00
|
|
|
alphanum=string.letters+'_'+string.digits
|
1998-07-20 12:46:13 -03:00
|
|
|
for i in range(len(pattern)):
|
|
|
|
char = pattern[i]
|
1998-03-26 17:13:24 -04:00
|
|
|
if char not in alphanum:
|
1998-07-20 12:46:13 -03:00
|
|
|
if char=='\000': result[i] = '\\000'
|
|
|
|
else: result[i] = '\\'+char
|
1997-12-08 13:12:06 -04:00
|
|
|
return string.join(result, '')
|
|
|
|
|
|
|
|
def compile(pattern, flags=0):
|
|
|
|
"Compile a regular expression pattern, returning a RegexObject."
|
|
|
|
groupindex={}
|
|
|
|
code=pcre_compile(pattern, flags, groupindex)
|
|
|
|
return RegexObject(pattern, flags, code, groupindex)
|
|
|
|
|
|
|
|
|
1997-07-10 18:00:31 -03:00
|
|
|
#
|
1997-12-08 13:12:06 -04:00
|
|
|
# Class definitions
|
1997-07-10 18:00:31 -03:00
|
|
|
#
|
|
|
|
|
|
|
|
class RegexObject:
|
1998-06-29 17:29:08 -03:00
|
|
|
|
1997-10-06 11:45:17 -03:00
|
|
|
def __init__(self, pattern, flags, code, groupindex):
|
1998-03-26 17:13:24 -04:00
|
|
|
self.code = code
|
|
|
|
self.flags = flags
|
|
|
|
self.pattern = pattern
|
|
|
|
self.groupindex = groupindex
|
1997-12-08 13:12:06 -04:00
|
|
|
|
|
|
|
def search(self, string, pos=0, endpos=None):
|
1998-03-26 17:13:24 -04:00
|
|
|
"""Scan through string looking for a match to the pattern, returning
|
|
|
|
a MatchObject instance, or None if no match was found."""
|
1997-12-08 13:12:06 -04:00
|
|
|
|
1998-03-26 17:13:24 -04:00
|
|
|
if endpos is None or endpos>len(string):
|
|
|
|
endpos=len(string)
|
|
|
|
if endpos<pos: endpos=pos
|
|
|
|
regs = self.code.match(string, pos, endpos, 0)
|
|
|
|
if regs is None:
|
|
|
|
return None
|
|
|
|
self._num_regs=len(regs)
|
|
|
|
|
|
|
|
return MatchObject(self,
|
|
|
|
string,
|
|
|
|
pos, endpos,
|
|
|
|
regs)
|
1997-07-17 11:52:48 -03:00
|
|
|
|
1997-12-08 13:12:06 -04:00
|
|
|
def match(self, string, pos=0, endpos=None):
|
1998-03-26 17:13:24 -04:00
|
|
|
"""Try to apply the pattern at the start of the string, returning
|
|
|
|
a MatchObject instance, or None if no match was found."""
|
1997-12-08 13:12:06 -04:00
|
|
|
|
1998-03-26 17:13:24 -04:00
|
|
|
if endpos is None or endpos>len(string):
|
|
|
|
endpos=len(string)
|
|
|
|
if endpos<pos: endpos=pos
|
|
|
|
regs = self.code.match(string, pos, endpos, ANCHORED)
|
|
|
|
if regs is None:
|
|
|
|
return None
|
|
|
|
self._num_regs=len(regs)
|
|
|
|
return MatchObject(self,
|
|
|
|
string,
|
|
|
|
pos, endpos,
|
|
|
|
regs)
|
1997-07-17 11:52:48 -03:00
|
|
|
|
1997-07-11 17:48:25 -03:00
|
|
|
def sub(self, repl, string, count=0):
|
1998-03-26 17:13:24 -04:00
|
|
|
"""Return the string obtained by replacing the leftmost
|
|
|
|
non-overlapping occurrences of the pattern in string by the
|
|
|
|
replacement repl"""
|
1997-12-08 13:12:06 -04:00
|
|
|
|
1997-07-18 01:26:03 -03:00
|
|
|
return self.subn(repl, string, count)[0]
|
1997-07-17 11:52:48 -03:00
|
|
|
|
1997-12-08 13:12:06 -04:00
|
|
|
def subn(self, repl, source, count=0):
|
1998-03-26 17:13:24 -04:00
|
|
|
"""Return a 2-tuple containing (new_string, number).
|
|
|
|
new_string is the string obtained by replacing the leftmost
|
1998-04-03 17:47:12 -04:00
|
|
|
non-overlapping occurrences of the pattern in the source
|
|
|
|
string by the replacement repl. number is the number of
|
|
|
|
substitutions that were made."""
|
1998-07-17 17:18:49 -03:00
|
|
|
|
1998-03-26 17:13:24 -04:00
|
|
|
if count < 0:
|
|
|
|
raise error, "negative substitution count"
|
|
|
|
if count == 0:
|
|
|
|
count = sys.maxint
|
|
|
|
n = 0 # Number of matches
|
|
|
|
pos = 0 # Where to start searching
|
|
|
|
lastmatch = -1 # End of last match
|
|
|
|
results = [] # Substrings making up the result
|
|
|
|
end = len(source)
|
1998-07-17 17:18:49 -03:00
|
|
|
|
|
|
|
if type(repl) is type(''):
|
|
|
|
# See if repl contains group references
|
|
|
|
try:
|
|
|
|
repl = pcre_expand(_Dummy, repl)
|
|
|
|
except:
|
|
|
|
m = MatchObject(self, source, 0, end, [])
|
|
|
|
repl = lambda m, repl=repl, expand=pcre_expand: expand(m, repl)
|
|
|
|
else:
|
|
|
|
m = None
|
|
|
|
else:
|
|
|
|
m = MatchObject(self, source, 0, end, [])
|
|
|
|
|
|
|
|
match = self.code.match
|
|
|
|
append = results.append
|
1998-03-26 17:13:24 -04:00
|
|
|
while n < count and pos <= end:
|
1998-07-17 17:18:49 -03:00
|
|
|
regs = match(source, pos, end, 0)
|
|
|
|
if not regs:
|
1998-03-26 17:13:24 -04:00
|
|
|
break
|
1998-08-21 15:39:38 -03:00
|
|
|
self._num_regs = len(regs)
|
1998-07-17 17:18:49 -03:00
|
|
|
i, j = regs[0]
|
1998-03-26 17:13:24 -04:00
|
|
|
if i == j == lastmatch:
|
|
|
|
# Empty match adjacent to previous match
|
|
|
|
pos = pos + 1
|
1998-07-17 17:18:49 -03:00
|
|
|
append(source[lastmatch:pos])
|
1998-03-26 17:13:24 -04:00
|
|
|
continue
|
|
|
|
if pos < i:
|
1998-07-17 17:18:49 -03:00
|
|
|
append(source[pos:i])
|
|
|
|
if m:
|
|
|
|
m.pos = pos
|
|
|
|
m.regs = regs
|
|
|
|
append(repl(m))
|
|
|
|
else:
|
|
|
|
append(repl)
|
1998-03-26 17:13:24 -04:00
|
|
|
pos = lastmatch = j
|
|
|
|
if i == j:
|
|
|
|
# Last match was empty; don't try here again
|
|
|
|
pos = pos + 1
|
1998-07-17 17:18:49 -03:00
|
|
|
append(source[lastmatch:pos])
|
1998-03-26 17:13:24 -04:00
|
|
|
n = n + 1
|
1998-07-17 17:18:49 -03:00
|
|
|
append(source[pos:])
|
1998-03-26 17:13:24 -04:00
|
|
|
return (string.join(results, ''), n)
|
|
|
|
|
1997-07-17 19:39:13 -03:00
|
|
|
def split(self, source, maxsplit=0):
|
1998-06-29 17:29:08 -03:00
|
|
|
"""Split the source string by the occurrences of the pattern,
|
1998-03-26 17:13:24 -04:00
|
|
|
returning a list containing the resulting substrings."""
|
1997-12-08 13:12:06 -04:00
|
|
|
|
1998-03-26 17:13:24 -04:00
|
|
|
if maxsplit < 0:
|
|
|
|
raise error, "negative split count"
|
|
|
|
if maxsplit == 0:
|
|
|
|
maxsplit = sys.maxint
|
|
|
|
n = 0
|
|
|
|
pos = 0
|
|
|
|
lastmatch = 0
|
|
|
|
results = []
|
|
|
|
end = len(source)
|
1998-07-17 17:18:49 -03:00
|
|
|
match = self.code.match
|
|
|
|
append = results.append
|
1998-03-26 17:13:24 -04:00
|
|
|
while n < maxsplit:
|
1998-07-17 17:18:49 -03:00
|
|
|
regs = match(source, pos, end, 0)
|
|
|
|
if not regs:
|
1998-03-26 17:13:24 -04:00
|
|
|
break
|
1998-07-17 17:18:49 -03:00
|
|
|
i, j = regs[0]
|
1998-03-26 17:13:24 -04:00
|
|
|
if i == j:
|
|
|
|
# Empty match
|
|
|
|
if pos >= end:
|
|
|
|
break
|
|
|
|
pos = pos+1
|
|
|
|
continue
|
1998-07-17 17:18:49 -03:00
|
|
|
append(source[lastmatch:i])
|
|
|
|
rest = regs[1:]
|
|
|
|
if rest:
|
|
|
|
for a, b in rest:
|
|
|
|
if a == -1 or b == -1:
|
|
|
|
group = None
|
|
|
|
else:
|
|
|
|
group = source[a:b]
|
|
|
|
append(group)
|
1998-03-26 17:13:24 -04:00
|
|
|
pos = lastmatch = j
|
|
|
|
n = n + 1
|
1998-07-17 17:18:49 -03:00
|
|
|
append(source[lastmatch:])
|
1998-03-26 17:13:24 -04:00
|
|
|
return results
|
1997-07-17 19:39:13 -03:00
|
|
|
|
1998-07-17 17:18:49 -03:00
|
|
|
def findall(self, source):
|
1998-06-29 17:29:08 -03:00
|
|
|
"""Return a list of all non-overlapping matches in the string.
|
|
|
|
|
|
|
|
If one or more groups are present in the pattern, return a
|
|
|
|
list of groups; this will be a list of tuples if the pattern
|
|
|
|
has more than one group.
|
|
|
|
|
|
|
|
Empty matches are included in the result.
|
|
|
|
|
|
|
|
"""
|
|
|
|
pos = 0
|
1998-07-17 17:18:49 -03:00
|
|
|
end = len(source)
|
|
|
|
results = []
|
|
|
|
match = self.code.match
|
|
|
|
append = results.append
|
|
|
|
while pos <= end:
|
|
|
|
regs = match(source, pos, end, 0)
|
|
|
|
if not regs:
|
1998-06-29 17:29:08 -03:00
|
|
|
break
|
1998-07-17 17:18:49 -03:00
|
|
|
i, j = regs[0]
|
|
|
|
rest = regs[1:]
|
|
|
|
if not rest:
|
|
|
|
gr = source[i:j]
|
|
|
|
elif len(rest) == 1:
|
|
|
|
a, b = rest[0]
|
|
|
|
gr = source[a:b]
|
|
|
|
else:
|
|
|
|
gr = []
|
|
|
|
for (a, b) in rest:
|
|
|
|
gr.append(source[a:b])
|
|
|
|
gr = tuple(gr)
|
|
|
|
append(gr)
|
|
|
|
pos = max(j, pos+1)
|
|
|
|
return results
|
1998-06-29 17:29:08 -03:00
|
|
|
|
1997-12-08 13:12:06 -04:00
|
|
|
# The following 3 functions were contributed by Mike Fletcher, and
|
|
|
|
# allow pickling and unpickling of RegexObject instances.
|
|
|
|
def __getinitargs__(self):
|
|
|
|
return (None,None,None,None) # any 4 elements, to work around
|
|
|
|
# problems with the
|
1998-03-26 17:13:24 -04:00
|
|
|
# pickle/cPickle modules not yet
|
|
|
|
# ignoring the __init__ function
|
1997-12-08 13:12:06 -04:00
|
|
|
def __getstate__(self):
|
|
|
|
return self.pattern, self.flags, self.groupindex
|
|
|
|
def __setstate__(self, statetuple):
|
|
|
|
self.pattern = statetuple[0]
|
|
|
|
self.flags = statetuple[1]
|
|
|
|
self.groupindex = statetuple[2]
|
|
|
|
self.code = apply(pcre_compile, statetuple)
|
|
|
|
|
1998-07-17 17:18:49 -03:00
|
|
|
class _Dummy:
|
|
|
|
# Dummy class used by _subn_string(). Has 'group' to avoid core dump.
|
|
|
|
group = None
|
|
|
|
|
1997-07-10 18:00:31 -03:00
|
|
|
class MatchObject:
|
1998-06-29 17:29:08 -03:00
|
|
|
|
1997-12-08 13:12:06 -04:00
|
|
|
def __init__(self, re, string, pos, endpos, regs):
|
1998-03-26 17:13:24 -04:00
|
|
|
self.re = re
|
|
|
|
self.string = string
|
|
|
|
self.pos = pos
|
|
|
|
self.endpos = endpos
|
|
|
|
self.regs = regs
|
|
|
|
|
1997-12-08 13:12:06 -04:00
|
|
|
def start(self, g = 0):
|
1998-03-26 17:13:24 -04:00
|
|
|
"Return the start of the substring matched by group g"
|
|
|
|
if type(g) == type(''):
|
|
|
|
try:
|
|
|
|
g = self.re.groupindex[g]
|
|
|
|
except (KeyError, TypeError):
|
1998-06-29 17:29:08 -03:00
|
|
|
raise IndexError, 'group %s is undefined' % `g`
|
1998-03-26 17:13:24 -04:00
|
|
|
return self.regs[g][0]
|
1997-07-17 11:52:48 -03:00
|
|
|
|
1997-12-08 13:12:06 -04:00
|
|
|
def end(self, g = 0):
|
1998-03-26 17:13:24 -04:00
|
|
|
"Return the end of the substring matched by group g"
|
|
|
|
if type(g) == type(''):
|
|
|
|
try:
|
|
|
|
g = self.re.groupindex[g]
|
|
|
|
except (KeyError, TypeError):
|
1998-06-29 17:29:08 -03:00
|
|
|
raise IndexError, 'group %s is undefined' % `g`
|
1998-03-26 17:13:24 -04:00
|
|
|
return self.regs[g][1]
|
1997-07-17 11:52:48 -03:00
|
|
|
|
1997-12-08 13:12:06 -04:00
|
|
|
def span(self, g = 0):
|
1998-06-29 17:29:08 -03:00
|
|
|
"Return (start, end) of the substring matched by group g"
|
1998-03-26 17:13:24 -04:00
|
|
|
if type(g) == type(''):
|
|
|
|
try:
|
|
|
|
g = self.re.groupindex[g]
|
|
|
|
except (KeyError, TypeError):
|
1998-06-29 17:29:08 -03:00
|
|
|
raise IndexError, 'group %s is undefined' % `g`
|
1998-03-26 17:13:24 -04:00
|
|
|
return self.regs[g]
|
1997-07-17 11:52:48 -03:00
|
|
|
|
1998-06-29 17:29:08 -03:00
|
|
|
def groups(self, default=None):
|
1998-03-26 17:13:24 -04:00
|
|
|
"Return a tuple containing all subgroups of the match object"
|
|
|
|
result = []
|
|
|
|
for g in range(1, self.re._num_regs):
|
1998-06-29 17:29:08 -03:00
|
|
|
a, b = self.regs[g]
|
|
|
|
if a == -1 or b == -1:
|
|
|
|
result.append(default)
|
1998-03-26 17:13:24 -04:00
|
|
|
else:
|
1998-06-29 17:29:08 -03:00
|
|
|
result.append(self.string[a:b])
|
1998-03-26 17:13:24 -04:00
|
|
|
return tuple(result)
|
1997-12-08 13:12:06 -04:00
|
|
|
|
1997-07-11 17:48:25 -03:00
|
|
|
def group(self, *groups):
|
1998-06-29 17:29:08 -03:00
|
|
|
"Return one or more groups of the match"
|
1998-03-26 17:13:24 -04:00
|
|
|
if len(groups) == 0:
|
|
|
|
groups = (0,)
|
|
|
|
result = []
|
|
|
|
for g in groups:
|
|
|
|
if type(g) == type(''):
|
|
|
|
try:
|
|
|
|
g = self.re.groupindex[g]
|
|
|
|
except (KeyError, TypeError):
|
1998-06-29 17:29:08 -03:00
|
|
|
raise IndexError, 'group %s is undefined' % `g`
|
|
|
|
if g >= len(self.regs):
|
|
|
|
raise IndexError, 'group %s is undefined' % `g`
|
|
|
|
a, b = self.regs[g]
|
|
|
|
if a == -1 or b == -1:
|
1998-03-26 17:13:24 -04:00
|
|
|
result.append(None)
|
|
|
|
else:
|
1998-06-29 17:29:08 -03:00
|
|
|
result.append(self.string[a:b])
|
1998-03-26 17:13:24 -04:00
|
|
|
if len(result) > 1:
|
|
|
|
return tuple(result)
|
|
|
|
elif len(result) == 1:
|
|
|
|
return result[0]
|
|
|
|
else:
|
|
|
|
return ()
|
1998-06-29 17:29:08 -03:00
|
|
|
|
|
|
|
def groupdict(self, default=None):
|
|
|
|
"Return a dictionary containing all named subgroups of the match"
|
|
|
|
dict = {}
|
|
|
|
for name, index in self.re.groupindex.items():
|
|
|
|
a, b = self.regs[index]
|
|
|
|
if a == -1 or b == -1:
|
|
|
|
dict[name] = default
|
|
|
|
else:
|
|
|
|
dict[name] = self.string[a:b]
|
|
|
|
return dict
|