Created
July 5, 2020 20:09
-
-
Save ahmed4end/2ae4d789ad4a9b5606e59ea3f4d96f35 to your computer and use it in GitHub Desktop.
how to replace multiple characters and words in a string using regex.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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