-1

I have been trying to extract a single value from some HTML data, but I haven't been able to focus on exactly the piece I am looking for:

The line I am trying to read from is:

<span class="temp">5</span>

There are many other "span" tags with different classes, so I need to only accept "temp" class, and then get the value?

I really have no experience with Regex, but have been trying to figure it out. Here are just some of the lines I have tried:

//MatchCollection data = Regex.Matches(searchData, @"<span class=""temp"">(.+?)<", RegexOptions.None);

//MatchCollection data = Regex.Matches(searchData, @"<span class=""temp"">\s*(.+?)\s*</span>", RegexOptions.Singleline);

//MatchCollection data = Regex.Matches(searchData, @"<span class=""temp""> (.*?) </span> ", RegexOptions.Singleline);

//MatchCollection data = Regex.Matches(searchData, @"<span class=""temp"">(([^<]|<[^\/]|<\/[^s]|<\/s[^p]|<\/sp[^a]|<\/spa[^n]|<\/span[^ \t >])*)</span>", RegexOptions.Singleline);

//MatchCollection data = Regex.Matches(searchData, @"<span class=""temp"">\s*([^<])\s*</span>", RegexOptions.Singleline);

//MatchCollection data = Regex.Matches(searchData, @"<span class=""temp"">\s*(<.*?>) * ([^<] *)\s*</span>", RegexOptions.Singleline);

//MatchCollection data = Regex.Matches(searchData, @"(?<=<span\s *class=""temp""[^><]*>\s*)[^<>]*", RegexOptions.Singleline);

//MatchCollection data = Regex.Matches(searchData, @"(?<=<span\s*class=""temp""[^><]*>\s*)[^<>]*", RegexOptions.Singleline);

//MatchCollection data = Regex.Matches(searchData, @"(?<=<span\s*class=""temp"">\s*)*", RegexOptions.Singleline);

//MatchCollection data = Regex.Matches(searchData, @"<span[^>] *> (.*?) </ span>", RegexOptions.Singleline);

//MatchCollection data = Regex.Matches(searchData, @"<span class=""temp""></span>", RegexOptions.Singleline);

//MatchCollection data = Regex.Matches(searchData, @"<span class=""temp""></span>", RegexOptions.Singleline); 

Thank you for any assistance you can provide! Doug

Doug
  • 13
  • 4

1 Answers1

0

You can try to find all elements you need using HtmlAgilityPack with XPATH expression:

var htmlDoc = new HtmlDocument();
htmlDoc.LoadHtml(html);
    
// select all <span> elements with class 'temp' containing text and no element children
var xpath = "//span[contains(@class, 'temp') and text() and not(*)]";
var nodes = htmlDoc.DocumentNode.SelectNodes(xpath);

foreach (var node in nodes)
{
    Console.WriteLine(node.InnerText);
}
Ruslan Gilmutdinov
  • 1,217
  • 2
  • 9
  • 20