5

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
2
  • 2
    Python does not support recursive regex. You might want to look into the regex module to access recursive regex (and some other regex syntax currently not supported by the re module). Commented Apr 20, 2014 at 14:59
  • 2
    This might help. Commented Apr 20, 2014 at 15:00

1 Answer 1

4

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)'
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.