@@ -176,13 +176,14 @@ def setup(sre_compiler, error_class, flags_table):
176
176
177
177
178
178
class Match ():
179
- def __init__ (self , pattern , pos , endpos , result , input_str , compiled_regex ):
179
+ def __init__ (self , pattern , pos , endpos , result , input_str , compiled_regex , indexgroup ):
180
180
self .__result = result
181
181
self .__compiled_regex = compiled_regex
182
182
self .__re = pattern
183
183
self .__pos = pos
184
184
self .__endpos = endpos
185
185
self .__input_str = input_str
186
+ self .__indexgroup = indexgroup
186
187
187
188
def end (self , groupnum = 0 ):
188
189
return self .__result .getEnd (groupnum )
@@ -270,16 +271,16 @@ def endpos(self):
270
271
@property
271
272
def lastgroup (self ):
272
273
lastindex = self .lastindex
273
- if lastindex is not None :
274
- return self .__group ( lastindex )
274
+ if lastindex is not None and self . __indexgroup is not None and lastindex in self . __indexgroup :
275
+ return self .__indexgroup [ lastindex ]
275
276
276
277
@property
277
278
def lastindex (self ):
278
- lastindex = None
279
- for index in range ( 1 , self . __compiled_regex . groupCount ) :
280
- if self . __result . getStart ( index ) >= 0 :
281
- lastindex = index
282
- return lastindex
279
+ lastindex = self . __result . lastGroup
280
+ if lastindex == - 1 :
281
+ return None
282
+ else :
283
+ return lastindex
283
284
284
285
def __repr__ (self ):
285
286
return "<%s object; span=%r, match=%r>" % (type (self ).__name__ , self .span (), self .group ())
@@ -315,14 +316,17 @@ def __init__(self, pattern, flags):
315
316
groups = compiled_regex .groups
316
317
if groups is None :
317
318
self .groupindex = {}
319
+ self .__indexgroup = {}
318
320
else :
319
321
group_names = dir (groups )
320
322
if isinstance (groups , __graalpython__ .ForeignType ):
321
323
# tregex groups object
322
324
self .groupindex = _mappingproxy ({name : getattr (groups , name ) for name in group_names })
325
+ self .__indexgroup = {getattr (groups , name ): name for name in group_names }
323
326
else :
324
327
# _sre._NamedCaptureGroups
325
328
self .groupindex = _mappingproxy ({name : groups [name ] for name in group_names })
329
+ self .__indexgroup = {groups [name ]: name for name in group_names }
326
330
327
331
@property
328
332
def flags (self ):
@@ -408,7 +412,7 @@ def _search(self, pattern, string, pos, endpos, sticky=False):
408
412
pattern = self .__tregex_compile (pattern , self .__flags_str + ("y" if sticky else "" ))
409
413
result = tregex_call_exec (pattern .exec , substring , pos )
410
414
if result .isMatch :
411
- return Match (self , pos , endpos , result , string , pattern )
415
+ return Match (self , pos , endpos , result , string , pattern , self . __indexgroup )
412
416
else :
413
417
return None
414
418
@@ -444,7 +448,7 @@ def __finditer_gen(self, string, compiled_regex, substring, pos, endpos):
444
448
if not result .isMatch :
445
449
break
446
450
else :
447
- yield Match (self , pos , endpos , result , string , compiled_regex )
451
+ yield Match (self , pos , endpos , result , string , compiled_regex , self . __indexgroup )
448
452
no_progress = (result .getStart (0 ) == result .getEnd (0 ))
449
453
pos = result .getEnd (0 ) + no_progress
450
454
return
@@ -465,7 +469,7 @@ def findall(self, string, pos=0, endpos=maxsize):
465
469
elif group_count == 2 :
466
470
matchlist .append (self .__sanitize_out_type (string [result .getStart (1 ):result .getEnd (1 )]))
467
471
else :
468
- matchlist .append (tuple (map (self .__sanitize_out_type , Match (self , pos , endpos , result , string , compiled_regex ).groups ())))
472
+ matchlist .append (tuple (map (self .__sanitize_out_type , Match (self , pos , endpos , result , string , compiled_regex , self . __indexgroup ).groups ())))
469
473
no_progress = (result .getStart (0 ) == result .getEnd (0 ))
470
474
pos = result .getEnd (0 ) + no_progress
471
475
return matchlist
@@ -503,7 +507,7 @@ def subn(self, repl, string, count=0):
503
507
if literal :
504
508
result .append (repl )
505
509
else :
506
- _srematch = Match (self , pos , - 1 , match_result , string , pattern )
510
+ _srematch = Match (self , pos , - 1 , match_result , string , pattern , self . __indexgroup )
507
511
_repl = repl (_srematch )
508
512
result .append (_repl )
509
513
pos = end
0 commit comments