C# Regex extract content of a div

c#, regex

Solution

Regex is not a good choice for parsing HTML files..

HTML is not strict nor is it regular with its format..

Use htmlagilitypack

Why use parser?

Consider your regex..There are infinite number of cases where you could break your code

- Your regex won't work if there are nested divs

- Some divs dont have an ending tag!(except XHTML)

You can use this code to retrieve it using `HtmlAgilityPack`

HtmlDocument doc = new HtmlDocument();
doc.Load(yourStream);

var itemList = doc.DocumentNode.SelectNodes("//div[@id='thumbs']")//this xpath selects all div with thubs id
                  .Select(p => p.InnerText)
                  .ToList();

//itemList now contain all the div tags content having its id as thumbs

Problem

I've seen some related questions of mine, and I tried them but they don't work. I want to match the content from a div with the id "thumbs". But the regex.Success returns false :( ``` Match regex = Regex.Match(html, @"<div[^>]*id=""thumbs"">(.+?)</div>"); ```

Original source