python string manipulation

后端 未结 8 2193
生来不讨喜
生来不讨喜 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:05

    You can use PyParsing to parse the string:

    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)
    

    Most code is from: How can a recursive regexp be implemented in python?

提交回复
热议问题