Here it is my backporting which has at least one problem (thanks tim-pietzcker ), but does not require recompilation of regular expressions:
import re def fullmatch(regex, string, flags=0): """Emulate python-3.4 re.fullmatch().""" m = re.match(regex, string, flags=flags) if m and m.span()[1] == len(string): return m
And here are a few test cases confirming the aforementioned emulation function.
def compare_expansion(regex, s, template): m1 = re.fullmatch(regex, s) s1 = m1.expand(template) if m1 else '<NO-MATCH>' m2 = fullmatch(regex, s) s2 = m2.expand(template) if m2 else '<NO-MATCH>' if s1 != s2: raise AssertionError("\n PY-3: '%s' \n PY-2: '%s' " % (s1, s2)) compare_expansion('.*', 'foo', r'A') compare_expansion('(.*)', 'foo', r'A_\1') compare_expansion('(.*)', 'foo', r'A_\g<0>') compare_expansion('a.*', 'afoo&', r'A') compare_expansion('a(\w*)', 'afoo&', r'A_\1') compare_expansion('a(\w*)', 'afoo&', r'A_\g<0>')
source share