Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master
Fetching contributors…

Cannot retrieve contributors at this time

executable file 120 lines (96 sloc) 3.169 kb
#!/usr/bin/env python3
# ----------
# RegExps.py
# ----------
import re
print("RegExps.py")
s = "b ab\naab 123"
a = re.split("ab", s)
assert type(a) is list
assert a == ['b ', '\na', ' 123']
s = "b ab\naab 123"
a = re.split("ba", s)
assert type(a) is list
assert a == [s]
s = "b ab\naab 123"
a = re.split("^b", s) # start of string
assert type(a) is list
assert a == ['', ' ab\naab 123']
s = "b ab\naab 123"
a = re.split("^a", s) # start of string
assert type(a) is list
assert a == [s]
s = "b ab\naab 123"
r = re.compile("^a", re.M) # multiline
assert str(type(r)) == "<class '_sre.SRE_Pattern'>"
a = r.split(s)
assert type(a) is list
assert a == ['b ab\n', 'ab 123']
s = "b ab\naab 123"
a = re.split("3$", s) # end of string
assert type(a) is list
assert a == ['b ab\naab 12', '']
s = "b ab\naab 123"
a = re.split("b$", s) # end of string
assert type(a) is list
assert a == [s]
s = "b ab\naab 123"
r = re.compile("b$", re.M) # multiline
assert str(type(r)) == "<class '_sre.SRE_Pattern'>"
a = r.split(s)
assert type(a) is list
assert a == ['b a', '\naab 123']
s = "b ab\naab 123"
a = re.split(".", s) # any character
assert type(a) is list
assert a == ['', '', '', '', '\n', '', '', '', '', '', '', '']
s = "b ab\naab 123"
a = re.split("\d", s) # any digit
assert type(a) is list
assert a == ['b ab\naab ', '', '', '']
s = "b ab\naab 123"
a = re.split("\D", s) # any non-digit
assert type(a) is list
assert a == ['', '', '', '', '', '', '', '', '', '123']
s = "b ab\naab 123"
a = re.split("\w", s) # any alphanumeric
assert type(a) is list
assert a == ['', ' ', '', '\n', '', '', ' ', '', '', '']
s = "b ab\naab 123"
a = re.split("\W", s) # any non-alphanumeric
assert type(a) is list
assert a == ['b', 'ab', 'aab', '123']
s = "b ab\naab 123"
m = re.search("(a*)b([^a]*)(a*)b", s) # * is zero or more
assert str(type(m)) == "<class '_sre.SRE_Match'>"
assert m.group(0) == "b ab"
assert m.group(1) == ""
assert m.group(2) == " "
assert m.group(3) == "a"
s = "b ab\naab 123"
m = re.search("(a+)b([^a]*)(a+)b", s) # + is one or more
assert str(type(m)) == "<class '_sre.SRE_Match'>"
assert m.group(0) == "ab\naab"
assert m.group(1) == "a"
assert m.group(2) == "\n"
assert m.group(3) == "aa"
s = "b ab\naab 123"
m = re.search("(a?)b([^a]*)(a?)b", s) # ? is zero or one
assert str(type(m)) == "<class '_sre.SRE_Match'>"
assert m.group(0) == "b ab"
assert m.group(1) == ""
assert m.group(2) == " "
assert m.group(3) == "a"
s = "b ab\naab 123"
t = re.sub("b ", "xx", s)
assert s == "b ab\naab 123"
assert t == "xxab\naaxx123"
s = "b ab\naab 123"
t = re.sub("b.", "xx", s)
assert s == "b ab\naab 123"
assert t == "xxab\naaxx123"
s = "b ab\naab 123"
t = re.sub("", "z", s)
assert s == "b ab\naab 123"
assert t == "zbz zazbz\nzazazbz z1z2z3z"
print("Done.")
Jump to Line
Something went wrong with that request. Please try again.