python string manipulation

后端 未结 8 2190
生来不讨喜
生来不讨喜 2021-01-18 13:29

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

相关标签:
8条回答
  • 2021-01-18 14:22

    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.

    0 讨论(0)
  • 2021-01-18 14:32

    Another simple option is removing the innermost parentheses at every 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

    0 讨论(0)
提交回复
热议问题