OK. Maybe you remember Thomas Aylott and I fiddled around to implement a command which is able to select/find balanced HTML/XML tags. Finally we found a solution but it makes usage of many many lines of source code (the command should be in the TM trunk experimental).
Some while ago I read Oniguruma's RE.txt carefully. This kind of match is supported natively by Oniguruma. It is called 'back reference with nest level'.
Example 1:
I have a string: "<foo>f<foo>b<bar>123<bar>456</bar></bar>bb</foo>f</ foo>" and this regexp (please don't be frightened ;):
(?<element>\g<stag>\g<content>*\g<etag>){0}(?<stag><\g<name>\s*>){0}(? <name>[a-zA-Z_:]+){0}(?<content>[^<&]+(\g<element>|[^<&]+)*){0}(? <etag></\k<name+1>>){0}\g<element>
Stop. I've just tried out the regexp with TM and IT ALREADY WORKS ;-O [I could swear that I tried it out some while ago]
I believe I did an error in reasoning. This has nothing to do with the replacement format string. All these nested stuff is only relevant within the regexp.
--Hans