kopia lustrzana https://github.com/micropython/micropython-lib
re-pcre: Implement findall() method.
rodzic
5fe6d7933b
commit
0e4a1ab7e1
|
@ -122,6 +122,21 @@ class PCREPattern:
|
|||
res.append(s)
|
||||
return res
|
||||
|
||||
def findall(self, s):
|
||||
res = []
|
||||
while True:
|
||||
m = self.search(s)
|
||||
if not m:
|
||||
return res
|
||||
if m.num == 1:
|
||||
res.append(m.group(0))
|
||||
elif m.num == 2:
|
||||
res.append(m.group(1))
|
||||
else:
|
||||
res.append(m.groups())
|
||||
beg, end = m.span(0)
|
||||
s = s[end:]
|
||||
|
||||
|
||||
def compile(pattern, flags=0):
|
||||
errptr = bytes(4)
|
||||
|
@ -150,6 +165,10 @@ def split(pattern, s, maxsplit=0, flags=0):
|
|||
r = compile(pattern, flags)
|
||||
return r.split(s, maxsplit)
|
||||
|
||||
def findall(pattern, s, flags=0):
|
||||
r = compile(pattern, flags)
|
||||
return r.findall(s)
|
||||
|
||||
|
||||
def escape(s):
|
||||
res = ""
|
||||
|
|
|
@ -33,3 +33,12 @@ assert re.split('[a-f]+', '0a3B9', flags=re.IGNORECASE) == ['0', '3', '9']
|
|||
assert re.split('(\W+)', '...words, words...') == ['', '...', 'words', ', ', 'words', '...', '']
|
||||
|
||||
assert re.sub(r"[ :/?&]", "_", "http://foo.ua/bar/?a=1&b=baz/") == "http___foo.ua_bar__a=1_b=baz_"
|
||||
|
||||
text = "He was carefully disguised but captured quickly by police."
|
||||
assert re.findall(r"\w+ly", text) == ['carefully', 'quickly']
|
||||
|
||||
text = "He was carefully disguised but captured quickly by police."
|
||||
assert re.findall(r"(\w+)(ly)", text) == [('careful', 'ly'), ('quick', 'ly')]
|
||||
|
||||
text = "He was carefully disguised but captured quickly by police."
|
||||
assert re.findall(r"(\w+)ly", text) == ['careful', 'quick']
|
||||
|
|
Ładowanie…
Reference in New Issue