2 # Secret Labs' Regular Expression Engine
4 # re-compatible interface for the sre matching engine
6 # Copyright (c) 1998-2000 by Secret Labs AB. All rights reserved.
8 # This version of the SRE library can be redistributed under CNRI's
9 # Python 1.6 license. For any other use, please contact Secret Labs
10 # AB (info@pythonware.com).
12 # Portions of this engine have been developed in cooperation with
13 # CNRI. Hewlett-Packard provided funding for 1.6 integration and
14 # other compatibility work.
17 # FIXME: change all FIXME's to XXX ;-)
25 I
= IGNORECASE
= sre_compile
.SRE_FLAG_IGNORECASE
26 L
= LOCALE
= sre_compile
.SRE_FLAG_LOCALE
27 M
= MULTILINE
= sre_compile
.SRE_FLAG_MULTILINE
28 S
= DOTALL
= sre_compile
.SRE_FLAG_DOTALL
29 X
= VERBOSE
= sre_compile
.SRE_FLAG_VERBOSE
31 # sre extensions (may or may not be in 1.6/2.0 final)
32 T
= TEMPLATE
= sre_compile
.SRE_FLAG_TEMPLATE
33 U
= UNICODE
= sre_compile
.SRE_FLAG_UNICODE
36 error
= sre_compile
.error
38 # --------------------------------------------------------------------
41 # FIXME: add docstrings
43 def match(pattern
, string
, flags
=0):
44 return _compile(pattern
, flags
).match(string
)
46 def search(pattern
, string
, flags
=0):
47 return _compile(pattern
, flags
).search(string
)
49 def sub(pattern
, repl
, string
, count
=0):
50 return _compile(pattern
, 0).sub(repl
, string
, count
)
52 def subn(pattern
, repl
, string
, count
=0):
53 return _compile(pattern
, 0).subn(repl
, string
, count
)
55 def split(pattern
, string
, maxsplit
=0):
56 return _compile(pattern
, 0).split(string
, maxsplit
)
58 def findall(pattern
, string
, maxsplit
=0):
59 return _compile(pattern
, 0).findall(string
, maxsplit
)
61 def compile(pattern
, flags
=0):
62 return _compile(pattern
, flags
)
67 def template(pattern
, flags
=0):
68 return _compile(pattern
, flags|T
)
72 for i
in range(len(pattern
)):
74 if not ("a" <= c
<= "z" or "A" <= c
<= "Z" or "0" <= c
<= "9"):
79 return _join(s
, pattern
)
81 # --------------------------------------------------------------------
88 # internal: join into string having the same type as sep
89 return string
.join(seq
, sep
[:0])
92 # internal: compile pattern
97 if type(pattern
) not in sre_compile
.STRING_TYPES
:
100 p
= sre_compile
.compile(pattern
, flags
)
102 raise error
, v
# invalid expression
103 if len(_cache
) >= _MAXCACHE
:
108 def _expand(pattern
, match
, template
):
109 # internal: match.expand implementation hook
110 template
= sre_parse
.parse_template(template
, pattern
)
111 return sre_parse
.expand_template(template
, match
)
113 def _sub(pattern
, template
, string
, count
=0):
114 # internal: pattern.sub implementation hook
115 return _subn(pattern
, template
, string
, count
)[0]
117 def _subn(pattern
, template
, string
, count
=0):
118 # internal: pattern.subn implementation hook
119 if callable(template
):
122 template
= sre_parse
.parse_template(template
, pattern
)
123 def filter(match
, template
=template
):
124 return sre_parse
.expand_template(template
, match
)
128 c
= pattern
.scanner(string
)
129 while not count
or n
< count
:
140 return _join(s
, string
[:0]), n
142 def _split(pattern
, string
, maxsplit
=0):
143 # internal: pattern.split implementation hook
148 c
= pattern
.scanner(string
)
150 while not maxsplit
or n
< maxsplit
:
167 # register myself for pickling
172 return _compile
, (p
.pattern
, p
.flags
)
174 copy_reg
.pickle(type(_compile("", 0)), _pickle
, _compile
)
176 # --------------------------------------------------------------------
177 # experimental stuff (see python-dev discussions for details)
180 def __init__(self
, lexicon
):
181 from sre_constants
import BRANCH
, SUBPATTERN
182 self
.lexicon
= lexicon
183 # combine phrases into a compound pattern
185 s
= sre_parse
.Pattern()
186 for phrase
, action
in lexicon
:
187 p
.append(sre_parse
.SubPattern(s
, [
188 (SUBPATTERN
, (len(p
), sre_parse
.parse(phrase
))),
190 p
= sre_parse
.SubPattern(s
, [(BRANCH
, (None, p
))])
192 self
.scanner
= sre_compile
.compile(p
)
193 def scan(self
, string
):
195 append
= result
.append
196 match
= self
.scanner
.match
205 action
= self
.lexicon
[m
.lastindex
][1]
208 action
= action(self
, m
.group())
209 if action
is not None:
212 return result
, string
[i
:]