import re
s = 'my name is mike'
print(s.split())
p = re.compile(r'\W+')
print(p.split(s))
p = re.compile('blue|white|red')
print(p.sub('colour', 'blue socks and red shoes'))
print(p.sub('colour', 'blue socks and red shoes', count=1))
print(p.subn('colour', 'blue socks and red shoes'))
実行結果:
['my', 'name', 'is', 'mike']
['my', 'name', 'is', 'mike']
colour socks and colour shoes
colour socks and red shoes
('colour socks and colour shoes', 2)