Skip to content

Commit c18dbd8

Browse files
committed
Add Match.expand and Match.regs
1 parent cfdb602 commit c18dbd8

File tree

2 files changed

+14
-5
lines changed
  • graalpython

2 files changed

+14
-5
lines changed

graalpython/com.oracle.graal.python.test/src/tests/unittest_tags/test_re.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,9 @@
4747
*graalpython.lib-python.3.test.test_re.ReTests.test_empty_array
4848
*graalpython.lib-python.3.test.test_re.ReTests.test_enum
4949
*graalpython.lib-python.3.test.test_re.ReTests.test_error
50+
*graalpython.lib-python.3.test.test_re.ReTests.test_expand
5051
*graalpython.lib-python.3.test.test_re.ReTests.test_flags
52+
*graalpython.lib-python.3.test.test_re.ReTests.test_getattr
5153
*graalpython.lib-python.3.test.test_re.ReTests.test_group
5254
*graalpython.lib-python.3.test.test_re.ReTests.test_group_name_in_exception
5355
*graalpython.lib-python.3.test.test_re.ReTests.test_groupdict

graalpython/lib-graalpython/_sre.py

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,14 @@ def start(self, groupnum=0):
226226
idxarg = self.__groupidx(groupnum)
227227
return self.__result.getStart(idxarg)
228228

229+
def expand(self, template):
230+
import re
231+
return re._expand(self.__re, self, template)
232+
233+
@property
234+
def regs(self):
235+
return tuple(self.span(i) for i in range(self.__compiled_regex.groupCount))
236+
229237
@property
230238
def string(self):
231239
return self.__input_str
@@ -463,11 +471,10 @@ def subn(self, repl, string, count=0):
463471
else:
464472
literal = b'\\' not in repl
465473
if not literal:
466-
import sre_parse
467-
template = sre_parse.parse_template(repl, self)
468-
469-
def repl(match):
470-
return sre_parse.expand_template(template, match)
474+
import re
475+
repl = re._subx(self, repl)
476+
if not callable(repl):
477+
literal = True
471478

472479
while (count == 0 or n < count) and pos <= len(string):
473480
match_result = tregex_call_exec(pattern.exec, string, pos)

0 commit comments

Comments
 (0)