-1

I need help with regualar expressions. (using c#)

In the html-sourcecode i got something like this.

[...]
<TR class=tblDataGreyNH>
<TD style="TEXT-ALIGN: right; FONT-WEIGHT: bold" class=tblHeader>Total Time </TD>
<TD>07:47 </TD>
<TD>04:48 </TD>
<TD>00:00 </TD>
<TD>00:00 </TD>
<TD>07:42 </TD>
<TD>00:00 </TD>
<TD>00:00 </TD></TR>
[..]
<TR class=tblDataGreyNH nowrap>
<TD>Total </TD>
<TD>20:17 </TD></TR>
<TR style="FONT-WEIGHT: bold" class=tblDataWhiteNH nowrap>
<TD>Total Time </TD>
<TD width=75>20:17 </TD></TR></TBODY></TABLE></TD>
<TD colSpan=3>
...

The classnames are always the same. I need all the TD's parsed into a stringarray. tblDataGreyNH is the importants class.

here is the whole table, where the td's are inside. (if some you need)

<table class="tblList">
<form action="/interface/timesheet/ViewUserTimeSheet.php" method="get" name="timesheet"></form>
<tbody>
<tr>
<tr class="tblHeader">
<tr class="tblHeader">
<tr class="tblDataWhiteNH">
<tr class="tblDataWhiteNH">
<tr class="tblHeader">
<tr class="tblDataGreyNH">
<td class="tblHeader" style="font-weight: bold; text-align: right"> Total Time </td>
<td> 07:47 </td>
<td> 04:48 </td>
<td> 00:00 </td>
<td> 00:00 </td>
<td> 07:42 </td>
<td> 00:00 </td>
<td> 00:00 </td>
</tr>
<tr class="tblDataWhiteNH">
<tr class="tblHeader">
<tr valign="top">
</tbody>
</table>

I hope there is someone who can help me with this problem. Regex seems impossible to understand for me. I can't grasp the basics with that ReGeX stuff!? :/

2 Answers2

3

Don't use Regex for HTML, I would suggest checking out the HtmlAgilityPack

Very simple:

var doc = new HtmlDocument();
doc.LoadHtml("...your sample html...");

// all <td> tags in the document
foreach (HtmlNode td in doc.DocumentNode.SelectNodes("//td")) 
{
     Console.WriteLine(td.InnerText);
}
eMi
  • 5,540
  • 10
  • 60
  • 109
0

You should not use regex to parse HTML (one of many refs: link)

There exists a great .NET library named HtmlAgilityPack that I would recommend.

Community
  • 1
  • 1
Anders Arpi
  • 8,277
  • 3
  • 33
  • 49