File: ure1.py

package info (click to toggle)
giac 1.6.0.41%2Bdfsg1-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 64,540 kB
  • sloc: cpp: 351,842; ansic: 105,138; python: 30,545; javascript: 8,675; yacc: 2,690; lex: 2,449; makefile: 1,243; sh: 579; perl: 314; lisp: 216; asm: 62; java: 41; sed: 16; csh: 7; pascal: 6
file content (110 lines) | stat: -rw-r--r-- 2,467 bytes parent folder | download | duplicates (3)
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
try:
    import ure as re
except ImportError:
    try:
        import re
    except ImportError:
        print("SKIP")
        raise SystemExit

r = re.compile(".+")
m = r.match("abc")
print(m.group(0))
try:
    m.group(1)
except IndexError:
    print("IndexError")

# conversion of re and match to string
str(r)
str(m)

r = re.compile("(.+)1")
m = r.match("xyz781")
print(m.group(0))
print(m.group(1))
try:
    m.group(2)
except IndexError:
    print("IndexError")

r = re.compile("[a-cu-z]")
m = r.match("a")
print(m.group(0))
m = r.match("z")
print(m.group(0))
m = r.match("d")
print(m)
m = r.match("A")
print(m)
print("===")

r = re.compile("[^a-cu-z]")
m = r.match("a")
print(m)
m = r.match("z")
print(m)
m = r.match("d")
print(m.group(0))
m = r.match("A")
print(m.group(0))
print("===")

# '-' character within character class block
print(re.match("[-a]+", "-a]d").group(0))
print(re.match("[a-]+", "-a]d").group(0))
print("===")

r = re.compile("o+")
m = r.search("foobar")
print(m.group(0))
try:
    m.group(1)
except IndexError:
    print("IndexError")


m = re.match(".*", "foo")
print(m.group(0))

m = re.search("w.r", "hello world")
print(m.group(0))

m = re.match('a+?', 'ab');  print(m.group(0))
m = re.match('a*?', 'ab');  print(m.group(0))
m = re.match('^ab$', 'ab'); print(m.group(0))
m = re.match('a|b', 'b');   print(m.group(0))
m = re.match('a|b|c', 'c'); print(m.group(0))

# Case where anchors fail to match
r = re.compile("^b|b$")
m = r.search("abc")
print(m)

try:
    re.compile("*")
except:
    print("Caught invalid regex")

# bytes objects
m = re.match(rb'a+?', b'ab');  print(m.group(0))
print("===")

# escaping
m = re.match(r'a\.c', 'a.c'); print(m.group(0) if m else '')
m = re.match(r'a\.b', 'abc'); print(m is None)
m = re.match(r'a\.b', 'a\\bc'); print(m is None)
m = re.match(r'[a\-z]', 'abc'); print(m.group(0))
m = re.match(r'[.\]]*', '.].]a'); print(m.group(0))
m = re.match(r'[.\]+]*', '.]+.]a'); print(m.group(0))
m = re.match(r'[a-f0-9x\-yz]*', 'abxcd1-23'); print(m.group(0))
m = re.match(r'[a\\b]*', 'a\\aa\\bb\\bbab'); print(m.group(0))
m = re.search(r'[a\-z]', '-'); print(m.group(0))
m = re.search(r'[a\-z]', 'f'); print(m is None)
m = re.search(r'[a\]z]', 'a'); print(m.group(0))
print(re.compile(r'[-a]').split('foo-bar'))
print(re.compile(r'[a-]').split('foo-bar'))
print(re.compile(r'[ax\-]').split('foo-bar'))
print(re.compile(r'[a\-x]').split('foo-bar'))
print(re.compile(r'[\-ax]').split('foo-bar'))
print("===")