John
John

Reputation: 329

HTML Regular Expression

I have a problem with my regular expression. My data look like this:

<tr class="abcData"><td>Text Number One</td><td>text</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td></tr>
<tr class="abcData"><td>Text Number Two</td><td>text</td><td><span class="value">3</span></td><td><span class="value">3</span></td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td></tr>
<tr class="abcData"><td>Text Number Three</td><td>text</td><td><span class="value">3.5</span></td><td><span class="value">2</span></td><td><span class="value">3.5</span></td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td></tr>

As you can see 3rd-8th cell can contain number (integer/double), &nbsp or number surrounded by span.

<tr class="abcData"><td>([\s\w]+)</td><td>([\s\w.-]+)</td><td>([\d.\s\w&;]+)</td><td>(\3)</td><td>(\3)</td><td>(\3)</td><td>(\3)</td><td>(\3)</td></tr>

My regex only works for first example (text number one) and I don't know how to write OR statement for this (I need result in the same 8 groups).

regex result

I'm testing my regex on http://derekslager.com/blog/posts/2007/09/a-better-dotnet-regular-expression-tester.ashx.

Upvotes: 0

Views: 82

Answers (1)

hd1
hd1

Reputation: 34677

Do not use regex for HTML, use JSoup or a specialized parser instead. I'd use perl for this:

$tree->look_down('_tag', 'td', sub { push @text, $_->[0]->as_text; });

... or something like that.

Upvotes: 1

Related Questions