@@ -171,50 +171,54 @@ def end(self, groupnum=0):
171
171
172
172
def group (self , * args ):
173
173
if not args :
174
- return self .__group__ (0 )
174
+ return self .__group (0 )
175
175
elif len (args ) == 1 :
176
- return self .__group__ (args [0 ])
176
+ return self .__group (args [0 ])
177
177
else :
178
178
lst = []
179
179
for arg in args :
180
- lst .append (self .__group__ (arg ))
180
+ lst .append (self .__group (arg ))
181
181
return tuple (lst )
182
182
183
183
def groups (self , default = None ):
184
184
lst = []
185
185
for arg in range (1 , self .compiled_regex .groupCount ):
186
- lst .append (self .__group__ (arg , default ))
186
+ lst .append (self .__group (arg , default ))
187
187
return tuple (lst )
188
188
189
- def __groupidx__ (self , idx ):
190
- if isinstance (idx , str ):
191
- return self .compiled_regex .groups [idx ]
192
- else :
193
- return idx
194
-
195
- def __group__ (self , idx , default = None ):
196
- idxarg = self .__groupidx__ (idx )
189
+ def __getitem__ (self , item ):
190
+ return self .__group (item )
191
+
192
+ def __groupidx (self , idx ):
193
+ try :
194
+ if isinstance (idx , str ):
195
+ return self .compiled_regex .groups [idx ]
196
+ elif 0 <= idx < self .compiled_regex .groupCount :
197
+ return idx
198
+ except Exception :
199
+ pass
200
+ raise IndexError ("no such group" )
201
+
202
+ def __group (self , idx , default = None ):
203
+ idxarg = self .__groupidx (idx )
197
204
start = self .result .getStart (idxarg )
198
205
if start < 0 :
199
206
return default
200
207
else :
201
208
return self .input_str [start :self .result .getEnd (idxarg )]
202
209
203
210
def groupdict (self , default = None ):
204
- d = {}
205
- if self .compiled_regex .groups :
206
- assert dir (self .compiled_regex .groups )
207
- for k in dir (self .compiled_regex .groups ):
208
- idx = self .compiled_regex .groups [k ]
209
- d [k ] = self .__group__ (idx )
210
- return d
211
+ groups = self .compiled_regex .groups
212
+ if groups :
213
+ return {name : self .__group (name , default ) for name in dir (groups )}
214
+ return {}
211
215
212
216
def span (self , groupnum = 0 ):
213
- idxarg = self .__groupidx__ (groupnum )
217
+ idxarg = self .__groupidx (groupnum )
214
218
return (self .result .getStart (idxarg ), self .result .getEnd (idxarg ))
215
219
216
220
def start (self , groupnum = 0 ):
217
- idxarg = self .__groupidx__ (groupnum )
221
+ idxarg = self .__groupidx (groupnum )
218
222
return self .result .getStart (idxarg )
219
223
220
224
@property
0 commit comments