-2

I have an HTML string from which I want to strip a specific div and all of its content based on some condition my div has a specific class and it contains some other nested divs in there. I have tried to use regex but the problem is that it picks up the first </div> and ignores the following content.

string html = "<div class=\"card-set--six manual-cardset\"><div>test string</div></div>";
html = new Regex("<div class=\"card-set--six manual-cardset\">.*?</div>").Replace(html, string.Empty);

In my case, it should remove all the content and I should get an empty string. But this regex is returning me </div>.

Lauren Rutledge
  • 1,195
  • 5
  • 18
  • 27
Aamir
  • 1,832
  • 14
  • 14
  • 4
    Dont use regex, use a library that is meant for this like [HtmlAgilityPack](http://html-agility-pack.net/) – maccettura Aug 10 '18 at 19:24
  • [Using RegEx](https://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags/1732454#1732454)? – Sani Huttunen Aug 10 '18 at 19:27
  • @maccettura I definitely had that in mind but the problem is this is actually used in a 3rd party .NET based CMS and using 3rd party extensions and dll`s in there is not very straightforward so I went on to try some Regex but as @SaniSinghHuttunen suggested Regex might not do the work here. – Aamir Aug 10 '18 at 19:33
  • do u want to replace the entire div by nothing? or only "test string" ? – Alan Deep Aug 10 '18 at 19:35
  • @Aamir I guess I am not understanding why you cannot use HtmlAgilityPack. I use it often in CMS development work. You are doing the work above in C# no? So why is there any restriction on what packages you can use from Nuget? HtmlAgilityPack is free... – maccettura Aug 10 '18 at 19:36
  • @AlanDeep the Idea is to replace the whole div with class "card-set--six manual-cardset" with nothing. – Aamir Aug 10 '18 at 19:37
  • try to remove the question mark, do not believe the downvoters – Alan Deep Aug 10 '18 at 19:43
  • @AlanDeep Thanks, that actually fixes the problem. – Aamir Aug 10 '18 at 19:54
  • I'm glad bro! @Aamir – Alan Deep Aug 10 '18 at 19:54

1 Answers1

-2

Remove the question mark after .*?

Alan Deep
  • 2,037
  • 1
  • 14
  • 22