3

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 ?

1 Answer 1

3

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] '
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.