micropython-lib/re-pcre/test_re.py

31 wiersze
1.1 KiB
Python
Czysty Zwykły widok Historia

2014-04-11 18:18:31 +00:00
import re
m = re.search(r"a+", "caaab")
assert m.group(0) == "aaa"
m = re.match(r"(?ms)foo.*\Z", "foo\nbar")
assert m.group(0) == "foo\nbar"
2014-04-11 18:18:31 +00:00
assert re.match(r"a+", "caaab") is None
m = re.match(r"a+", "aaaab")
assert m.group(0) == "aaaa"
assert re.sub("a", "z", "caaab") == "czzzb"
assert re.sub("a+", "z", "caaab") == "czb"
assert re.sub("a", lambda m: m.group(0) * 2, "caaab") == "caaaaaab"
m = re.match(r"(\d+)\.(\d+)", "24.1632")
assert m.groups() == ('24', '1632')
assert m.group(2, 1) == ('1632', '24')
2014-05-09 20:07:29 +00:00
assert re.escape(r"1243*&[]_dsfAd") == r"1243\*\&\[\]_dsfAd"
2014-05-11 09:20:31 +00:00
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', '...', '']