0

I am trying to extract the Item that contain String "Active" from the following HTML source code. Please be noted the Active item may go first.

Here is the REGEX I am using(Trying to be non-greedy):

<TR class=\\w*?Item>.*?Active.*?</TD></TR>

with Pattern.CASE_INSENSITIVE| Pattern.DOTALL | Pattern.MULTILINE

It extract the whole source code instead of the second one: ... .

HTML Source code:

<TR class=Item>
<TD style="WIDTH: 100px"><A id=ctl00_BodyContents_gvServers_ctl02_HyperLink1 onclick=javascript:turnColor(this); href="AddEditVirtualServer.aspx?ServerId=16733" target=_blank>server01</A> </TD>
<TD></TD>
<TD style="WIDTH: 100px"><A id=ctl00_BodyContents_gvServers_ctl02_HyperLink2 onclick=javascript:turnColor(this); href="AddEditVirtualServer.aspx?ServerId=16733" target=_blank>07D8F15</A> </TD>
<TD style="WIDTH: 150px">IBM 8204-E8A</TD>
<TD style="WIDTH: 150px"><SPAN>AIX - 5.3.0.0 - ML 12</SPAN></TD>
<TD style="WIDTH: 100px">PowerPC_POWER6</TD>
<TD style="WIDTH: 75px">1</TD>
<TD style="WIDTH: 100px">C1-D-G13</TD>
<TD style="WIDTH: 100px"><SPAN id=ctl00_BodyContents_gvServers_ctl02_lbLevel1SupportGroup>UNIX TEAM 1&amp;CORP</SPAN> </TD>
<TD style="WIDTH: 100px"><SPAN id=ctl00_BodyContents_gvServers_ctl02_lbLevel2SupportGroup>UNIX TEAM 1&amp;CORP</SPAN> </TD>
<TD style="WIDTH: 100px"><SPAN id=ctl00_BodyContents_gvServers_ctl02_lbLevel3SupportGroup>UNIX TEAM 1&amp;CORP</SPAN> </TD>
<TD style="WIDTH: 100px">2011-04-15</TD>
<TD style="WIDTH: 100px">Cool Down </TD>
<TD style="WIDTH: 100px"><A id=ctl00_BodyContents_gvServers_ctl02_btnDeleteServer disabled>Delete</A> </TD></TR>
<TR class=AlternateItem>
<TD style="WIDTH: 100px"><A id=ctl00_BodyContents_gvServers_ctl03_HyperLink1 onclick=javascript:turnColor(this); href="AddEditVirtualServer.aspx?ServerId=19631" target=_blank>server01</A> </TD>
<TD></TD>
<TD style="WIDTH: 100px"><A id=ctl00_BodyContents_gvServers_ctl03_HyperLink2 onclick=javascript:turnColor(this); href="AddEditVirtualServer.aspx?ServerId=19631" target=_blank>105ABCD</A> </TD>
<TD style="WIDTH: 150px">IBM Power 770</TD>
<TD style="WIDTH: 150px"><SPAN>AIX - 5.3.0.0 - TL 12 SP 01</SPAN></TD>
<TD style="WIDTH: 100px">PowerPC_POWER7</TD>
<TD style="WIDTH: 75px">1</TD>
<TD style="WIDTH: 100px">C1-O-G11</TD>
<TD style="WIDTH: 100px"><SPAN id=ctl00_BodyContents_gvServers_ctl03_lbLevel1SupportGroup>UNIX TEAM 1&amp;CORP</SPAN> </TD>
<TD style="WIDTH: 100px"><SPAN id=ctl00_BodyContents_gvServers_ctl03_lbLevel2SupportGroup>UNIX TEAM 1&amp;CORP</SPAN> </TD>
<TD style="WIDTH: 100px"><SPAN id=ctl00_BodyContents_gvServers_ctl03_lbLevel3SupportGroup>UNIX TEAM 1&amp;CORP</SPAN> </TD>
<TD style="WIDTH: 100px">2012-02-09</TD>
<TD style="WIDTH: 100px">Active </TD>
<TD style="WIDTH: 100px"><A id=ctl00_BodyContents_gvServers_ctl03_btnDeleteServer disabled>Delete</A> </TD></TR>

Your help will be appreciated!

  • 1
    i see that you are doing something terrible. – mkoryak Feb 14 '12 at 05:08
  • 2
    Trying to parse XML/HTML with regex is a fast route to insanity. Back away from the keyboard... nice and slow, put down that asterisk gently so it doesn't go off... Then read this http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags/1732454#1732454 – Jim Garrison Feb 14 '12 at 05:21
  • Thanks for your suggestion, I've downloaded jsoup and get it working! It's way better than using regex with HTML! – Jason Liang Feb 20 '12 at 17:34

2 Answers2

1

*? is zero or more times. You probably want <TR class=\\w+?Item>

MByD
  • 135,866
  • 28
  • 264
  • 277
1

You can enable multi-line mode by Pattern p = Pattern.compile("....", Pattern.MULTILINE); And you probably have to change </TD></TR> to </TD>.*?</TR> in your pattern.

Bob Wang
  • 606
  • 4
  • 8