Insert literal replacement text verbatim when use_regex is off (review SE-2)

re.sub always parses the replacement as a template, so a backslash in
the replacement (e.g. C:\1) raised PatternError even in literal mode.
Also drop an unused import and align js import paths with the
documented two-level extension path. Bump to 2.0.13.
This commit is contained in:
BRADSEC
2026-07-03 10:52:03 +10:00
parent 29d6e99dc2
commit 67b5a0e37f
5 changed files with 21 additions and 6 deletions
+14
View File
@@ -106,6 +106,20 @@ class TestStringNodes(unittest.TestCase):
)[0]
self.assertEqual(result, "Hello everyone")
def test_replace_literal_backslash_replacement(self):
# With use_regex disabled, replacement text is literal: backslash
# sequences must not be treated as re.sub group references.
result = self.replace_node.string_replace(
input_string="path is X",
replacement_pairs=r"X::C:\1",
replacement_delimiter="::",
match_case=False,
match_whole_string=True,
preserve_punctuation=True,
remove_extra_spaces=False
)[0]
self.assertEqual(result, r"path is C:\1")
def test_replace_multiple_pairs(self):
input_string = "The quick brown fox jumps over the lazy dog"
replacement_pairs = """quick::slow