1try: 2 import ure as re 3except ImportError: 4 try: 5 import re 6 except ImportError: 7 print("SKIP") 8 raise SystemExit 9 10r = re.compile(".+") 11m = r.match("abc") 12print(m.group(0)) 13try: 14 m.group(1) 15except IndexError: 16 print("IndexError") 17 18# conversion of re and match to string 19str(r) 20str(m) 21 22r = re.compile("(.+)1") 23m = r.match("xyz781") 24print(m.group(0)) 25print(m.group(1)) 26try: 27 m.group(2) 28except IndexError: 29 print("IndexError") 30 31r = re.compile("[a-cu-z]") 32m = r.match("a") 33print(m.group(0)) 34m = r.match("z") 35print(m.group(0)) 36m = r.match("d") 37print(m) 38m = r.match("A") 39print(m) 40print("===") 41 42r = re.compile("[^a-cu-z]") 43m = r.match("a") 44print(m) 45m = r.match("z") 46print(m) 47m = r.match("d") 48print(m.group(0)) 49m = r.match("A") 50print(m.group(0)) 51print("===") 52 53# '-' character within character class block 54print(re.match("[-a]+", "-a]d").group(0)) 55print(re.match("[a-]+", "-a]d").group(0)) 56print("===") 57 58r = re.compile("o+") 59m = r.search("foobar") 60print(m.group(0)) 61try: 62 m.group(1) 63except IndexError: 64 print("IndexError") 65 66 67m = re.match(".*", "foo") 68print(m.group(0)) 69 70m = re.search("w.r", "hello world") 71print(m.group(0)) 72 73m = re.match("a+?", "ab") 74print(m.group(0)) 75m = re.match("a*?", "ab") 76print(m.group(0)) 77m = re.match("^ab$", "ab") 78print(m.group(0)) 79m = re.match("a|b", "b") 80print(m.group(0)) 81m = re.match("a|b|c", "c") 82print(m.group(0)) 83 84# Case where anchors fail to match 85r = re.compile("^b|b$") 86m = r.search("abc") 87print(m) 88 89try: 90 re.compile("*") 91except: 92 print("Caught invalid regex") 93 94# bytes objects 95m = re.match(rb"a+?", b"ab") 96print(m.group(0)) 97print("===") 98 99# escaping 100m = re.match(r"a\.c", "a.c") 101print(m.group(0) if m else "") 102m = re.match(r"a\.b", "abc") 103print(m is None) 104m = re.match(r"a\.b", "a\\bc") 105print(m is None) 106m = re.match(r"[a\-z]", "abc") 107print(m.group(0)) 108m = re.match(r"[.\]]*", ".].]a") 109print(m.group(0)) 110m = re.match(r"[.\]+]*", ".]+.]a") 111print(m.group(0)) 112m = re.match(r"[a-f0-9x\-yz]*", "abxcd1-23") 113print(m.group(0)) 114m = re.match(r"[a\\b]*", "a\\aa\\bb\\bbab") 115print(m.group(0)) 116m = re.search(r"[a\-z]", "-") 117print(m.group(0)) 118m = re.search(r"[a\-z]", "f") 119print(m is None) 120m = re.search(r"[a\]z]", "a") 121print(m.group(0)) 122print(re.compile(r"[-a]").split("foo-bar")) 123print(re.compile(r"[a-]").split("foo-bar")) 124print(re.compile(r"[ax\-]").split("foo-bar")) 125print(re.compile(r"[a\-x]").split("foo-bar")) 126print(re.compile(r"[\-ax]").split("foo-bar")) 127print("===") 128 129# Module functions take str/bytes/re. 130for f in (re.match, re.search): 131 print(f(".", "foo").group(0)) 132 print(f(b".", b"foo").group(0)) 133 print(f(re.compile("."), "foo").group(0)) 134 try: 135 f(123, "a") 136 except TypeError: 137 print("TypeError") 138print("===") 139