-
Notifications
You must be signed in to change notification settings - Fork 46
/
RegExpBuilder.py
194 lines (148 loc) · 5.31 KB
/
RegExpBuilder.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
import re
class RegExpBuilder:
def __init__(self):
self._literal = ''
self._ignoreCase = False
self._specialCharactersInsideCharacterClass = set(['^', '-', ']'])
self._specialCharactersOutsideCharacterClass = set(['.', '^', '$', '*', '+', '?', '(', ')', '[', '{'])
self._min = -1
self._max = -1
self._of = ''
self._ofAny = False
self._from = ''
self._notFrom = ''
self._like = ''
self._behind = ''
self._notBehind = ''
self._either = ''
self._reluctant = False
self._capture = False
def _clear(self):
self._ignoreCase = False
self._multiLine = False
self._min = -1
self._max = -1
self._of = ''
self._ofAny = False
self._from = ''
self._notFrom = ''
self._like = ''
self._behind = ''
self._notBehind = ''
self._either = ''
self._reluctant = False
self._capture = False
def _flushState(self):
if self._of != '' or self._ofAny or self._from != '' or self._notFrom != '' or self._like != '':
captureLiteral = '' if self._capture else '?:'
quantityLiteral = self._getQuantityLiteral()
characterLiteral = self._getCharacterLiteral()
reluctantLiteral = '?' if self._reluctant else ''
behindLiteral = '(?=' + self._behind + ')' if self._behind != '' else ''
notBehindLiteral = '(?!' + self._notBehind + ')' if self._notBehind != '' else ''
self._literal += '(' + captureLiteral + '(?:' + characterLiteral + ')' + quantityLiteral + reluctantLiteral + ')' + behindLiteral + notBehindLiteral
self._clear()
def _getQuantityLiteral(self):
if self._min != -1:
if self._max != -1:
return '{' + str(self._min) + ',' + str(self._max) + '}'
return '{' + str(self._min) + ',}'
return '{0,' + str(self._max) + '}'
def _getCharacterLiteral(self):
if self._of != '':
return self._of
if self._ofAny:
return '.'
if self._from != '':
return '[' + self._from + ']'
if self._notFrom != '':
return '[^' + self._notFrom + ']'
if self._like != '':
return self._like
def getLiteral(self):
self._flushState()
return self._literal
def getRegExp(self):
self._flushState()
flags = 0
if self._ignoreCase:
flags = flags | re.IGNORECASE
if self._multiLine:
flags = flags | re.MULTILINE
return re.compile(self._literal, flags)
def ignoreCase(self):
self._ignoreCase = True
return self
def multiLine(self):
self._multiLine = True
return self
def start(self):
self._literal += '(?:^)'
return self
def end(self):
self._flushState()
self._literal += '(?:$)'
return self
def eitherLike(self, r):
self._flushState()
self._either = r(RegExpBuilder()).getLiteral()
return self
def orLike(self, r):
eitherLike = self._either
orLike = r(RegExpBuilder()).getLiteral()
self._literal += '(?:(?:' + eitherLike + ')|(?:' + orLike + '))'
self._clear()
return self
def exactly(self, n):
self._flushState()
self._min = n
self._max = n
return self
def min(self, n):
self._flushState()
self._min = n
return self
def max(self, n):
self._flushState()
self._max = n
return self
def of(self, s):
self._of = self._escapeOutsideCharacterClass(s)
return self
def ofAny(self):
self._ofAny = True
return self
def fromClass(self, s):
self._from = self._escapeInsideCharacterClass(''.join(s))
return self
def notFromClass(self, s):
self._notFrom = self._escapeInsideCharacterClass(''.join(s))
return self
def like(self, r):
self._like = r(RegExpBuilder()).getLiteral()
return self
def reluctantly(self):
self._reluctant = True
return self
def behind(self, r):
self._behind = r(RegExpBuilder()).getLiteral()
return self
def notBehind(self, r):
self._notBehind = r(RegExpBuilder()).getLiteral()
return self
def asCapturingGroup(self):
self._capture = True
return self
def _escapeInsideCharacterClass(self, s):
return self._escapeSpecialCharacters(s, self._specialCharactersInsideCharacterClass)
def _escapeOutsideCharacterClass(self, s):
return self._escapeSpecialCharacters(s, self._specialCharactersOutsideCharacterClass)
def _escapeSpecialCharacters(self, s, specialCharacters):
escapedString = ''
for i in range(len(s)):
character = s[i]
if character in specialCharacters:
escapedString += '\\' + character
else:
escapedString += character
return escapedString