blob: e0a51e345bcec2af9ed35ac861d6d79805a3a704 [file] [log] [blame]
Guido van Rossum7627c0d2000-03-31 14:58:54 +00001#
2# Secret Labs' Regular Expression Engine
3# $Id$
4#
5# re-compatible interface for the sre matching engine
6#
7# Copyright (c) 1998-2000 by Secret Labs AB. All rights reserved.
8#
Guido van Rossum7627c0d2000-03-31 14:58:54 +00009# Portions of this engine have been developed in cooperation with
10# CNRI. Hewlett-Packard provided funding for 1.6 integration and
11# other compatibility work.
12#
13
Guido van Rossum7627c0d2000-03-31 14:58:54 +000014import sre_compile
Fredrik Lundh436c3d582000-06-29 08:58:44 +000015import sre_parse
Guido van Rossum7627c0d2000-03-31 14:58:54 +000016
Jeremy Hyltonb1aa1952000-06-01 17:39:12 +000017# flags
18I = IGNORECASE = sre_compile.SRE_FLAG_IGNORECASE
19L = LOCALE = sre_compile.SRE_FLAG_LOCALE
20M = MULTILINE = sre_compile.SRE_FLAG_MULTILINE
21S = DOTALL = sre_compile.SRE_FLAG_DOTALL
22X = VERBOSE = sre_compile.SRE_FLAG_VERBOSE
23
Fredrik Lundh436c3d582000-06-29 08:58:44 +000024# sre extensions (may or may not be in 1.6 final)
25T = TEMPLATE = sre_compile.SRE_FLAG_TEMPLATE
26U = UNICODE = sre_compile.SRE_FLAG_UNICODE
27
28# sre exception
Fredrik Lundhbe2211e2000-06-29 16:57:40 +000029error = sre_compile.error
Fredrik Lundh436c3d582000-06-29 08:58:44 +000030
Guido van Rossum7627c0d2000-03-31 14:58:54 +000031# --------------------------------------------------------------------
32# public interface
33
Jeremy Hyltonb1aa1952000-06-01 17:39:12 +000034# FIXME: add docstrings
Guido van Rossum7627c0d2000-03-31 14:58:54 +000035
36def match(pattern, string, flags=0):
Jeremy Hyltonb1aa1952000-06-01 17:39:12 +000037 return _compile(pattern, flags).match(string)
Guido van Rossum7627c0d2000-03-31 14:58:54 +000038
39def search(pattern, string, flags=0):
Jeremy Hyltonb1aa1952000-06-01 17:39:12 +000040 return _compile(pattern, flags).search(string)
Guido van Rossum7627c0d2000-03-31 14:58:54 +000041
Jeremy Hyltonb1aa1952000-06-01 17:39:12 +000042def sub(pattern, repl, string, count=0):
43 return _compile(pattern).sub(repl, string, count)
44
45def subn(pattern, repl, string, count=0):
46 return _compile(pattern).subn(repl, string, count)
47
48def split(pattern, string, maxsplit=0):
49 return _compile(pattern).split(string, maxsplit)
50
51def findall(pattern, string, maxsplit=0):
52 return _compile(pattern).findall(string, maxsplit)
53
54def compile(pattern, flags=0):
55 return _compile(pattern, flags)
56
Fredrik Lundh436c3d582000-06-29 08:58:44 +000057def template(pattern, flags=0):
58 return _compile(pattern, flags|T)
59
Jeremy Hyltonb1aa1952000-06-01 17:39:12 +000060def escape(pattern):
61 s = list(pattern)
62 for i in range(len(pattern)):
63 c = pattern[i]
64 if not ("a" <= c <= "z" or "A" <= c <= "Z" or "0" <= c <= "9"):
65 if c == "\000":
66 s[i] = "\\000"
67 else:
68 s[i] = "\\" + c
69 return pattern[:0].join(s)
Guido van Rossum7627c0d2000-03-31 14:58:54 +000070
71# --------------------------------------------------------------------
Jeremy Hyltonb1aa1952000-06-01 17:39:12 +000072# internals
Guido van Rossum7627c0d2000-03-31 14:58:54 +000073
Jeremy Hyltonb1aa1952000-06-01 17:39:12 +000074_cache = {}
75_MAXCACHE = 100
Guido van Rossum7627c0d2000-03-31 14:58:54 +000076
Jeremy Hyltonb1aa1952000-06-01 17:39:12 +000077def _compile(pattern, flags=0):
78 # internal: compile pattern
79 tp = type(pattern)
80 if tp not in (type(""), type(u"")):
81 return pattern
82 key = (tp, pattern, flags)
83 try:
84 return _cache[key]
85 except KeyError:
86 pass
87 p = sre_compile.compile(pattern, flags)
88 if len(_cache) >= _MAXCACHE:
89 _cache.clear()
90 _cache[key] = p
91 return p
92
93def _sub(pattern, template, string, count=0):
94 # internal: pattern.sub implementation hook
95 return _subn(pattern, template, string, count)[0]
96
Jeremy Hyltonb1aa1952000-06-01 17:39:12 +000097def _subn(pattern, template, string, count=0):
98 # internal: pattern.subn implementation hook
99 if callable(template):
Andrew M. Kuchlinge8d52af2000-06-18 20:27:10 +0000100 filter = template
Jeremy Hyltonb1aa1952000-06-01 17:39:12 +0000101 else:
Fredrik Lundh436c3d582000-06-29 08:58:44 +0000102 template = sre_parse.parse_template(template, pattern)
Jeremy Hyltonb1aa1952000-06-01 17:39:12 +0000103 def filter(match, template=template):
Fredrik Lundh436c3d582000-06-29 08:58:44 +0000104 return sre_parse.expand_template(template, match)
Jeremy Hyltonb1aa1952000-06-01 17:39:12 +0000105 n = i = 0
106 s = []
107 append = s.append
Fredrik Lundhbe2211e2000-06-29 16:57:40 +0000108 c = pattern.scanner(string)
Jeremy Hyltonb1aa1952000-06-01 17:39:12 +0000109 while not count or n < count:
110 m = c.search()
111 if not m:
112 break
113 j = m.start()
114 if j > i:
115 append(string[i:j])
116 append(filter(m))
117 i = m.end()
Fredrik Lundh436c3d582000-06-29 08:58:44 +0000118 if i <= j:
119 break
Jeremy Hyltonb1aa1952000-06-01 17:39:12 +0000120 n = n + 1
121 if i < len(string):
122 append(string[i:])
123 return string[:0].join(s), n
124
125def _split(pattern, string, maxsplit=0):
126 # internal: pattern.split implementation hook
127 n = i = 0
128 s = []
129 append = s.append
Fredrik Lundhbe2211e2000-06-29 16:57:40 +0000130 extend = s.extend
131 c = pattern.scanner(string)
132 g = c.groups
Jeremy Hyltonb1aa1952000-06-01 17:39:12 +0000133 while not maxsplit or n < maxsplit:
134 m = c.search()
135 if not m:
136 break
Fredrik Lundhbe2211e2000-06-29 16:57:40 +0000137 b, e = m.span()
138 if e == i:
139 continue
140 append(string[i:b])
141 if g and b != e:
142 extend(m.groups())
143 i = e
Jeremy Hyltonb1aa1952000-06-01 17:39:12 +0000144 n = n + 1
145 if i < len(string):
146 append(string[i:])
147 return s