0

Data:

<tr>
<td>
<a href="somelink">
some. .data...
</a>
</td>
<td>Black</td>
<td>57234</td>
<td>5431.60</td>
<td><font class="down">  -125.02</font></td>
</tr><tr>
<td>
<a href="somelink">
some. .data...
</a>
</td>
<td>Blue</td>
<td>57234</td>
<td>5431.60</td>
<td><font class="up">  -125.02</font></td>
</tr><tr>
<td>
<a href="somelink">
some. .data...
</a>
</td>
<td>Brown</td>
<td>57234</td>
<td>5431.60</td>
<td><font class="down">  -125.02</font></td>
</tr>
...more data...

I want to extract 'some. .data...'; 'Black'; '57234'; '5431.60'; at one time. [fifth td data is not required.]

Initially,

<tr><td><a.*>([a-zA-Z0-9 -]+)</a></td><td>(\w+)</td><td>([\d]+\.\d+)</td><td>(\d+\.\d+)</td>

was working. (via hit and miss approach)

But, now it's broke.

Now, when I use <td>(.*)</td> or <\w+>(.*)</\w+> : it shows data from last four tds in every tr. But then, Why won't it show <a href...>...</a> and how can I get data I want?

1
  • Why don't you load the XML in an XElement and use LINQ to parse whatever you want? Isn't it easier that way? Commented Jan 20, 2013 at 18:38

2 Answers 2

6

Regex is, in general, a bad way to parse HTML.

I suggest taking a look at the HTML Agility Pack or CsQuery that are purpose built HTML parsers for .NET.

The HTML Agility Pack can be queried using XPath and LINQ, and CsQuery uses jQuery selectors.

Sign up to request clarification or add additional context in comments.

3 Comments

Good answer. I'd just like to vouch for CsQuery, it is a lot more modern, a lot faster, and a lot nicer than HtmlAgilityPack.
What's broke? I do understand the futility of my 'parsing HTML with regex' but as a curious exercise, how can I do so?
@AnubhavSaini - The futility of parsing HTML with regex turns a lot of people off from even attempting it. So I'm not sure you're going to get an answer. The thing is that even if you get it to work, it's always going to be a fragile solution.
1

If you used a real html parser, your code would be simpler and easier to maintain

HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
doc.LoadHtml(html);

var table = doc.DocumentNode.Descendants("tr")
           .Select(tr => tr.Descendants("td").Select(td => td.InnerText).ToList())
           .ToList();

Given the sample html you provided, above code will return 3 rows each containing 5 columns.

1 Comment

didn't work. I think HtmlWeb hweb = new HtmlWeb(); doc= hweb.Load(url, "get"); is what does it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.