import re
patterns = "dog|cat|fox"
repl = "animal"
sentence = "dog, cat, fox, pen."
re.sub(patterns, repl, sentence)
>> 'animal, animal, animal, pen.'

replaceではなくre.subで置換するパターンを正規表現で指定するのだ。

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です