kopia lustrzana https://github.com/micropython/micropython-lib
re-pcre: Implement split() method.
rodzic
ef315ca90e
commit
cc4cd5b47a
|
@ -96,6 +96,27 @@ class PCREPattern:
|
||||||
res += repl
|
res += repl
|
||||||
s = s[end:]
|
s = s[end:]
|
||||||
|
|
||||||
|
def split(self, s, maxsplit=0):
|
||||||
|
res = []
|
||||||
|
while True:
|
||||||
|
m = self.search(s)
|
||||||
|
g = None
|
||||||
|
if m:
|
||||||
|
g = m.group(0)
|
||||||
|
if not m or not g:
|
||||||
|
res.append(s)
|
||||||
|
return res
|
||||||
|
beg, end = m.span(0)
|
||||||
|
res.append(s[:beg])
|
||||||
|
if m.num > 1:
|
||||||
|
res.extend(m.groups())
|
||||||
|
s = s[end:]
|
||||||
|
if maxsplit > 0:
|
||||||
|
maxsplit -= 1
|
||||||
|
if maxsplit == 0:
|
||||||
|
res.append(s)
|
||||||
|
return res
|
||||||
|
|
||||||
|
|
||||||
def compile(pattern, flags=0):
|
def compile(pattern, flags=0):
|
||||||
errptr = bytes(4)
|
errptr = bytes(4)
|
||||||
|
@ -120,6 +141,11 @@ def sub(pattern, repl, s, count=0, flags=0):
|
||||||
return r.sub(repl, s)
|
return r.sub(repl, s)
|
||||||
|
|
||||||
|
|
||||||
|
def split(pattern, s, maxsplit=0, flags=0):
|
||||||
|
r = compile(pattern, flags)
|
||||||
|
return r.split(s, maxsplit)
|
||||||
|
|
||||||
|
|
||||||
def escape(s):
|
def escape(s):
|
||||||
res = ""
|
res = ""
|
||||||
for c in s:
|
for c in s:
|
||||||
|
|
|
@ -5,7 +5,7 @@ sys.path.pop(0)
|
||||||
from distutils.core import setup
|
from distutils.core import setup
|
||||||
|
|
||||||
setup(name='micropython-re-pcre',
|
setup(name='micropython-re-pcre',
|
||||||
version='0.1.5',
|
version='0.1.6',
|
||||||
description='re module for MicroPython, based on PCRE and FFI',
|
description='re module for MicroPython, based on PCRE and FFI',
|
||||||
url='https://github.com/micropython/micropython/issues/405',
|
url='https://github.com/micropython/micropython/issues/405',
|
||||||
author='Paul Sokolovsky',
|
author='Paul Sokolovsky',
|
||||||
|
|
|
@ -20,3 +20,11 @@ assert m.groups() == ('24', '1632')
|
||||||
assert m.group(2, 1) == ('1632', '24')
|
assert m.group(2, 1) == ('1632', '24')
|
||||||
|
|
||||||
assert re.escape(r"1243*&[]_dsfAd") == r"1243\*\&\[\]_dsfAd"
|
assert re.escape(r"1243*&[]_dsfAd") == r"1243\*\&\[\]_dsfAd"
|
||||||
|
|
||||||
|
assert re.split('x*', 'foo') == ['foo']
|
||||||
|
assert re.split("(?m)^$", "foo\n\nbar\n") == ["foo\n\nbar\n"]
|
||||||
|
assert re.split('\W+', 'Words, words, words.') == ['Words', 'words', 'words', '']
|
||||||
|
assert re.split('(\W+)', 'Words, words, words.') == ['Words', ', ', 'words', ', ', 'words', '.', '']
|
||||||
|
assert re.split('\W+', 'Words, words, words.', 1) == ['Words', 'words, words.']
|
||||||
|
assert re.split('[a-f]+', '0a3B9', flags=re.IGNORECASE) == ['0', '3', '9']
|
||||||
|
assert re.split('(\W+)', '...words, words...') == ['', '...', 'words', ', ', 'words', '...', '']
|
||||||
|
|
Ładowanie…
Reference in New Issue