我可以在不区分大小写的方法中使用以下解决方案替换字符串内容中的单词
http://code.activestate.com/recipes/552726/
import re
class str_cir(str):
''' A string with a built-in case-insensitive replacement method '''
def ireplace(self,old,new,count=0):
''' Behaves like S.replace(), but does so in a case-insensitive
fashion. '''
pattern = re.compile(re.escape(old),re.I)
return re.sub(pattern,new,self,count)
我的问题是我需要完全替换我提供的单词
para = "Train toy tram dog cat cow plane TOY Joy JoyTOY"
我需要用“ham”替换“toy”这个词,我得到了
'Train HAM tram dog cat cow plane HAM Joy JoyHAM'
我需要的是
'Train HAM tram dog cat cow plane HAM Joy JoyTOY'