kopia lustrzana https://github.com/micropython/micropython-lib
re-pcre: Add escape() function.
rodzic
d5ba7f7eba
commit
3ff262e08f
|
@ -116,3 +116,13 @@ def match(pattern, string, flags=0):
|
||||||
def sub(pattern, repl, s, count=0, flags=0):
|
def sub(pattern, repl, s, count=0, flags=0):
|
||||||
r = compile(pattern, flags)
|
r = compile(pattern, flags)
|
||||||
return r.sub(repl, s)
|
return r.sub(repl, s)
|
||||||
|
|
||||||
|
|
||||||
|
def escape(s):
|
||||||
|
res = ""
|
||||||
|
for c in s:
|
||||||
|
if '0' <= c <= '9' or 'A' <= c <= 'Z' or 'a' <= c <= 'z' or c == '_':
|
||||||
|
res += c
|
||||||
|
else:
|
||||||
|
res += "\\" + c
|
||||||
|
return res
|
||||||
|
|
|
@ -14,3 +14,5 @@ assert re.sub("a", lambda m: m.group(0) * 2, "caaab") == "caaaaaab"
|
||||||
|
|
||||||
m = re.match(r"(\d+)\.(\d+)", "24.1632")
|
m = re.match(r"(\d+)\.(\d+)", "24.1632")
|
||||||
assert m.groups() == ('24', '1632')
|
assert m.groups() == ('24', '1632')
|
||||||
|
|
||||||
|
assert re.escape(r"1243*&[]_dsfAd") == r"1243\*\&\[\]_dsfAd"
|
||||||
|
|
Ładowanie…
Reference in New Issue