C#:Regex How to Match specific div close tag but the last close tag?

MapleStory

For examle:

<div id="outer">
    <div id="a">
        <div class="b"> 11111111111</div>
        <div class="b"> 22222222222222</div>
    </div>
</div>

Now I want to match the elements of id is a, and replace it to empty, but I found I can't, because id="a" is not the outer div. This is my c# code ,it will match the last Tag.

Regex regex = new Regex(@"<div id=""a([\s\S]*) (<\/[div]>+)");
Enigmativity

Try this:

var doc = new HtmlAgilityPack.HtmlDocument();
doc.LoadHtml(html);

var divs = doc.DocumentNode.Descendants().Where(x => x.Name == "div" && x.Id == "a");

foreach (var div in divs.ToArray())
{
    div.InnerHtml = "";
}

var result = doc.DocumentNode.OuterHtml;

The result I get is:

<div id="outer">
    <div id="a"></div>
</div>

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related