-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathoore.py
192 lines (166 loc) · 6.43 KB
/
oore.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
# encoding: utf-8
"""
oore
~~~~
:copyright: 2014 by Daniel Neuhäuser
:license: BSD, see LICENSE.rst for details
"""
import re
class r(object):
"""
A wrapper for :class:`re.RegexObject`, that provides several useful ways to
create new regular expressions.
Concatenation::
r('foo') + r('bar') == r('foobar')
Or::
r('foo') + r('bar') == r('foo|bar')
Repetition::
r('foo')[n] == r('foo{n}')
Zero/One/N or more::
r('foo')[0, ...] == r('(foo)*')
r('foo')[1, ...] == r('(foo)+')
r('foo')[n, ...] == r('(foo){n}foo*')
In addition to that all attributes/methods provided by
:class:`re.RegexObject` instances are supported as well.
Combining patterns using different string types e.g.
``r(u'foo') + r(b'bar')`` will raise a :exc:`TypeError`.
"""
def __init__(self, pattern):
self.pattern = pattern
self._compiled = re.compile(self.pattern)
@property
def flags(self):
return self._compiled.flags
@property
def groups(self):
return self._compiled.groups
@property
def groupindex(self):
return self._compiled.groupindex
def __add__(self, other):
if isinstance(other, r):
if self.pattern.__class__ != other.pattern.__class__:
raise TypeError(
'incompatible pattern types: {!r} and {!r}'.format(
self.pattern, other.pattern
)
)
elif isinstance(self.pattern, bytes):
return r(
'(?:{})(?:{})'.format(
self.pattern.decode('latin1'),
other.pattern.decode('latin1')
).encode('latin1')
)
else:
return r(u'(?:{})(?:{})'.format(self.pattern, other.pattern))
return NotImplemented
def __or__(self, other):
if isinstance(other, r):
if self.pattern.__class__ != other.pattern.__class__:
raise TypeError(
'incompatible pattern types: {!r} and {!r}'.format(
self.pattern,
other.pattern
)
)
elif isinstance(self.pattern, bytes):
return r(
'(?:{})|(?:{})'.format(
self.pattern.decode('latin1'),
other.pattern.decode('latin1')
).encode('latin1')
)
else:
return r(u'(?:{})|(?:{})'.format(self.pattern, other.pattern))
return NotImplemented
def __getitem__(self, index):
if isinstance(index, int) and index > 0:
if isinstance(self.pattern, bytes):
return r(
'(?:{}){{{}}}'.format(
self.pattern.decode('latin1'),
index
).encode('latin1')
)
else:
return r(u'(?:{}){{{}}}'.format(self.pattern, index))
elif isinstance(index, tuple) and len(index) == 2:
if (
isinstance(index[0], int) and isinstance(index[1], int) and
index[0] < index[1]
):
if isinstance(self.pattern, bytes):
return r(
'(?:{}){{{},{}}}'.format(
self.pattern.decode('latin1'),
*index
).encode('latin1')
)
else:
return r(u'(?:{}){{{},{}}}'.format(self.pattern, *index))
elif (
isinstance(index[0], int) and index[1] is Ellipsis
):
if index[0] == 0:
if isinstance(self.pattern, bytes):
return r(
'(?:{})*'.format(
self.pattern.decode('latin1')
).encode('latin1')
)
else:
return r(u'(?:{})*'.format(self.pattern))
elif index[0] == 1:
if isinstance(self.pattern, bytes):
return r(
'(?:{})+'.format(
self.pattern.decode('latin1')
).encode('latin1')
)
else:
return r(u'(?:{})+'.format(self.pattern))
else:
if isinstance(self.pattern, bytes):
return r(
'(?:{pattern}){{{n}}}(?:{pattern})*'.format(
pattern=self.pattern.decode('latin1'),
n=index[0]
).encode('latin1')
)
else:
return r(
u'(?:{pattern}){{{n}}}(?:{pattern})*'.format(
pattern=self.pattern, n=index[0]
)
)
return NotImplemented
def search(self, string, *args):
return self._compiled.search(string, *args)
def match(self, string, *args):
return self._compiled.match(string, *args)
def split(self, string, maxsplit=0):
return self._compiled.split(string, maxsplit=maxsplit)
def findall(self, string, *args):
return self._compiled.findall(string, *args)
def finditer(self, string, *args):
return self._compiled.finditer(string, *args)
def sub(self, repl, string, count=0):
return self._compiled.sub(repl, string, count=count)
def subn(self, repl, string, count=0):
return self._compiled.subn(repl, string, count=count)
def grouped(self, name=None):
"""
Returns this regular expression object, wrapped in a group.
"""
if name is None:
if isinstance(self.pattern, bytes):
return r('({})'.format(self.pattern.decode('latin1')).encode('latin1'))
return r(u'({})'.format(self.pattern))
else:
if isinstance(self.pattern, bytes):
return r('(?P<{}>{})'.format(
name,
self.pattern.decode('latin1')
).encode('latin1'))
return r(u'(?P<{}>{})'.format(name, self.pattern))