In the string
my_string = 'abcd (ef gh ) ij'
I need to remove the spaces only when they appear within parentheses, resulting in:
my_clean_string = 'abcd (efgh) ij'
This post suggests how to remove all parentheses text entirely via re.sub(r'\([^)]*\)', '', my_string)
, however I do not know how specify the removal should only be applied to whitespaces ' '
.
Is there a regexpr (or simple python) solution that does this without looping through each character expressly?