1997-07-10 21:00:31 +00:00
|
|
|
#!/usr/bin/env python
|
|
|
|
# -*- mode: python -*-
|
1997-08-13 03:25:34 +00:00
|
|
|
|
1997-10-06 14:45:17 +00:00
|
|
|
import sys
|
|
|
|
import string
|
|
|
|
from pcre import *
|
1997-07-10 21:00:31 +00:00
|
|
|
|
|
|
|
#
|
1997-10-06 14:45:17 +00:00
|
|
|
# First, the public part of the interface:
|
1997-07-10 21:00:31 +00:00
|
|
|
#
|
|
|
|
|
1997-10-06 14:45:17 +00:00
|
|
|
# pcre.error and re.error should be the same, since exceptions can be
|
1997-12-08 17:12:06 +00:00
|
|
|
# raised from either module.
|
1997-10-06 14:45:17 +00:00
|
|
|
|
|
|
|
# compilation flags
|
|
|
|
|
|
|
|
I = IGNORECASE
|
1997-12-08 17:12:06 +00:00
|
|
|
L = LOCALE
|
1997-10-06 14:45:17 +00:00
|
|
|
M = MULTILINE
|
|
|
|
S = DOTALL
|
|
|
|
X = VERBOSE
|
1997-07-15 15:38:20 +00:00
|
|
|
|
|
|
|
#
|
|
|
|
#
|
|
|
|
#
|
|
|
|
|
1997-07-15 18:59:04 +00:00
|
|
|
_cache = {}
|
|
|
|
_MAXCACHE = 20
|
1997-07-17 14:52:48 +00:00
|
|
|
|
1997-07-17 22:39:13 +00:00
|
|
|
def _cachecompile(pattern, flags=0):
|
1997-07-15 18:59:04 +00: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 21:00:31 +00:00
|
|
|
def match(pattern, string, flags=0):
|
1997-07-15 18:59:04 +00:00
|
|
|
return _cachecompile(pattern, flags).match(string)
|
1997-07-17 14:52:48 +00:00
|
|
|
|
1997-07-10 21:00:31 +00:00
|
|
|
def search(pattern, string, flags=0):
|
1997-07-15 18:59:04 +00:00
|
|
|
return _cachecompile(pattern, flags).search(string)
|
1997-07-17 14:52:48 +00:00
|
|
|
|
1997-07-10 21:00:31 +00:00
|
|
|
def sub(pattern, repl, string, count=0):
|
1997-07-17 22:39:13 +00:00
|
|
|
if type(pattern) == type(''):
|
|
|
|
pattern = _cachecompile(pattern)
|
|
|
|
return pattern.sub(repl, string, count)
|
1997-07-10 21:00:31 +00:00
|
|
|
|
|
|
|
def subn(pattern, repl, string, count=0):
|
1997-07-17 22:39:13 +00:00
|
|
|
if type(pattern) == type(''):
|
|
|
|
pattern = _cachecompile(pattern)
|
|
|
|
return pattern.subn(repl, string, count)
|
1997-07-17 14:52:48 +00:00
|
|
|
|
1997-07-11 20:48:25 +00:00
|
|
|
def split(pattern, string, maxsplit=0):
|
1997-07-17 22:39:13 +00:00
|
|
|
if type(pattern) == type(''):
|
|
|
|
pattern = _cachecompile(pattern)
|
|
|
|
return pattern.split(string, maxsplit)
|
1997-07-10 21:00:31 +00:00
|
|
|
|
1997-12-08 17:12:06 +00:00
|
|
|
def escape(pattern):
|
|
|
|
"Escape all non-alphanumeric characters in pattern."
|
|
|
|
result = []
|
|
|
|
alphanum=string.letters+'_'+string.digits
|
|
|
|
for char in pattern:
|
|
|
|
if char not in alphanum:
|
|
|
|
result.append('\\')
|
|
|
|
result.append(char)
|
|
|
|
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 21:00:31 +00:00
|
|
|
#
|
1997-12-08 17:12:06 +00:00
|
|
|
# Class definitions
|
1997-07-10 21:00:31 +00:00
|
|
|
#
|
|
|
|
|
|
|
|
class RegexObject:
|
1997-10-06 14:45:17 +00:00
|
|
|
def __init__(self, pattern, flags, code, groupindex):
|
|
|
|
self.code = code
|
1997-07-10 21:00:31 +00:00
|
|
|
self.flags = flags
|
|
|
|
self.pattern = pattern
|
|
|
|
self.groupindex = groupindex
|
1997-12-08 17:12:06 +00:00
|
|
|
|
|
|
|
def search(self, string, pos=0, endpos=None):
|
|
|
|
"""Scan through string looking for a match to the pattern, returning
|
|
|
|
a MatchObject instance, or None if no match was found."""
|
|
|
|
|
|
|
|
if endpos is None or endpos>len(string):
|
|
|
|
endpos=len(string)
|
|
|
|
if endpos<pos: endpos=pos
|
|
|
|
regs = self.code.match(string, pos, endpos, 0)
|
1997-07-10 21:00:31 +00:00
|
|
|
if regs is None:
|
|
|
|
return None
|
1997-12-08 17:12:06 +00:00
|
|
|
self._num_regs=len(regs)
|
1997-07-17 14:52:48 +00:00
|
|
|
|
1997-07-10 21:00:31 +00:00
|
|
|
return MatchObject(self,
|
|
|
|
string,
|
1997-12-08 17:12:06 +00:00
|
|
|
pos, endpos,
|
1997-07-10 21:00:31 +00:00
|
|
|
regs)
|
1997-07-17 14:52:48 +00:00
|
|
|
|
1997-12-08 17:12:06 +00:00
|
|
|
def match(self, string, pos=0, endpos=None):
|
|
|
|
"""Try to apply the pattern at the start of the string, returning
|
|
|
|
a MatchObject instance, or None if no match was found."""
|
|
|
|
|
|
|
|
if endpos is None or endpos>len(string):
|
|
|
|
endpos=len(string)
|
|
|
|
if endpos<pos: endpos=pos
|
|
|
|
regs = self.code.match(string, pos, endpos, ANCHORED)
|
1997-07-11 20:48:25 +00:00
|
|
|
if regs is None:
|
|
|
|
return None
|
1997-12-08 17:12:06 +00:00
|
|
|
self._num_regs=len(regs)
|
1997-07-11 20:48:25 +00:00
|
|
|
return MatchObject(self,
|
|
|
|
string,
|
1997-12-08 17:12:06 +00:00
|
|
|
pos, endpos,
|
1997-07-11 20:48:25 +00:00
|
|
|
regs)
|
1997-07-17 14:52:48 +00:00
|
|
|
|
1997-07-11 20:48:25 +00:00
|
|
|
def sub(self, repl, string, count=0):
|
1997-12-08 17:12:06 +00:00
|
|
|
"""Return the string obtained by replacing the leftmost
|
|
|
|
non-overlapping occurrences of the pattern in string by the
|
|
|
|
replacement repl"""
|
|
|
|
|
1997-07-18 04:26:03 +00:00
|
|
|
return self.subn(repl, string, count)[0]
|
1997-07-17 14:52:48 +00:00
|
|
|
|
1997-12-08 17:12:06 +00:00
|
|
|
def subn(self, repl, source, count=0):
|
|
|
|
"""Return a 2-tuple containing (new_string, number).
|
|
|
|
new_string is the string obtained by replacing the leftmost
|
|
|
|
non-overlapping occurrences of the pattern in string by the
|
|
|
|
replacement repl. number is the number of substitutions that
|
|
|
|
were made."""
|
|
|
|
|
1997-07-18 04:26:03 +00:00
|
|
|
if count < 0:
|
1997-10-06 14:45:17 +00:00
|
|
|
raise error, "negative substitution count"
|
1997-07-18 04:26:03 +00:00
|
|
|
if count == 0:
|
|
|
|
import sys
|
|
|
|
count = sys.maxint
|
1997-07-17 22:39:13 +00:00
|
|
|
if type(repl) == type(''):
|
|
|
|
if '\\' in repl:
|
1997-10-06 14:45:17 +00:00
|
|
|
repl = lambda m, r=repl: pcre_expand(m, r)
|
1997-07-17 22:39:13 +00:00
|
|
|
else:
|
|
|
|
repl = lambda m, r=repl: r
|
1997-07-18 04:26:03 +00: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 22:39:13 +00:00
|
|
|
end = len(source)
|
|
|
|
while n < count and pos <= end:
|
|
|
|
m = self.search(source, pos)
|
1997-07-18 04:26:03 +00:00
|
|
|
if not m:
|
|
|
|
break
|
1997-07-17 22:39:13 +00:00
|
|
|
i, j = m.span(0)
|
|
|
|
if i == j == lastmatch:
|
|
|
|
# Empty match adjacent to previous match
|
1997-07-18 04:26:03 +00:00
|
|
|
pos = pos + 1
|
1997-07-17 22:39:13 +00:00
|
|
|
results.append(source[lastmatch:pos])
|
|
|
|
continue
|
1997-07-18 04:26:03 +00:00
|
|
|
if pos < i:
|
|
|
|
results.append(source[pos:i])
|
1997-07-17 22:39:13 +00:00
|
|
|
results.append(repl(m))
|
|
|
|
pos = lastmatch = j
|
|
|
|
if i == j:
|
|
|
|
# Last match was empty; don't try here again
|
1997-07-18 04:26:03 +00:00
|
|
|
pos = pos + 1
|
1997-07-17 22:39:13 +00:00
|
|
|
results.append(source[lastmatch:pos])
|
1997-07-18 04:26:03 +00:00
|
|
|
n = n + 1
|
1997-07-17 22:39:13 +00:00
|
|
|
results.append(source[pos:])
|
|
|
|
return (string.join(results, ''), n)
|
1997-07-18 04:26:03 +00:00
|
|
|
|
1997-07-17 22:39:13 +00:00
|
|
|
def split(self, source, maxsplit=0):
|
1997-12-08 17:12:06 +00:00
|
|
|
"""Split \var{string} by the occurrences of the pattern,
|
|
|
|
returning a list containing the resulting substrings."""
|
|
|
|
|
1997-07-18 04:26:03 +00:00
|
|
|
if maxsplit < 0:
|
|
|
|
raise error, "negative split count"
|
|
|
|
if maxsplit == 0:
|
|
|
|
import sys
|
|
|
|
maxsplit = sys.maxint
|
1997-07-17 22:39:13 +00:00
|
|
|
n = 0
|
|
|
|
pos = 0
|
|
|
|
lastmatch = 0
|
|
|
|
results = []
|
|
|
|
end = len(source)
|
|
|
|
while n < maxsplit:
|
|
|
|
m = self.search(source, pos)
|
1997-07-18 04:26:03 +00:00
|
|
|
if not m:
|
|
|
|
break
|
1997-07-17 22:39:13 +00:00
|
|
|
i, j = m.span(0)
|
|
|
|
if i == j:
|
|
|
|
# Empty match
|
1997-07-18 04:26:03 +00:00
|
|
|
if pos >= end:
|
|
|
|
break
|
1997-07-17 22:39:13 +00:00
|
|
|
pos = pos+1
|
|
|
|
continue
|
|
|
|
results.append(source[lastmatch:i])
|
1997-12-08 17:12:06 +00:00
|
|
|
g = m.groups()
|
1997-07-17 22:39:13 +00:00
|
|
|
if g:
|
1997-12-08 17:12:06 +00:00
|
|
|
if type(g)==type( "" ): g = [g]
|
1997-07-17 22:39:13 +00:00
|
|
|
results[len(results):] = list(g)
|
|
|
|
pos = lastmatch = j
|
|
|
|
results.append(source[lastmatch:])
|
|
|
|
return results
|
|
|
|
|
1997-12-08 17:12:06 +00: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
|
|
|
|
# pickle/cPickle modules not yet
|
|
|
|
# ignoring the __init__ function
|
|
|
|
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)
|
|
|
|
|
1997-07-10 21:00:31 +00:00
|
|
|
class MatchObject:
|
1997-12-08 17:12:06 +00:00
|
|
|
def __init__(self, re, string, pos, endpos, regs):
|
1997-07-10 21:00:31 +00:00
|
|
|
self.re = re
|
|
|
|
self.string = string
|
1997-12-08 17:12:06 +00:00
|
|
|
self.pos = pos
|
|
|
|
self.endpos = endpos
|
1997-07-10 21:00:31 +00:00
|
|
|
self.regs = regs
|
1997-07-17 14:52:48 +00:00
|
|
|
|
1997-12-08 17:12:06 +00:00
|
|
|
def start(self, g = 0):
|
|
|
|
"Return the start of the substring matched by group g"
|
1997-07-11 20:48:25 +00:00
|
|
|
if type(g) == type(''):
|
1997-07-10 21:00:31 +00:00
|
|
|
try:
|
1997-07-11 20:48:25 +00:00
|
|
|
g = self.re.groupindex[g]
|
1997-07-10 21:00:31 +00:00
|
|
|
except (KeyError, TypeError):
|
1997-07-11 20:48:25 +00:00
|
|
|
raise IndexError, ('group "' + g + '" is undefined')
|
|
|
|
return self.regs[g][0]
|
1997-07-17 14:52:48 +00:00
|
|
|
|
1997-12-08 17:12:06 +00:00
|
|
|
def end(self, g = 0):
|
|
|
|
"Return the end of the substring matched by group g"
|
1997-07-11 20:48:25 +00:00
|
|
|
if type(g) == type(''):
|
1997-07-10 21:00:31 +00:00
|
|
|
try:
|
1997-07-11 20:48:25 +00:00
|
|
|
g = self.re.groupindex[g]
|
1997-07-10 21:00:31 +00:00
|
|
|
except (KeyError, TypeError):
|
1997-07-11 20:48:25 +00:00
|
|
|
raise IndexError, ('group "' + g + '" is undefined')
|
|
|
|
return self.regs[g][1]
|
1997-07-17 14:52:48 +00:00
|
|
|
|
1997-12-08 17:12:06 +00:00
|
|
|
def span(self, g = 0):
|
|
|
|
"""Return a tuple containing the start,end of the substring
|
|
|
|
matched by group g"""
|
1997-07-11 20:48:25 +00:00
|
|
|
if type(g) == type(''):
|
1997-07-10 21:00:31 +00:00
|
|
|
try:
|
1997-07-11 20:48:25 +00:00
|
|
|
g = self.re.groupindex[g]
|
1997-07-10 21:00:31 +00:00
|
|
|
except (KeyError, TypeError):
|
1997-07-11 20:48:25 +00:00
|
|
|
raise IndexError, ('group "' + g + '" is undefined')
|
|
|
|
return self.regs[g]
|
1997-07-17 14:52:48 +00:00
|
|
|
|
1997-12-08 17:12:06 +00:00
|
|
|
def groups(self):
|
|
|
|
"Return a tuple containing all subgroups of the match object"
|
|
|
|
|
|
|
|
# If _num_regs==1, we don't want to call self.group with an
|
|
|
|
# empty tuple.
|
|
|
|
if self.re._num_regs == 1: return ()
|
|
|
|
return apply(self.group, tuple(range(1, self.re._num_regs) ) )
|
|
|
|
|
1997-07-11 20:48:25 +00:00
|
|
|
def group(self, *groups):
|
1997-12-08 17:12:06 +00:00
|
|
|
"Return one or more groups of the match."
|
1997-07-11 20:48:25 +00:00
|
|
|
if len(groups) == 0:
|
1997-12-08 17:12:06 +00:00
|
|
|
groups = (0,)
|
1997-07-11 20:48:25 +00: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 14:45:17 +00: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 20:48:25 +00:00
|
|
|
result.append(None)
|
|
|
|
else:
|
|
|
|
result.append(self.string[self.regs[g][0]:self.regs[g][1]])
|
1997-12-08 17:12:06 +00:00
|
|
|
if len(result) > 1:
|
1997-07-11 20:48:25 +00:00
|
|
|
return tuple(result)
|
|
|
|
elif len(result) == 1:
|
|
|
|
return result[0]
|
|
|
|
else:
|
|
|
|
return ()
|
1997-07-10 21:00:31 +00:00
|
|
|
|