blob: edfefc12b77a6f1f8eb8bd2beaa24f32c4c088ed [file] [log] [blame]
Guido van Rossum7627c0d2000-03-31 14:58:54 +00001#
2# Secret Labs' Regular Expression Engine
Guido van Rossum7627c0d2000-03-31 14:58:54 +00003#
4# re-compatible interface for the sre matching engine
5#
6# Copyright (c) 1998-2000 by Secret Labs AB. All rights reserved.
7#
Fredrik Lundh29c4ba92000-08-01 18:20:07 +00008# 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).
11#
Guido van Rossum7627c0d2000-03-31 14:58:54 +000012# Portions of this engine have been developed in cooperation with
Fredrik Lundh29c4ba92000-08-01 18:20:07 +000013# CNRI. Hewlett-Packard provided funding for 1.6 integration and
Guido van Rossum7627c0d2000-03-31 14:58:54 +000014# other compatibility work.
15#
16
Fredrik Lundh8a3ebf82000-07-23 21:46:17 +000017# FIXME: change all FIXME's to XXX ;-)
18
Guido van Rossum7627c0d2000-03-31 14:58:54 +000019import sre_compile
Fredrik Lundh436c3d52000-06-29 08:58:44 +000020import sre_parse
Guido van Rossum7627c0d2000-03-31 14:58:54 +000021
Fredrik Lundh8a3ebf82000-07-23 21:46:17 +000022import string
23
Jeremy Hyltonb1aa1952000-06-01 17:39:12 +000024# flags
25I = IGNORECASE = sre_compile.SRE_FLAG_IGNORECASE
26L = LOCALE = sre_compile.SRE_FLAG_LOCALE
27M = MULTILINE = sre_compile.SRE_FLAG_MULTILINE
28S = DOTALL = sre_compile.SRE_FLAG_DOTALL
29X = VERBOSE = sre_compile.SRE_FLAG_VERBOSE
30
Fredrik Lundh29c4ba92000-08-01 18:20:07 +000031# sre extensions (may or may not be in 1.6/2.0 final)
Fredrik Lundh436c3d52000-06-29 08:58:44 +000032T = TEMPLATE = sre_compile.SRE_FLAG_TEMPLATE
33U = UNICODE = sre_compile.SRE_FLAG_UNICODE
34
35# sre exception
Fredrik Lundhbe2211e2000-06-29 16:57:40 +000036error = sre_compile.error
Fredrik Lundh436c3d52000-06-29 08:58:44 +000037
Guido van Rossum7627c0d2000-03-31 14:58:54 +000038# --------------------------------------------------------------------
39# public interface
40
Jeremy Hyltonb1aa1952000-06-01 17:39:12 +000041# FIXME: add docstrings
Guido van Rossum7627c0d2000-03-31 14:58:54 +000042
43def match(pattern, string, flags=0):
Jeremy Hyltonb1aa1952000-06-01 17:39:12 +000044 return _compile(pattern, flags).match(string)
Guido van Rossum7627c0d2000-03-31 14:58:54 +000045
46def search(pattern, string, flags=0):
Jeremy Hyltonb1aa1952000-06-01 17:39:12 +000047 return _compile(pattern, flags).search(string)
Guido van Rossum7627c0d2000-03-31 14:58:54 +000048
Jeremy Hyltonb1aa1952000-06-01 17:39:12 +000049def sub(pattern, repl, string, count=0):
50 return _compile(pattern).sub(repl, string, count)
51
52def subn(pattern, repl, string, count=0):
53 return _compile(pattern).subn(repl, string, count)
54
55def split(pattern, string, maxsplit=0):
56 return _compile(pattern).split(string, maxsplit)
57
58def findall(pattern, string, maxsplit=0):
59 return _compile(pattern).findall(string, maxsplit)
60
61def compile(pattern, flags=0):
62 return _compile(pattern, flags)
63
Fredrik Lundh8a3ebf82000-07-23 21:46:17 +000064def purge():
65 _cache.clear()
66
Fredrik Lundh436c3d52000-06-29 08:58:44 +000067def template(pattern, flags=0):
68 return _compile(pattern, flags|T)
69
Jeremy Hyltonb1aa1952000-06-01 17:39:12 +000070def escape(pattern):
71 s = list(pattern)
72 for i in range(len(pattern)):
73 c = pattern[i]
74 if not ("a" <= c <= "z" or "A" <= c <= "Z" or "0" <= c <= "9"):
75 if c == "\000":
76 s[i] = "\\000"
77 else:
78 s[i] = "\\" + c
Fredrik Lundh8a3ebf82000-07-23 21:46:17 +000079 return _join(s, pattern)
Guido van Rossum7627c0d2000-03-31 14:58:54 +000080
81# --------------------------------------------------------------------
Jeremy Hyltonb1aa1952000-06-01 17:39:12 +000082# internals
Guido van Rossum7627c0d2000-03-31 14:58:54 +000083
Jeremy Hyltonb1aa1952000-06-01 17:39:12 +000084_cache = {}
85_MAXCACHE = 100
Guido van Rossum7627c0d2000-03-31 14:58:54 +000086
Fredrik Lundh8a3ebf82000-07-23 21:46:17 +000087def _join(seq, sep):
88 # internal: join into string having the same type as sep
89 return string.join(seq, sep[:0])
90
Jeremy Hyltonb1aa1952000-06-01 17:39:12 +000091def _compile(pattern, flags=0):
92 # internal: compile pattern
93 tp = type(pattern)
Fredrik Lundh8a3ebf82000-07-23 21:46:17 +000094 if tp not in sre_compile.STRING_TYPES:
Jeremy Hyltonb1aa1952000-06-01 17:39:12 +000095 return pattern
96 key = (tp, pattern, flags)
97 try:
98 return _cache[key]
99 except KeyError:
100 pass
Fredrik Lundhe1869832000-08-01 22:47:49 +0000101 try:
102 p = sre_compile.compile(pattern, flags)
103 except error, v:
104 raise error, v # invalid expression
Jeremy Hyltonb1aa1952000-06-01 17:39:12 +0000105 if len(_cache) >= _MAXCACHE:
106 _cache.clear()
107 _cache[key] = p
108 return p
109
110def _sub(pattern, template, string, count=0):
111 # internal: pattern.sub implementation hook
112 return _subn(pattern, template, string, count)[0]
113
Jeremy Hyltonb1aa1952000-06-01 17:39:12 +0000114def _subn(pattern, template, string, count=0):
115 # internal: pattern.subn implementation hook
116 if callable(template):
Andrew M. Kuchlinge8d52af2000-06-18 20:27:10 +0000117 filter = template
Jeremy Hyltonb1aa1952000-06-01 17:39:12 +0000118 else:
Fredrik Lundh90a07912000-06-30 07:50:59 +0000119 template = sre_parse.parse_template(template, pattern)
Jeremy Hyltonb1aa1952000-06-01 17:39:12 +0000120 def filter(match, template=template):
Fredrik Lundh436c3d52000-06-29 08:58:44 +0000121 return sre_parse.expand_template(template, match)
Jeremy Hyltonb1aa1952000-06-01 17:39:12 +0000122 n = i = 0
123 s = []
124 append = s.append
Fredrik Lundhbe2211e2000-06-29 16:57:40 +0000125 c = pattern.scanner(string)
Jeremy Hyltonb1aa1952000-06-01 17:39:12 +0000126 while not count or n < count:
127 m = c.search()
128 if not m:
129 break
Fredrik Lundh90a07912000-06-30 07:50:59 +0000130 b, e = m.span()
Fredrik Lundh01016fe2000-06-30 00:27:46 +0000131 if i < b:
132 append(string[i:b])
Jeremy Hyltonb1aa1952000-06-01 17:39:12 +0000133 append(filter(m))
Fredrik Lundh90a07912000-06-30 07:50:59 +0000134 i = e
Jeremy Hyltonb1aa1952000-06-01 17:39:12 +0000135 n = n + 1
Fredrik Lundh01016fe2000-06-30 00:27:46 +0000136 append(string[i:])
Fredrik Lundh8a3ebf82000-07-23 21:46:17 +0000137 return _join(s, string[:0]), n
Jeremy Hyltonb1aa1952000-06-01 17:39:12 +0000138
139def _split(pattern, string, maxsplit=0):
140 # internal: pattern.split implementation hook
141 n = i = 0
142 s = []
143 append = s.append
Fredrik Lundhbe2211e2000-06-29 16:57:40 +0000144 extend = s.extend
145 c = pattern.scanner(string)
Fredrik Lundh01016fe2000-06-30 00:27:46 +0000146 g = pattern.groups
Jeremy Hyltonb1aa1952000-06-01 17:39:12 +0000147 while not maxsplit or n < maxsplit:
148 m = c.search()
149 if not m:
150 break
Fredrik Lundh90a07912000-06-30 07:50:59 +0000151 b, e = m.span()
152 if b == e:
153 if i >= len(string):
154 break
155 continue
Fredrik Lundhbe2211e2000-06-29 16:57:40 +0000156 append(string[i:b])
Fredrik Lundh90a07912000-06-30 07:50:59 +0000157 if g and b != e:
158 extend(m.groups())
159 i = e
Jeremy Hyltonb1aa1952000-06-01 17:39:12 +0000160 n = n + 1
Fredrik Lundh80946112000-06-29 18:03:25 +0000161 append(string[i:])
Jeremy Hyltonb1aa1952000-06-01 17:39:12 +0000162 return s
Fredrik Lundh0640e112000-06-30 13:55:15 +0000163
164# register myself for pickling
165
166import copy_reg
167
168def _pickle(p):
169 return _compile, (p.pattern, p.flags)
170
171copy_reg.pickle(type(_compile("")), _pickle, _compile)
Fredrik Lundh7cafe4d2000-07-02 17:33:27 +0000172
173# --------------------------------------------------------------------
174# experimental stuff (see python-dev discussions for details)
175
176class Scanner:
177 def __init__(self, lexicon):
Fredrik Lundh29c4ba92000-08-01 18:20:07 +0000178 from sre_constants import BRANCH, SUBPATTERN
Fredrik Lundh7cafe4d2000-07-02 17:33:27 +0000179 self.lexicon = lexicon
Fredrik Lundh8a3ebf82000-07-23 21:46:17 +0000180 # combine phrases into a compound pattern
Fredrik Lundh7cafe4d2000-07-02 17:33:27 +0000181 p = []
Fredrik Lundh8a3ebf82000-07-23 21:46:17 +0000182 s = sre_parse.Pattern()
Fredrik Lundh7cafe4d2000-07-02 17:33:27 +0000183 for phrase, action in lexicon:
Fredrik Lundh8a3ebf82000-07-23 21:46:17 +0000184 p.append(sre_parse.SubPattern(s, [
Fredrik Lundh29c4ba92000-08-01 18:20:07 +0000185 (SUBPATTERN, (len(p), sre_parse.parse(phrase))),
Fredrik Lundh8a3ebf82000-07-23 21:46:17 +0000186 ]))
187 p = sre_parse.SubPattern(s, [(BRANCH, (None, p))])
188 s.groups = len(p)
189 self.scanner = sre_compile.compile(p)
Fredrik Lundh7cafe4d2000-07-02 17:33:27 +0000190 def scan(self, string):
191 result = []
192 append = result.append
193 match = self.scanner.match
194 i = 0
195 while 1:
196 m = match(string, i)
197 if not m:
198 break
199 j = m.end()
200 if i == j:
201 break
Fredrik Lundh019bcb52000-07-02 22:59:57 +0000202 action = self.lexicon[m.lastindex][1]
Fredrik Lundh7cafe4d2000-07-02 17:33:27 +0000203 if callable(action):
204 self.match = match
205 action = action(self, m.group())
206 if action is not None:
207 append(action)
208 i = j
209 return result, string[i:]