1

I have html string like this (yahoo xml description element)

<img src="http://l.yimg.com/a/i/us/we/52/26.gif"/><br /> 
<b>Current Conditions:</b><br /> Cloudy, 1 C<BR /> <BR />
<b>Forecast:</b><BR /> Mon - Snow. High: -5 Low: -14<br /> Tue - Light Snow. High: -8 Low: -16<br /> <br /> 
....

I want to get only High and Low values (for above example: -5, -14, -8, -16)

I try to get with htmlAgilityPack like this:

HtmlDocument htmlDoc = new HtmlDocument();
htmlDoc.LoadHtml(rssDescriptionElement);
List<string> elements = new List<string>();

foreach (HtmlNode element in htmlDoc.DocumentNode.SelectNodes("//br"))
{
    elements.Add(element.NextSibling.InnerText);
}

elements list output for above htmlString:

"\n"
"\nCloudy, 1 C"
"\n"
"Forecast:"
"\nMon - Snow. High: -5 Low: -14"
"\nTue - Light Snow. High: -8 Low: -16"
"\n"
"\n"
""
"\n(provided by "
"\n"

How Can I get only high and low values (-5, -14, -8,-16) from this list Or another different solution?

AliRıza Adıyahşi
  • 15,658
  • 24
  • 115
  • 197

1 Answers1

1

Use Regex:

(?:High|Low)\s*:\s*(?<num>-?\d+)

and get group named num. sample code:

List<string> elements = new List<string>();
var pattern = @"(?:High|Low)\s*:\s*(?<num>-?\d+)";

foreach (HtmlNode element in htmlDoc.DocumentNode.SelectNodes("//br"))
{
    foreach(Match mc in Regex.Matches(element.NextSibling.InnerText, pattern))
    {
        elements.Add(mc.Groups["num"].ToString());
    }
}
Ria
  • 10,237
  • 3
  • 33
  • 60