4

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!

Maroun
  • 94,125
  • 30
  • 188
  • 241
Stan Zeez
  • 1,138
  • 2
  • 16
  • 38
  • The output you're expecting is not right (if I understand you correctly), "eaardf((eaar)" still have unclosed parenthesis. – Maroun Apr 19 '15 at 15:00
  • Need "eaardf((eaar)" from "(eaardf((eaar)(eaar". – Stan Zeez Apr 19 '15 at 15:02
  • 1
    Why? What's the logic? The first "(" is not closed. Can you be more specific regarding the rules? – Maroun Apr 19 '15 at 15:02
  • possible duplicate of [Can regular expressions be used to match nested patterns?](http://stackoverflow.com/questions/133601/can-regular-expressions-be-used-to-match-nested-patterns) (Hint—the answer is no.) – DaoWen Apr 19 '15 at 15:03
  • 1
    This can be implemented with "stack" IMO. Will try to solve it with stack. – pnv Apr 19 '15 at 15:20
  • MarounMaroun, I can not explain this logic. Just need a result( – Stan Zeez Apr 19 '15 at 15:20
  • Most modern pattern matching libraries allow for matching nested data directly. – tchrist Apr 19 '15 at 18:19
  • You can try this pattern: `r'\(([a-zA-Z\(]*\))'`, but it will remove the first '(' every time. I confused that why the first '(' is removed in your example? If you don't want to remove the first '(', you can try this pattern instead: `r'(\([a-zA-Z\(]*\))'`. – Huan-Yu Tseng Apr 20 '15 at 06:55

1 Answers1

2

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.

Community
  • 1
  • 1
Serge Ballesta
  • 143,923
  • 11
  • 122
  • 252