convert & in sed to \g<0> so python handles full-match replacements
This commit is contained in:
parent
e84c75e266
commit
7ebad30c2f
1 changed files with 4 additions and 10 deletions
|
@ -26,16 +26,10 @@ class SedReplace(Sed):
|
||||||
count: int
|
count: int
|
||||||
|
|
||||||
def match(self, s):
|
def match(self, s):
|
||||||
matches = list(self.pattern.finditer(s))
|
replace_copy = self.replace
|
||||||
if not self.count == 0:
|
for token in reversed(_tokens(replace_copy, "&")):
|
||||||
matches = matches[:self.count]
|
replace_copy = replace_copy[:token]+r"\g<0>"+replace_copy[token+1:]
|
||||||
|
s = re.sub(self.pattern, replace_copy, s, self.count)
|
||||||
for match in matches:
|
|
||||||
replace_copy = self.replace
|
|
||||||
for token in reversed(_tokens(replace_copy, "&")):
|
|
||||||
replace_copy = (
|
|
||||||
replace_copy[:token]+match.group(0)+replace_copy[token+1:])
|
|
||||||
s = re.sub(self.pattern, replace_copy, s, 1)
|
|
||||||
return s
|
return s
|
||||||
|
|
||||||
@dataclasses.dataclass
|
@dataclasses.dataclass
|
||||||
|
|
Loading…
Reference in a new issue