How to refer to a named capturing group in the Python PyPi regex pattern

85 Views Asked by At

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.

1

There are 1 best solutions below

0
On BEST ANSWER

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()