import re 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))
输出:
colour socks and colour shoes colour socks and red shoes
import re p = re.compile(‘(blue|white|red)’) print(p.subn(‘colour’,'blue socks and red shoes’)) print(p.subn(‘colour’,'blue socks and red shoes’, count=1))
输出
(‘colour socks and colour shoes’, 2) (‘colour socks and red shoes’, 1)