Python string manipulation
I have a line swith parentheses enclosed:s = "AX(p>q)&E((-p)Ur)"
I want to remove all characters between all pairs of brackets and save in a new line as follows: new_string = AX&E
I tried to do this:
p = re.compile("\(.*?\)", re.DOTALL)
new_string = p.sub("", s)
It outputs the result: AX&EUr)
Is there a way to fix this, and not iterate over each item in a string?
Another simple option is to remove the innermost parentheses at each stage until there are no more parentheses:
p = re.compile("\([^()]*\)")
count = 1
while count:
s, count = p.subn("", s)
Working example: http://ideone.com/WicDK