You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
136 lines
2.6 KiB
136 lines
2.6 KiB
10 years ago
|
try:
|
||
2 years ago
|
import re
|
||
10 years ago
|
except ImportError:
|
||
2 years ago
|
print("SKIP")
|
||
|
raise SystemExit
|
||
10 years ago
|
|
||
|
r = re.compile(".+")
|
||
|
m = r.match("abc")
|
||
|
print(m.group(0))
|
||
|
try:
|
||
|
m.group(1)
|
||
|
except IndexError:
|
||
|
print("IndexError")
|
||
|
|
||
8 years ago
|
# conversion of re and match to string
|
||
|
str(r)
|
||
|
str(m)
|
||
|
|
||
10 years ago
|
r = re.compile("(.+)1")
|
||
|
m = r.match("xyz781")
|
||
|
print(m.group(0))
|
||
|
print(m.group(1))
|
||
|
try:
|
||
|
m.group(2)
|
||
|
except IndexError:
|
||
|
print("IndexError")
|
||
|
|
||
10 years ago
|
r = re.compile("[a-cu-z]")
|
||
10 years ago
|
m = r.match("a")
|
||
|
print(m.group(0))
|
||
10 years ago
|
m = r.match("z")
|
||
|
print(m.group(0))
|
||
10 years ago
|
m = r.match("d")
|
||
|
print(m)
|
||
|
m = r.match("A")
|
||
|
print(m)
|
||
10 years ago
|
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))
|
||
7 years ago
|
print("===")
|
||
10 years ago
|
|
||
7 years ago
|
# '-' character within character class block
|
||
|
print(re.match("[-a]+", "-a]d").group(0))
|
||
|
print(re.match("[a-]+", "-a]d").group(0))
|
||
|
print("===")
|
||
10 years ago
|
|
||
|
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))
|
||
10 years ago
|
|
||
5 years ago
|
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))
|
||
9 years ago
|
|
||
8 years ago
|
# Case where anchors fail to match
|
||
|
r = re.compile("^b|b$")
|
||
|
m = r.search("abc")
|
||
|
print(m)
|
||
|
|
||
9 years ago
|
try:
|
||
|
re.compile("*")
|
||
|
except:
|
||
|
print("Caught invalid regex")
|
||
7 years ago
|
|
||
|
# bytes objects
|
||
5 years ago
|
m = re.match(rb"a+?", b"ab")
|
||
|
print(m.group(0))
|
||
5 years ago
|
print("===")
|
||
|
|
||
|
# escaping
|
||
5 years ago
|
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"))
|
||
5 years ago
|
print("===")
|
||
4 years ago
|
|
||
|
# Module functions take str/bytes/re.
|
||
|
for f in (re.match, re.search):
|
||
|
print(f(".", "foo").group(0))
|
||
|
print(f(b".", b"foo").group(0))
|
||
|
print(f(re.compile("."), "foo").group(0))
|
||
|
try:
|
||
|
f(123, "a")
|
||
|
except TypeError:
|
||
|
print("TypeError")
|
||
|
print("===")
|