Skip to content

Instantly share code, notes, and snippets.

@xbalaji
Last active January 27, 2018 07:25
Show Gist options
  • Save xbalaji/da129694c8480fa69ac5fb60386dcb51 to your computer and use it in GitHub Desktop.
Save xbalaji/da129694c8480fa69ac5fb60386dcb51 to your computer and use it in GitHub Desktop.
python regular expression replace
import re
mystr="this is a complex string !@#$%^&*()-=+,.?:;'\\/\" with numbers 1234?"
# note: raw string used with double quotes in re.sub
out_str = re.sub(r"([ !@#$%^&*()\-=+,.?:;'/\\\"])", r'_', mystr)
print(mystr)
print(out_str)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment