summaryrefslogtreecommitdiff
path: root/tests/extmod/re_start_end_pos.py
blob: bd16584374b89580195f2def04a257de9cd3de10 (plain)
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# test start and end pos specification

try:
    import re
except ImportError:
    print("SKIP")
    raise SystemExit


def print_groups(match):
    print("----")
    try:
        if match is not None:
            i = 0
            while True:
                print(match.group(i))
                i += 1
    except IndexError:
        pass


p = re.compile(r"o")
m = p.match("dog")
print_groups(m)

m = p.match("dog", 1)
print_groups(m)

m = p.match("dog", 2)
print_groups(m)

# No match past end of input
m = p.match("dog", 5)
print_groups(m)

m = p.match("dog", 0, 1)
print_groups(m)

# Caret only matches the actual beginning
p = re.compile(r"^o")
m = p.match("dog", 1)
print_groups(m)

# End at beginning means searching empty string
p = re.compile(r"o")
m = p.match("dog", 1, 1)
print_groups(m)

# End before the beginning doesn't match anything
m = p.match("dog", 2, 1)
print_groups(m)

# Negative starting values don't crash
m = p.search("dog", -2)
print_groups(m)

m = p.search("dog", -2, -5)
print_groups(m)

# Search also works
print("--search")

p = re.compile(r"o")
m = p.search("dog")
print_groups(m)

m = p.search("dog", 1)
print_groups(m)

m = p.search("dog", 2)
print_groups(m)

# Negative starting values don't crash
m = p.search("dog", -2)
print_groups(m)

m = p.search("dog", -2, -5)
print_groups(m)