home changes contents help options

文字列contentを頭から走査し、特定の文字列papasanにマッチしたら文字列mamasanに置き換える。:

>>> import re
>>> re_papasan = re.compile('papasan')

>>> content = 'papasan1 papasan2 papasan3'
>>> end = 0
>>> new_content = ''

>>> for m in re_papasan.finditer(content):
...     new_content += content[end:m.start()]
...     new_content += 'mamasan'
...     end = m.end()
... 
>>> new_content += content[end:]
>>> print new_content
mamasan1 mamasan2 mamasan3
>>>