With this C# regex, you can easily match/Parse Nested HTML tags.
Example input:
<!DOCTYPE html>
<html>
<head>
<title>someTitle</title>
</head>
<body>
<div class="outer">
<h1>SomeHeader</h1>
<div id="target">
<h2>anotherHeader</h2>
<div id="child">
<div id="grand-child">
<h4>HelloWorld</h4>
</div>
</div>
</div>
</div>
</body>
</html>
Output:
<div id="target">
<h2>anotherHeader</h2>
<div id="child">
<div id="grand-child">
<h4>HelloWorld</h4>
</div>
</div>
</div>
string pattern = $@"{ startP }((?'nested'{ openP })|{ closeP }(?'-nested')|[\w\W]*?)*{ closeP }";
*'StartP' (Must include open tag), example: <div id="target" *'openP' example: <div *'closeP' example: </div
In Depth with RegEx Matching Nested Constructions In Depth with .NET RegEx Balanced Grouping Regular expression matches closed HTML tags (nesting is supported)