"e...">

Remove closed parentheses

How to remove unclosed brackets and their contents if after block closing. For example:

"(eaardf((eaar)(eaar" -> "eaardf((eaar)"

      

I do like this, but I can't seem to create the correct regex:

import re
str1 = '(eaardf((eaar)(eaar'
p = re.compile(r'\([a-z)]*.*')
p.sub('', str1)
>>> ''

      

Please, help!

+3


source to share


1 answer


Short answer: you can't with Python regexes.

A very detailed explanation for this has already been given in this link cited in DaoWen's comment



Medium answer: The standard re

module cannot handle recursive patterns, but there is a module in Pypi that claims it can: regex 2015.03.18 : Recursive and repeated patterns are supported. - beware, because when something is too complicated for re, I prefer to create a dedicated parser after all via PLY , which is a Python implementation of the good old lex + yacc.

+1


source







All Articles