Excellent, glad it was helpful.
It really ought to be possible to combine subexpression calls and back references to do things like matching well-formed XML elements. Unfortunately this doesn't work with the Onigurama engine, because subexpression calls mess up back referencing. That's a real shame: it does work in PCRE.
If you change \k<name> to \g<name> in the code below, then it will work (but then of course it doesn't check that the closing tag has the same name as the opening one).
Robin
(?x) # Enable layout and comments
(?<element> (?<stag> < (?<name> # An XML name begins with a letter, underscore or colon: [[:alpha:]_:] # followed by a sequence of NameChars (see the XML definition): [[:alnum:].-_:]* ) (?<attr> # In XML, the whitespace chars are space, tab, CR and LF. (?<space>[\ \t\r\n]+) \g<name>\g<space>?=\g<space>? (?<attrval> '[^']*' | "[^"]*" ) )* \g<space>? > ) (?<content> [^<] # This is not quite technically correct, but is probably good enough | \g<element> )* (?<etag> </ \k<name> \g<space>? > ) | (?<emptyelemtag> < \g<name> \g<attr>* \g<space>? /> ) )