3

I have string:

acd (e(fg)h) ij)

I need to delete text within opened and coresponding closed bracket. So in example I need to delete

(e(fg)h)

In result I want to have

acd del ij)

I try to use next code:

re.sub(r'\((((?>[^()]+)|(?R))*)\)', r'del', 'acd (e(fg)h) ij)')

But python say:

sre_constants.error: unexpected end of pattern
Ivan Borshchov
  • 3,036
  • 5
  • 40
  • 62
  • 2
    Python does not support recursive regex. You might want to look into the [regex](https://pypi.python.org/pypi/regex) module to access recursive regex (and some other regex syntax currently not supported by the re module). – Jerry Apr 20 '14 at 14:59
  • 2
    [This](http://stackoverflow.com/a/12280660/2235132) might help. – devnull Apr 20 '14 at 15:00

1 Answers1

3

Thanks Jerry and devnull! regex module for python instead of default re module solved my issue

import regex
>>> regex.sub(r'\((((?>[^()]+)|(?R))*)\)', r'del', 'acd (e(fg)h) ij)')
'acd del ij)'
Ivan Borshchov
  • 3,036
  • 5
  • 40
  • 62