I've been struggling with this for a while
var matches = Regex.Matches("<h2>hello world</h2>",
@"<(?<tag>[^\s/>]+)(?<innerHtml>.*)(?<closeTag>[^\s>]+)>",
RegexOptions.IgnoreCase | RegexOptions.Compiled | RegexOptions.Multiline);
string tag = matches[0].Groups["tag"].Value; // "h2"
string innerHtml = matches[0].Groups["innerHtml"].Value; // ">hello world</h"
string closeTag = matches[0].Groups["closeTag"].Value; // "2"
As can be seen tag
works as expected while the innerHtml
and closeTag
does not. Any advice? Thanks.
Update
The input string may vary, this is another scenario
"<div class='myclass'><h2>hello world</h2></div>"