In perl s/[^\w:]//g
would replace all non alphanumeric characters EXCEPT :
In python I'm using re.sub(r'\W+', '',mystring)
which does remove all non alphanumeric except _ underscore.
Is there any way to put exceptions, I wish not to replace signs like = and .
Previously I was applying the other approach i.e. to replace all unwanted characters usingre.sub('[!@#\'\"
$()]', '',mystring`)
However, it is not possible for me to predict what all characters may come in mystring hence I wish to remove all non alphanumeric characters except a few.
Google didnt provide an appropriate answer. The closest search being python regex split any \W+ with some exceptions but this didnt help me either.