Is it possible to create a reqex that finds characters that are NOT is a specific set?
Rather than Blacklisting a bunch of characters and replacing them, it would be easier for me to allow a certain set and replace characters that are not in that set.
My set looks like this: [.a-zA-Z0-9]
I would like to do something like this:
clean_filename = re.sub(r'([.a-zA-Z0-9])', "_", filename)
obviously this code would replace the characters I want to keep, is there a way to replace the characters NOT in that set?
Yes, use the ^
negation "modifier": r'[^.a-zA-Z0-9]'
clean_filename = re.sub(r'[^.a-zA-Z0-9]', "_", filename)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With