1

As the title reads, we can easily match nested parentheses in regex with e.g.

(\(((?:[^()]+|(?1))+))

which will match balanced parentheses.
How can we use a named subgroup instead, as e.g. in

(?P<key>\(((?:[^()]+|(?\g<key>))+))

I'm not looking for a parser solution or anything but really for the pattern above in Python (regex module) or PCRE.

4
  • 1
    Use (?&key).... Commented Sep 15, 2020 at 12:46
  • note that the python builtin module is called re. the regex package on PyPI is a third-party package with the most prominent feature being that it can fuzzy match. Commented Feb 7 at 11:28
  • @ChristophRackwitz: No offense, but this comment is really rather basic knowledge. Also, the PyPi regex module has a lot more features including recursion and yes fuzzy match. Commented Feb 7 at 15:55
  • you'd think it's basic and I would think that, but plenty of people mix the tags up, even high-rep users who should know better, because that PyPI package claims a common name for itself. check the tag usage info for python-regex. go ahead, open a debate about this on meta. this question appropriately uses the tag. plenty of others use it against the tag guideline, or only have it because an answer happens to suggest using the third party library. Commented Feb 7 at 16:39

1 Answer 1

1

According to the PyPi regex documentation, the named backreference syntax is

(?&NAME)

See a Python demo:

import regex
print ( regex.sub(r'(?P<key>\((?:[^()]++|(?&key))+\))', '', '(ab(c)d) a(b()') )
# =>  a(b()
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.