kopia lustrzana https://github.com/micropython/micropython-lib
re-pcre: sub(): support functional replacement arg.
rodzic
72b5eedd3d
commit
9ad8f7090d
|
@ -62,7 +62,8 @@ class PCREPattern:
|
||||||
return self.search(s, PCRE_ANCHORED)
|
return self.search(s, PCRE_ANCHORED)
|
||||||
|
|
||||||
def sub(self, repl, s):
|
def sub(self, repl, s):
|
||||||
assert "\\" not in repl, "Backrefs not implemented"
|
if not callable(repl):
|
||||||
|
assert "\\" not in repl, "Backrefs not implemented"
|
||||||
res = ""
|
res = ""
|
||||||
while s:
|
while s:
|
||||||
m = self.search(s)
|
m = self.search(s)
|
||||||
|
@ -70,8 +71,10 @@ class PCREPattern:
|
||||||
return res + s
|
return res + s
|
||||||
beg, end = m.span()
|
beg, end = m.span()
|
||||||
res += s[:beg]
|
res += s[:beg]
|
||||||
assert not callable(repl)
|
if callable(repl):
|
||||||
res += repl
|
res += repl(m)
|
||||||
|
else:
|
||||||
|
res += repl
|
||||||
s = s[end:]
|
s = s[end:]
|
||||||
|
|
||||||
|
|
||||||
|
|
Ładowanie…
Reference in New Issue