Stan Zeez
Stan Zeez

Reputation: 1188

Remove unclosed brackets

How to remove unclosed brackets and its contents if after they have closed block. For example:

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

I do so, but I can not make a correct regex:

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

Please help!

Upvotes: 4

Views: 1139

Answers (1)

Serge Ballesta
Serge Ballesta

Reputation: 148870

Short answer : you cannot with Python regexes.

A very detailed explaination for that has already be given in this link given by DaoWen's comment

Medium answer : the standard re module cannot process recursive patterns, but there is a module in Pypi that claims being able to : regex 2015.03.18 : Recursive and repeated patterns are supported. - beware untested because when things go too complex for re I prefere to build a dedicated parser eventually through PLY which is a Python implementation of the good old lex+yacc.

Upvotes: 2

Related Questions