[PYTHON] Splitting re.split and replacing re.compile

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'))

Execution result:

['my', 'name', 'is', 'mike']
['my', 'name', 'is', 'mike']
colour socks and colour shoes
colour socks and red shoes
('colour socks and colour shoes', 2)

Recommended Posts

Splitting re.split and replacing re.compile
re.split and re.compile
re.group, re.compile and re.VERBOSE
Tips for replacing and debugging functions