I have a string like this:
s = 'word1 word2 (word3 word4) word5 word6 (word7 word8) word9 word10'
how can I delete everything that is in brackets, so that the output is:
'word1 word2 word5 word6 word9 word10'
I tried regular expression but that doesn't seem to work. Any suggestions?
Best Jacques
import re
s = re.sub(r'\(.*?\)', '', s)
Note that this deletes everything between parentheses only. This means you'll be left with double space between "word2 and word5". Output from my terminal:
>>> re.sub(r'\(.*?\)', '', s)
'word1 word2 word5 word6 word9 word10'
>>> # -------^ -----------^ (Note double spaces there)
However, the output you have provided isn't so. To remove the extra-spaces, you can do something like this:
>>> re.sub(r'\(.*?\)\ *', '', s)
'word1 word2 word5 word6 word9 word10'
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