DRYな備忘録

Don't Repeat Yourself.

Pythonでの文字列置換メモ【import re】【Python】

正規表現で文字列置換

>>> import re
>>> src = 'this is milk for this cat'
>>> result = re.sub(r'this', 'that', src)
>>> re.sub(r'this', 'that', src)
'that is milk for that cat'
>>> re.sub(r'this', 'that', src)
'that is milk for that cat'
>>> re.sub(r'this', 'that', src, 1)
'that is milk for this cat'
>>> re.sub(r'[a-z]+is\s', 'These ', src, 1)
'These is milk for this cat'
>>> re.sub(r'[a-z]+is\s', 'These ', src)
'These is milk for These cat'

複数形にはisじゃなくてareだと思いました