forked from micropython/micropython-lib
- Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_re.py
62 lines (48 loc) · 2.08 KB
/
test_re.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
importre
m=re.search(r"a+", "caaab")
assertm.group(0) =="aaa"
assertm.group() =="aaa"
m=re.match(r"(?ms)foo.*\Z", "foo\nbar")
assertm.group(0) =="foo\nbar"
assertre.match(r"a+", "caaab") isNone
m=re.match(r"a+", "aaaab")
assertm.group(0) =="aaaa"
assertre.sub("a", "z", "caaab") =="czzzb"
assertre.sub("a+", "z", "caaab") =="czb"
assertre.sub("a", "z", "caaab", 1) =="czaab"
assertre.sub("a", "z", "caaab", 2) =="czzab"
assertre.sub("a", "z", "caaab", 10) =="czzzb"
assertre.sub("a", lambdam: m.group(0) *2, "caaab") =="caaaaaab"
m=re.match(r"(\d+)\.(\d+)", "24.1632")
assertm.groups() == ("24", "1632")
assertm.group(2, 1) == ("1632", "24")
assertre.escape(r"1243*&[]_dsfAd") ==r"1243\*\&\[\]_dsfAd"
assertre.split("x*", "foo") == ["foo"]
assertre.split("(?m)^$", "foo\n\nbar\n") == ["foo\n\nbar\n"]
assertre.split("\W+", "Words, words, words.") == ["Words", "words", "words", ""]
assertre.split("(\W+)", "Words, words, words.") == [
"Words",
", ",
"words",
", ",
"words",
".",
"",
]
assertre.split("\W+", "Words, words, words.", 1) == ["Words", "words, words."]
assertre.split("[a-f]+", "0a3B9", flags=re.IGNORECASE) == ["0", "3", "9"]
assertre.split("(\W+)", "...words, words...") == ["", "...", "words", ", ", "words", "...", ""]
assertre.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."
assertre.findall(r"\w+ly", text) == ["carefully", "quickly"]
text="He was carefully disguised but captured quickly by police."
assertre.findall(r"(\w+)(ly)", text) == [("careful", "ly"), ("quick", "ly")]
text="He was carefully disguised but captured quickly by police."
assertre.findall(r"(\w+)ly", text) == ["careful", "quick"]
_leading_whitespace_re=re.compile("(^[ \t]*)(?:[^ \t\n])", re.MULTILINE)
text="\tfoo\n\tbar"
indents=_leading_whitespace_re.findall(text)
assertindents== ["\t", "\t"]
text=" \thello there\n\t how are you?"
indents=_leading_whitespace_re.findall(text)
assertindents== [" \t", " \t "]