Skip to content Skip to sidebar Skip to footer

Python String Manipulation

I have a string s with nested brackets: s = 'AX(p>q)&E((-p)Ur)' I want to remove all characters between all pairs of brackets and store in a new string like this: new_string

Solution 1:

Another simple option is removing the innermost parentheses at every stage, until there are no more parentheses:

p = re.compile("\([^()]*\)")
count =1while count:
    s, count = p.subn("", s)

Working example: http://ideone.com/WicDK

Solution 2:

You can just use string manipulation without regular expression

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

I leave it to you to join the strings up.

Solution 3:

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

Solution 4:

Yeah, it should be:

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

Solution 5:

Nested brackets (or tags, ...) are something that are not possible to handle in a general way using regex. See http://www.amazon.de/Mastering-Regular-Expressions-Jeffrey-Friedl/dp/0596528124/ref=sr_1_1?ie=UTF8&s=gateway&qid=1304230523&sr=8-1-spell for details why. You would need a real parser.

It's possible to construct a regex which can handle two levels of nesting, but they are already ugly, three levels will already be quite long. And you don't want to think about four levels. ;-)

Post a Comment for "Python String Manipulation"