Kamil Kamil - 9 months ago 131
C# Question

Regex expression C# for HTML

I have following regex:

^(<span style=.*?font-weight:bold.*?>.*?</span>)


It matches the following code:

<span style="font-family:Arial; font-size:10pt"> r.</span></p><p style="margin:0pt"><span style="font-family:Arial; font-size:10pt; font-weight:bold">&#xa0;</span>


But I would like to match only this part (last span containing font-weight:bold style)

<span style="font-family:Arial; font-size:10pt; font-weight:bold">&#xa0;</span>

Answer Source

Use HTML Agility Pack to parse html:

HtmlDocument doc = new HtmlDocument();
doc.LoadHtml(htmlContent);

var boldSpans = from s in doc.DocumentNode.SelectNodes("//span")
                let style = s.Attributes["style"].Value
                where style.Contains("font-weight:bold")
                select s;

Or even better xpath, which selects all nodes in one line:

doc.DocumentNode.SelectNodes("//span[contains(@style, 'font-weight:bold')]")
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download