diff --git a/src/util/regex.py b/src/util/regex.py index 6348c6fb1..37eeb6d88 100644 --- a/src/util/regex.py +++ b/src/util/regex.py @@ -38,7 +38,16 @@ def replace(s, pattern, replacement): pattern (str): the search expression replacement (str): the string to replace each match with """ - return re.sub(pattern, replacement, s) + # the replacement string may contain invalid backreferences (like \1 or \g) + # which will cause python's regex to blow up. Since this should emulate + # the jam version exactly and the jam version didn't support + # backreferences, this version shouldn't either. re.sub + # allows replacement to be a callable; this is being used + # to simply return the replacement string and avoid the hassle + # of worrying about backreferences within the string. + def _replacement(matchobj): + return replacement + return re.sub(pattern, _replacement, s) @bjam_signature((['items', '*'], ['match'], ['replacement']))