blob: 455cd2785e8fc894555d22d84babeadf380cf809 [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
15
Jeremy Hyltonb1aa1952000-06-01 17:39:12 +000016# flags
17I = IGNORECASE = sre_compile.SRE_FLAG_IGNORECASE
18L = LOCALE = sre_compile.SRE_FLAG_LOCALE
19M = MULTILINE = sre_compile.SRE_FLAG_MULTILINE
20S = DOTALL = sre_compile.SRE_FLAG_DOTALL
21X = VERBOSE = sre_compile.SRE_FLAG_VERBOSE
22
Guido van Rossum7627c0d2000-03-31 14:58:54 +000023# --------------------------------------------------------------------
24# public interface
25
Jeremy Hyltonb1aa1952000-06-01 17:39:12 +000026# FIXME: add docstrings
Guido van Rossum7627c0d2000-03-31 14:58:54 +000027
28def match(pattern, string, flags=0):
Jeremy Hyltonb1aa1952000-06-01 17:39:12 +000029 return _compile(pattern, flags).match(string)
Guido van Rossum7627c0d2000-03-31 14:58:54 +000030
31def search(pattern, string, flags=0):
Jeremy Hyltonb1aa1952000-06-01 17:39:12 +000032 return _compile(pattern, flags).search(string)
Guido van Rossum7627c0d2000-03-31 14:58:54 +000033
Jeremy Hyltonb1aa1952000-06-01 17:39:12 +000034def sub(pattern, repl, string, count=0):
35 return _compile(pattern).sub(repl, string, count)
36
37def subn(pattern, repl, string, count=0):
38 return _compile(pattern).subn(repl, string, count)
39
40def split(pattern, string, maxsplit=0):
41 return _compile(pattern).split(string, maxsplit)
42
43def findall(pattern, string, maxsplit=0):
44 return _compile(pattern).findall(string, maxsplit)
45
46def compile(pattern, flags=0):
47 return _compile(pattern, flags)
48
49def escape(pattern):
50 s = list(pattern)
51 for i in range(len(pattern)):
52 c = pattern[i]
53 if not ("a" <= c <= "z" or "A" <= c <= "Z" or "0" <= c <= "9"):
54 if c == "\000":
55 s[i] = "\\000"
56 else:
57 s[i] = "\\" + c
58 return pattern[:0].join(s)
Guido van Rossum7627c0d2000-03-31 14:58:54 +000059
60# --------------------------------------------------------------------
Jeremy Hyltonb1aa1952000-06-01 17:39:12 +000061# internals
Guido van Rossum7627c0d2000-03-31 14:58:54 +000062
Jeremy Hyltonb1aa1952000-06-01 17:39:12 +000063_cache = {}
64_MAXCACHE = 100
Guido van Rossum7627c0d2000-03-31 14:58:54 +000065
Jeremy Hyltonb1aa1952000-06-01 17:39:12 +000066def _compile(pattern, flags=0):
67 # internal: compile pattern
68 tp = type(pattern)
69 if tp not in (type(""), type(u"")):
70 return pattern
71 key = (tp, pattern, flags)
72 try:
73 return _cache[key]
74 except KeyError:
75 pass
76 p = sre_compile.compile(pattern, flags)
77 if len(_cache) >= _MAXCACHE:
78 _cache.clear()
79 _cache[key] = p
80 return p
81
82def _sub(pattern, template, string, count=0):
83 # internal: pattern.sub implementation hook
84 return _subn(pattern, template, string, count)[0]
85
86def _expand(match, template):
87 # internal: expand template
88 return template # FIXME
89
90def _subn(pattern, template, string, count=0):
91 # internal: pattern.subn implementation hook
92 if callable(template):
93 filter = callable
94 else:
95 # FIXME: prepare template
96 def filter(match, template=template):
97 return _expand(match, template)
98 n = i = 0
99 s = []
100 append = s.append
101 c = pattern.cursor(string)
102 while not count or n < count:
103 m = c.search()
104 if not m:
105 break
106 j = m.start()
107 if j > i:
108 append(string[i:j])
109 append(filter(m))
110 i = m.end()
111 n = n + 1
112 if i < len(string):
113 append(string[i:])
114 return string[:0].join(s), n
115
116def _split(pattern, string, maxsplit=0):
117 # internal: pattern.split implementation hook
118 n = i = 0
119 s = []
120 append = s.append
121 c = pattern.cursor(string)
122 while not maxsplit or n < maxsplit:
123 m = c.search()
124 if not m:
125 break
126 j = m.start()
127 append(string[i:j])
128 i = m.end()
129 n = n + 1
130 if i < len(string):
131 append(string[i:])
132 return s