Using regex
instead of re
, you could go with
"[^"]+"(*SKIP)(*FAIL) # ignore anything between double quotes| # or\( (?:[^()]*|(?R))+ # match nested parentheses\)
In
Python
this could beimport regex as redata = """"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))"""rx = re.compile(r'''"[^"]+"(*SKIP)(*FAIL) | \( (?:[^()]*|(?R))+ \)''', re.VERBOSE)data = rx.sub("", data)print(data)
Yielding
"Hello World (Don't want to strip this (also not this))" anything outside round brackets should remain as is