Skip to content

Instantly share code, notes, and snippets.

@ahmed4end
Created July 5, 2020 20:09
Show Gist options
  • Save ahmed4end/2ae4d789ad4a9b5606e59ea3f4d96f35 to your computer and use it in GitHub Desktop.
Save ahmed4end/2ae4d789ad4a9b5606e59ea3f4d96f35 to your computer and use it in GitHub Desktop.
how to replace multiple characters and words in a string using regex.
#python - how to replace multiple characters and words in a string using regex.
import re
text = "hello ,world!"
replaces = {"hello": "hi", "world":" 2020", "!":"."}
regex = re.sub("|".join(replaces.keys()), lambda match: replaces[match.string[match.start():match.end()]], text)
print(regex)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment