2014-09-14 18:14:28 -04:00
|
|
|
try:
|
|
|
|
import ure as re
|
|
|
|
except ImportError:
|
2017-02-14 17:56:22 -05:00
|
|
|
try:
|
|
|
|
import re
|
|
|
|
except ImportError:
|
|
|
|
print("SKIP")
|
2017-06-10 13:14:16 -04:00
|
|
|
raise SystemExit
|
2014-09-14 18:14:28 -04:00
|
|
|
|
|
|
|
r = re.compile(".+")
|
|
|
|
m = r.match("abc")
|
|
|
|
print(m.group(0))
|
|
|
|
try:
|
|
|
|
m.group(1)
|
|
|
|
except IndexError:
|
|
|
|
print("IndexError")
|
|
|
|
|
2017-01-17 01:26:59 -05:00
|
|
|
# conversion of re and match to string
|
|
|
|
str(r)
|
|
|
|
str(m)
|
|
|
|
|
2014-09-14 18:14:28 -04:00
|
|
|
r = re.compile("(.+)1")
|
|
|
|
m = r.match("xyz781")
|
|
|
|
print(m.group(0))
|
|
|
|
print(m.group(1))
|
|
|
|
try:
|
|
|
|
m.group(2)
|
|
|
|
except IndexError:
|
|
|
|
print("IndexError")
|
|
|
|
|
2017-07-24 19:09:47 -04:00
|
|
|
r = re.compile(r"\n")
|
|
|
|
m = r.match("\n")
|
|
|
|
print(m.group(0))
|
|
|
|
m = r.match("\\")
|
|
|
|
print(m)
|
|
|
|
r = re.compile(r"[\n-\r]")
|
|
|
|
m = r.match("\n")
|
|
|
|
print(m.group(0))
|
|
|
|
r = re.compile(r"[\]]")
|
|
|
|
m = r.match("]")
|
|
|
|
print(m.group(0))
|
|
|
|
print("===")
|
|
|
|
|
2014-10-16 06:56:13 -04:00
|
|
|
r = re.compile("[a-cu-z]")
|
2014-10-14 21:43:13 -04:00
|
|
|
m = r.match("a")
|
|
|
|
print(m.group(0))
|
2014-10-16 06:56:13 -04:00
|
|
|
m = r.match("z")
|
|
|
|
print(m.group(0))
|
2014-10-14 21:43:13 -04:00
|
|
|
m = r.match("d")
|
|
|
|
print(m)
|
|
|
|
m = r.match("A")
|
|
|
|
print(m)
|
2014-10-16 06:56:13 -04:00
|
|
|
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))
|
2017-10-04 20:32:55 -04:00
|
|
|
print("===")
|
2014-10-16 06:56:13 -04:00
|
|
|
|
2017-10-04 20:32:55 -04:00
|
|
|
# '-' character within character class block
|
|
|
|
print(re.match("[-a]+", "-a]d").group(0))
|
|
|
|
print(re.match("[a-]+", "-a]d").group(0))
|
|
|
|
print("===")
|
2014-09-14 18:14:28 -04:00
|
|
|
|
|
|
|
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))
|
2015-03-14 18:56:02 -04:00
|
|
|
|
2021-03-15 09:57:36 -04:00
|
|
|
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))
|
2015-10-31 17:38:12 -04:00
|
|
|
|
2017-03-14 01:11:25 -04:00
|
|
|
# Case where anchors fail to match
|
|
|
|
r = re.compile("^b|b$")
|
|
|
|
m = r.search("abc")
|
|
|
|
print(m)
|
|
|
|
|
2015-10-31 17:38:12 -04:00
|
|
|
try:
|
|
|
|
re.compile("*")
|
|
|
|
except:
|
|
|
|
print("Caught invalid regex")
|
2017-06-30 18:25:23 -04:00
|
|
|
|
|
|
|
# bytes objects
|
2021-03-15 09:57:36 -04:00
|
|
|
m = re.match(rb"a+?", b"ab")
|
|
|
|
print(m.group(0))
|