q)&E((-p)Ur)" I want to remove all characters between all pairs...">

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?

+5
source share
7 answers

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

+5
source

You can just use string manipulations without regular expression

>>> s = "AX(p>q)&E(qUr)"
>>> [ i.split("(")[0] for i in s.split(")") ]
['AX', '&E', '']

I leave this to you to join the lines up.

+4
>>> import re
>>> s = "AX(p>q)&E(qUr)"
>>> re.compile("""\([^\)]*\)""").sub('', s)
'AX&E'
+3

, :

>>> import re
>>> s = "AX(p>q)&E(qUr)"
>>> p = re.compile("\(.*?\)", re.DOTALL)
>>> new_string = p.sub("", s)
>>> new_string
'AX&E'
+2

PyParsing :

from pyparsing import nestedExpr
import sys

s = "AX(p>q)&E((-p)Ur)"
expr = nestedExpr('(', ')')
result = expr.parseString('(' + s + ')').asList()[0]
s = ''.join(filter(lambda x: isinstance(x, str), result))
print(s)

: regexp python?

+2

re.subn():

import re

s = 'AX(p>q)&E((-p)Ur)'
while True:
    s, n = re.subn(r'\([^)(]*\)', '', s)
    if n == 0:
        break
print(s)

Output

AX&E
0
source

All Articles