/
rxe.py
260 lines (188 loc) · 5.79 KB
/
rxe.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
import re
class rxe:
def __init__(self):
self.pattern = ''
def raw(self, pattern):
self.pattern += pattern
return self
def any_character(self):
return self.raw('.')
def begin_line(self):
return self.raw('^')
def end_line(self):
return self.raw('$')
def begin_string(self):
return self.raw('\A')
def end_string(self):
return self.raw('\Z')
def digit(self):
return self.raw('\d')
def non_digit(self):
return self.raw('\D')
def alphanumeric(self):
return self.raw('\w')
def non_alphanumeric(self):
return self.raw('\W')
def whitespace(self):
return self.raw('\s')
def non_whitespace(self):
return self.raw('\S')
def word_boundary(self):
return self.raw('\b')
def non_word_boundary(self):
return self.raw('\B')
def literal(self, s):
s = re.escape(s)
self.pattern += s
return self
@staticmethod
def to_regexp_str(s):
if type(s) is str:
return re.escape(s)
elif type(s) is type(rxe()):
return s.pattern
return s
def range(self, fr, to):
self.pattern += fr + '-' + to
return self
def at_least(self, n, s):
s = rxe.to_regexp_str(s)
parens = '(?:' + s + ')'
if n == 0:
self.pattern += parens + '*'
elif n == 1:
self.pattern += parens + '+'
else:
self.pattern += parens + '{%d,}' % n
return self
def exactly(self, n, s):
s = rxe.to_regexp_str(s)
parens = '(?:' + s + ')'
self.pattern += parens + '{%d}' % n
return self
def one(self, s):
return self.exactly(1, s)
def at_least_at_most(self, min, max, s):
s = rxe.to_regexp_str(s)
parens = '(?:' + s + ')'
if min == 0 and max == 1:
self.pattern += parens + '?'
else:
self.pattern += parens + '{%d,%d}' % (min, max)
return self
def zero_or_more(self, s):
return self.at_least(0, s)
def one_or_more(self, s):
return self.at_least(1, s)
def zero_or_one(self, s):
return self.at_least_at_most(0, 1, s)
def non_greedy(self):
return self.raw('?')
def either(self, s1, s2):
s1 = rxe.to_regexp_str(s1)
s2 = rxe.to_regexp_str(s2)
parens1 = '(?:' + s1 + ')'
parens2 = '(?:' + s2 + ')'
self.pattern += parens1 + '|' + parens2
return self
def set(self, li):
s = ''.join([rxe.to_regexp_str(i) for i in li])
self.pattern += '[' + s + ']'
return self
def set_except(self, li):
s = ''.join([rxe.to_regexp_str(i) for i in li])
self.pattern += '[^' + s + ']'
return self
def named(self, name, s):
s = rxe.to_regexp_str(s)
self.pattern += '(?P<%s>%s)' % (name, s)
return self
def assert_lookahead(self, s):
s = rxe.to_regexp_str(s)
self.pattern += '(?=' + s + ')'
return self
def assert_lookahead_not(self, s):
s = rxe.to_regexp_str(s)
self.pattern += '(?!' + s + ')'
return self
def get_pattern(self):
return self.pattern
def fullmatch(self, s):
m = re.compile(self.pattern + '$')
return m.match(s)
def compile(self, flags=0):
return re.compile(self.pattern, flags)
def search(self, string, flags=0):
return re.search(self.pattern, string, flags)
def match(self, string, flags=0):
return re.match(self.pattern, string, flags)
def split(self, string, maxsplit=0, flags=0):
return re.split(self.pattern, string, maxsplit, flags)
def findall(self, string, flags=0):
return re.findall(self.pattern, string, flags)
def finditer(self, string, flags=0):
return re.finditer(self.pattern, string, flags)
def sub(self, repl, string, count=0, flags=0):
return re.sub(self.pattern, repl, string, count=0, flags=0)
def subn(self, repl, string, count=0, flags=0):
return re.subn(self.pattern, repl, string, count=0, flags=0)
def raw(pattern):
return rxe().raw(pattern)
def any_character():
return rxe().any_character()
def begin_line():
return rxe().begin_line()
def end_line():
return rxe().end_line()
def begin_string():
return rxe().begin_string()
def end_string():
return rxe().end_string()
def digit():
return rxe().digit()
def non_digit():
return rxe().non_digit()
def alphanumeric():
return rxe().alphanumeric()
def non_alphanumeric():
return rxe().non_alphanumeric()
def whitespace():
return rxe().whitespace()
def non_whitespace():
return rxe().non_whitespace()
def word_boundary():
return rxe().word_boundary()
def non_word_boundary():
return rxe().non_word_boundary()
def literal(s):
return rxe().literal(s)
def range(fr, to):
return rxe().range(fr, to)
def at_least(n, s):
return rxe().at_least(n, s)
def exactly(n, s):
return rxe().exactly(n, s)
def one(s):
return rxe().one(s)
def at_least_at_most(min, max, s):
return rxe().at_least_at_most(min, max, s)
def zero_or_more(s):
return rxe().zero_or_more(s)
def one_or_more(s):
return rxe().one_or_more(s)
def zero_or_one(s):
return rxe().zero_or_one(s)
def non_greedy():
return rxe().non_greedy()
def either(s1, s2):
return rxe().either(s1, s2)
def set(li):
return rxe().set(li)
def set_except(li):
return rxe().set_except(li)
def named(name, s):
return rxe().named(name, s)
def assert_lookahead(s):
return rxe().assert_lookahead(s)
def assert_lookahead_not(s):
return rxe().assert_lookahead_not(s)