Skip to content

Instantly share code, notes, and snippets.

@Hammer2900
Created July 17, 2015 09:50
Show Gist options
  • Select an option

  • Save Hammer2900/e589e69a14628f83408a to your computer and use it in GitHub Desktop.

Select an option

Save Hammer2900/e589e69a14628f83408a to your computer and use it in GitHub Desktop.
mass replace dict func
def multiple_replace(dict, text):
# Create a regular expression from the dictionary keys
regex = re.compile("(%s)" % "|".join(map(re.escape, dict.keys())))
# For each match, look-up corresponding value in dictionary
return regex.sub(lambda mo: dict[mo.string[mo.start():mo.end()]], text)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment