﻿# This will match a balanced markup tag.
\<                          #  The tag Start
(?<TagName>\w+)             #  The name of the Tag is any non number of non-word characters
(?<TagAttributes>           #  It's attributes are 
    .+?                     #  anything until
    (?=                     #  the close of the tag. 
        (?>                 #  The close of the tag can be either (IsClosed)/> or (IsOpen)> 
            (?<IsClosed>/>)|(?<IsOpen>>)
        )
    )
)
(?(IsOpen)(?:                # If the tag was open
        >                    # match the end tag (so that it's not captured as TagContent)
        (?<TagContent>       # then capture the tag content            
                (?>
                    (.|\s)+?(?!<\k<TagName>)|
                    (?=\<\k<TagName>)(?<TagDepth>)|
                    (?=\<\/\k<TagName>)(?<-TagDepth>)                    
                )*(?(TagDepth)(?!))   
                              # It is anything until 
        )(?=\<\/\k<TagName>)  # the closing tag
        (?:\<\/\k<TagName>>)
    )
)