kopia lustrzana https://github.com/micropython/micropython-lib
re-pcre: Implement count arg to re.sub().
rodzic
c4c29b4f57
commit
5262fb8237
|
@ -1,5 +1,5 @@
|
||||||
name = re
|
name = re
|
||||||
srctype = micropython-lib
|
srctype = micropython-lib
|
||||||
type = module
|
type = module
|
||||||
version = 0.1.7
|
version = 0.1.8
|
||||||
author = Paul Sokolovsky
|
author = Paul Sokolovsky
|
||||||
|
|
|
@ -80,7 +80,7 @@ class PCREPattern:
|
||||||
def match(self, s, pos=0, endpos=-1):
|
def match(self, s, pos=0, endpos=-1):
|
||||||
return self.search(s, pos, endpos, PCRE_ANCHORED)
|
return self.search(s, pos, endpos, PCRE_ANCHORED)
|
||||||
|
|
||||||
def sub(self, repl, s):
|
def sub(self, repl, s, count=0):
|
||||||
if not callable(repl):
|
if not callable(repl):
|
||||||
assert "\\" not in repl, "Backrefs not implemented"
|
assert "\\" not in repl, "Backrefs not implemented"
|
||||||
res = ""
|
res = ""
|
||||||
|
@ -95,6 +95,10 @@ class PCREPattern:
|
||||||
else:
|
else:
|
||||||
res += repl
|
res += repl
|
||||||
s = s[end:]
|
s = s[end:]
|
||||||
|
if count != 0:
|
||||||
|
count -= 1
|
||||||
|
if count == 0:
|
||||||
|
return res + s
|
||||||
return res
|
return res
|
||||||
|
|
||||||
def split(self, s, maxsplit=0):
|
def split(self, s, maxsplit=0):
|
||||||
|
@ -139,7 +143,7 @@ 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, count)
|
||||||
|
|
||||||
|
|
||||||
def split(pattern, s, maxsplit=0, flags=0):
|
def split(pattern, s, maxsplit=0, flags=0):
|
||||||
|
|
|
@ -6,7 +6,7 @@ from setuptools import setup
|
||||||
|
|
||||||
|
|
||||||
setup(name='micropython-re-pcre',
|
setup(name='micropython-re-pcre',
|
||||||
version='0.1.7',
|
version='0.1.8',
|
||||||
description='re-pcre module for MicroPython',
|
description='re-pcre module for MicroPython',
|
||||||
long_description="This is a module reimplemented specifically for MicroPython standard library,\nwith efficient and lean design in mind. Note that this module is likely work\nin progress and likely supports just a subset of CPython's corresponding\nmodule. Please help with the development if you are interested in this\nmodule.",
|
long_description="This is a module reimplemented specifically for MicroPython standard library,\nwith efficient and lean design in mind. Note that this module is likely work\nin progress and likely supports just a subset of CPython's corresponding\nmodule. Please help with the development if you are interested in this\nmodule.",
|
||||||
url='https://github.com/micropython/micropython/issues/405',
|
url='https://github.com/micropython/micropython/issues/405',
|
||||||
|
|
|
@ -12,6 +12,9 @@ assert m.group(0) == "aaaa"
|
||||||
|
|
||||||
assert re.sub("a", "z", "caaab") == "czzzb"
|
assert re.sub("a", "z", "caaab") == "czzzb"
|
||||||
assert re.sub("a+", "z", "caaab") == "czb"
|
assert re.sub("a+", "z", "caaab") == "czb"
|
||||||
|
assert re.sub("a", "z", "caaab", 1) == "czaab"
|
||||||
|
assert re.sub("a", "z", "caaab", 2) == "czzab"
|
||||||
|
assert re.sub("a", "z", "caaab", 10) == "czzzb"
|
||||||
|
|
||||||
assert re.sub("a", lambda m: m.group(0) * 2, "caaab") == "caaaaaab"
|
assert re.sub("a", lambda m: m.group(0) * 2, "caaab") == "caaaaaab"
|
||||||
|
|
||||||
|
|
Ładowanie…
Reference in New Issue