Reputation: 483
I want to replace words in my document with a Python dictionary and I want to realize case insensitive replacements. Like we have a string:
string = 'spam fOo bar foo bar spam fOO'
and a dictionary:
substitutions = {"foo": "TEST", "bar": "BAR"}
in result I want to get:
'spam TEST bar TEST bar spam TEST'
i.e. all "foo" words get replaced regadless of capital or small letters.
For this purpose I found the next function:
def replace(string, substitutions):
regex = re.compile('|'.join(map(re.escape, substitutions)))
return regex.sub(lambda match: substitutions[match.group(0)], string)
it returns me:
'TEST spam fOo BAR TEST BAR spam fOO'
i.e. only exact match was replaced. If I put re.IGNORECASE as a flag for re.compile() - nothing changes.
Upvotes: 0
Views: 129
Reputation: 2946
Add re.IGNORECASE and try
... substitutions[match.group().lower()] ...
since subtitutions[match.group()] does not find relevant value when the match is say "fOo"
Upvotes: 1