0

what will be the regular expression for a table with class name tn_result?

I have tried two expressions given below but neither is working. What is wrong with them? How can I make this work?

 string TableExpression = "<TTable class=\"tn_results\">(.*?)</table>";
Gilles 'SO- stop being evil'
  • 104,111
  • 38
  • 209
  • 254
  • 1
    One happy day, no-one will ask any more questions about how to parse HTML with regular expressions... – Graham Clark Apr 05 '12 at 11:57
  • Stackoverflow should really redirect users asking questions containing "regular expression", "html", "table", "class"... directly to http://htmlagilitypack.codeplex.com/ – Goran Apr 05 '12 at 12:21

2 Answers2

1

See this answer on why this is a bad idea.

The answer depends on whether you need to get this table server-side or client-side.

On the client, use jQuery:

var table = $('table.tn_result');

On the client, if you're using ASP.Net WebForms, it will probably be easiest to add runat="server" and id attributes on the table(s) you want to get. Then you can just get them in the code-behind like any other control:

var table = this.TableId; //not really required, but hopefully makes things clear
if(table.Attributes["class"].Contains("tn_result"))
{
    // do something with the table
}
Community
  • 1
  • 1
Graham Clark
  • 12,886
  • 8
  • 50
  • 82
  • this is not what i am looking for. I need regexpression because i am writting some application for data scrapping. – user1282751 Apr 05 '12 at 12:11
  • Regular Expressions will not work with HTML. You do not actually need a regular expression, you need an HTML parser. – Graham Clark Apr 05 '12 at 12:15
1

For scraping HTML use the Html Agility Pack. It's very flexible and lets you manipulate poorly formed HTML as if it were well-formed XML, so you can use XPATH or just iterate over nodes.

wp78de
  • 18,207
  • 7
  • 43
  • 71
Goran
  • 6,798
  • 9
  • 41
  • 57