If your parentheses are balanced (with help of this answer):
import reinput_string = '''"Hello World (Don't want to strip this (also not this))" anything outside round brackets should remain as is(strip this (strip this also as it is outside double quotes) xxx) Also remain this (String this)'''def strip_parentheses(g): n = 1 # run at least once while n: g, n = re.subn(r'\([^()]*\)', '', g) # remove non-nested/flat balanced parts return gs = re.sub(r'".*?"|([^"]*)', lambda g: strip_parentheses(g.group(1)) if g.group(1) else g.group(), input_string)print(s)
Prints:
"Hello World (Don't want to strip this (also not this))" anything outside round brackets should remain as is Also remain this
EDIT Running some test-cases:
import reinput_string = '''"Hello World (Don't want to strip this (also not this))" anything outside round brackets should remain as is(strip this (strip this also as it is outside double quotes) xxx) Also remain this ((String this))'''test_cases = ['Normal string (strip this)','"Normal string (dont strip this)"','"Normal string (dont strip this)" but (strip this)','"Normal string (dont strip this)" but (strip this) and (strip this)','"Normal string (dont strip this)" but (strip this) and (strip this) but "dont strip (this)"','"Normal string (dont strip this)" but ((strip this) and this) and (strip (strip this))','"Normal string (dont strip this)" but ((strip this) but "remain this (xxx)") ',]def strip_parentheses(g): n = 1 # run at least once while n: g, n = re.subn(r'\([^()]*\)', '', g) # remove non-nested/flat balanced parts return gdef my_strip(s): return re.sub(r'".*?"|([^"]*)', lambda g: strip_parentheses(g.group(1)) if g.group(1) else g.group(), s)for test in test_cases: print(test) print(my_strip(test)) print()
Prints:
Normal string (strip this)Normal string "Normal string (dont strip this)""Normal string (dont strip this)""Normal string (dont strip this)" but (strip this)"Normal string (dont strip this)" but "Normal string (dont strip this)" but (strip this) and (strip this)"Normal string (dont strip this)" but and "Normal string (dont strip this)" but (strip this) and (strip this) but "dont strip (this)""Normal string (dont strip this)" but and but "dont strip (this)""Normal string (dont strip this)" but ((strip this) and this) and (strip (strip this))"Normal string (dont strip this)" but and "Normal string (dont strip this)" but ((strip this) but "remain this (xxx)") "Normal string (dont strip this)" but ( but "remain this (xxx)")
EDIT: To remove all ()
, even with quoted strings inside them:
import reinput_string = '''"Hello World (Don't want to strip this (also not this))" anything outside round brackets should remain as is(strip this (strip this also as it is outside double quotes) xxx) Also remain this ((String this))'''test_cases = ['"Normal string (dont strip this)" but (strip this) and (strip this) but "dont strip (this)"','"Normal string (dont strip this)" but ((strip this) and this) and (strip (strip this))','"Normal string (dont strip this)" but ((strip this) but "remain this (xxx)") ',]def strip_parentheses(g): n = 1 # run at least once while n: g, n = re.subn(r'\([^()]*\)', '', g) # remove non-nested/flat balanced parts return gdef my_strip(s): s = re.sub(r'".*?"|([^"]*)', lambda g: strip_parentheses(g.group(1)) if g.group(1) else g.group(), s) return re.sub(r'".*?"|(\(.*\))', lambda g: '' if g.group(1) else g.group(), s)for test in test_cases: print(test) print(my_strip(test)) print()
Prints:
"Normal string (dont strip this)" but (strip this) and (strip this) but "dont strip (this)""Normal string (dont strip this)" but and but "dont strip (this)""Normal string (dont strip this)" but ((strip this) and this) and (strip (strip this))"Normal string (dont strip this)" but and "Normal string (dont strip this)" but ((strip this) but "remain this (xxx)") "Normal string (dont strip this)" but