Replace Special Characters in String

Hi all,

I am building a bot in which I have to save some emails to a local folder. However, since there can be characters like “(”, “[”, “:”, “;”, and “\” in the mail subject, it is not getting saved.
I would like to use Regex to replace these special characters.
Can anyone provide me with a Regex expression so I can replace the special characters with “_” ??

Thanks!!

You can use the backslash character as the escape character for literal matching of special chars.
For refining your regular expression, I recommend using these two resources. These are my go-to for building any regex (testing and learning)

  1. https://regex101.com - realtime testing of regex and also gives info at the same time
  2. https://www.regular-expressions.info/ - Theory in detail
1 Like