pythonregexpython-regex

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


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.


Solution

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