I have the following regex , which remove all no alpha numeric characters from a string text
re.sub(r'[^a-zA-Z0-9]',' ', text)
How can I modify this expression to include the characters '[' and ']' in the string text ?
Add [, ] to the charcter class ([ .. ]) with escaping.
re.sub(r'[^a-zA-Z0-9\[\]]',' ', text)
Example:
>>> re.sub(r'[^a-zA-Z0-9\[\]]', ' ', 'a,b[c-d]!')
'a b[c d] '
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