0

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.

Community
  • 1
  • 1
John
  • 339
  • 1
  • 3
  • 13
  • 1
    If you've got a regex question. Please include the language/tool you're using. – HamZa Mar 01 '14 at 20:07
  • Sorry. I'm testing regex on http://derekslager.com/blog/posts/2007/09/a-better-dotnet-regular-expression-tester.ashx. I program in Java – John Mar 01 '14 at 20:14
  • 1
    http://stackoverflow.com/a/1732454/784331 – Bugs Mar 01 '14 at 20:23
  • 1
    Now that I've taken a closer look at the input. I see some random `` popping out from here and there. I also see a redundant expression `[\d.\s\w&;]+`, `\w` includes `\d` so you could just remove `\d`. It will get complicated if you take the regex way especially if you're not very good at it. Shouldn't you consider a proper HTML parser ? – HamZa Mar 01 '14 at 20:24
  • @Bugs That might be a funny answer but [it's a *rant*](http://meta.stackexchange.com/q/182189). – HamZa Mar 01 '14 at 20:25

1 Answers1

1

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.

hd1
  • 33,938
  • 5
  • 80
  • 91