Replacing Multiple Patterns in a Single Pass
Credit: Xavier Defrang
Problem
You need to perform several string substitutions on a string.
Solution
Sometimes regular expressions afford the fastest solution even in
cases where their applicability is anything but obvious. In
particular, the sub method of re objects
makes regular expressions a good way to perform string substitutions.
Here is how you can produce a result string from an input string
where each occurrence of any key in a given dictionary is replaced by
the corresponding value in the dictionary:
# requires Python 2.1 or later
from _ _future_ _ import nested_scopes
import re
# the simplest, lambda-based implementation
def multiple_replace(adict, text):
# Create a regular expression from all of the dictionary keys
regex = re.compile("|".join(map(re.escape, adict.keys( ))))
# For each match, look up the corresponding value in the dictionary
return regex.sub(lambda match: adict[match.group(0)], text)A more powerful and flexible
approach is to wrap the dictionary into a callable object that
directly supports the lookup and replacement idea, which you can use
directly as the callback in the sub method. This
object-oriented approach is more flexible because the callable object
can keep its own state and therefore is easily extensible to other
tasks. In Python 2.2 and later, you can create a class for this
object by extending the dict built-in type, while
in older Python versions you must fall back on
UserDict.UserDict (built-in ...