in reply to Stack overflow while performing nested substitutions

And lastly, using regexes — or, indeed, any form of direct textual munging — is considered very bad style, when dealing with XML (or XHTML). It looks to me like you're trying to perform a simple transformation which would be easily within the power of XSLT... but if you really want to code it from scratch, at least let one of the fine XML parsers do the hard part for you.