2014-09-14 23:14:28 +01:00
|
|
|
try:
|
|
|
|
import ure as re
|
|
|
|
except ImportError:
|
|
|
|
import re
|
|
|
|
|
|
|
|
r = re.compile(".+")
|
|
|
|
m = r.match("abc")
|
|
|
|
print(m.group(0))
|
|
|
|
try:
|
|
|
|
m.group(1)
|
|
|
|
except IndexError:
|
|
|
|
print("IndexError")
|
|
|
|
|
|
|
|
r = re.compile("(.+)1")
|
|
|
|
m = r.match("xyz781")
|
|
|
|
print(m.group(0))
|
|
|
|
print(m.group(1))
|
|
|
|
try:
|
|
|
|
m.group(2)
|
|
|
|
except IndexError:
|
|
|
|
print("IndexError")
|
|
|
|
|
2014-10-16 11:56:13 +01:00
|
|
|
r = re.compile("[a-cu-z]")
|
2014-10-15 02:43:13 +01:00
|
|
|
m = r.match("a")
|
|
|
|
print(m.group(0))
|
2014-10-16 11:56:13 +01:00
|
|
|
m = r.match("z")
|
|
|
|
print(m.group(0))
|
2014-10-15 02:43:13 +01:00
|
|
|
m = r.match("d")
|
|
|
|
print(m)
|
|
|
|
m = r.match("A")
|
|
|
|
print(m)
|
2014-10-16 11:56:13 +01: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))
|
|
|
|
|
2014-09-14 23:14:28 +01: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 22:56:02 +00: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 21:38:12 +00:00
|
|
|
|
|
|
|
try:
|
|
|
|
re.compile("*")
|
|
|
|
except:
|
|
|
|
print("Caught invalid regex")
|