Question

I have a html page with source code as below. My requirement is to search for given word only in column with heading "Ship Name". I would like to know which Regex would help me to only extract this particular column's data.

I have used following two Regex to extract table headers and rows:

table headers = @"<th[^>]*?>(?<Value>.*?)</th>" by this regex I can find the column Ship Name

table rows = @"<td>(.*?)</td>" by this regex I can get every column of every row

but my need is just to find 2nd column of every row. Please help if there exists any such regex with which i can do this or any other method by which i can get my job done. Please Help me

Html Page source code is:

<link href="http://www.mhpa.co.uk/reports.css" rel="STYLESHEET" type="text/css" CHARSET="ISO-8859-1">

<table cellpadding=2 cellspacing=0 border=0 width=100%>
<tbody>
<tr>
<td align=right><b>11/09/2013&nbsp;at&nbsp;09:48</b></td>
</tr>
</tbody>
</table>
<center>
<table border="1" bordercolor="silver" cellpadding="2" cellspacing="0" width="100%">
<thead>
<tr>
<th width=100>ETA</th>
<th width=100>Ship Name</th>
<th width=80>From port</th>
<th width=80>To berth</th>
<th width=130>Agent</th>
</tr>
</thead>
<tbody>

<tr><td>11/09/2013 at 09:00&nbsp;</td>
<td>SONANGOL KALANDULA&nbsp;</td>
<td>Cabinda&nbsp;</td>
<td>Valero 6&nbsp;</td>
<td>Graypen&nbsp;</td>
</tr>

<tr>
<td>11/09/2013 at 11:45&nbsp;</td>
<td>ISLE OF INISHMORE&nbsp;</td>
<td>Rosslare&nbsp;</td>
<td>PDFT&nbsp;</td>
<td>Irish Ferries&nbsp;</td>
</tr>

<tr>
<td>11/09/2013 at 12:00&nbsp;</td>
<td>BRO DEVELOPER&nbsp;</td>
<td>Avonmouth&nbsp;</td>
<td>Valero 8&nbsp;</td>
<td>GAC&nbsp;</td>
</tr>

<tr>
<td>11/09/2013 at 14:00&nbsp;</td>
<td>WELSH PIPER&nbsp;</td>
<td>Sea&nbsp;</td>
<td>POP No 1&nbsp;</td>
<td>Tarmac Marine Dredging Ltd&nbsp;</td>
</tr>

<tr>
<td>11/09/2013 at 22:00&nbsp;</td>
<td>PATRICIA&nbsp;</td>
<td>Le Havre&nbsp;</td>
<td>Murco 1&nbsp;</td>
<td>GAC&nbsp;</td>
</tr>

<tr>
<td>12/09/2013 at 05:00&nbsp;</td>
<td>BRO DESIGNER&nbsp;</td>
<td>Plymouth&nbsp;</td>
<td>Murco 2&nbsp;</td>
<td>Graypen&nbsp;</td>
</tr>

<tr>
<td>12/09/2013 at 06:00&nbsp;</td>
<td>BASTOGNE&nbsp;</td>
<td>Mongstad&nbsp;</td>
<td>Valero 3&nbsp;</td>
<td>Denholm Wilhelmsen Ltd&nbsp;</td>
</tr>

<tr>
<td>12/09/2013 at 06:00&nbsp;</td>
<td>MINERVA GRACE&nbsp;</td>
<td>Ras Lanuf&nbsp;</td>
<td>Valero 1&nbsp;</td>
<td>Graypen&nbsp;</td>
</tr>

<tr>
<td>12/09/2013 at 06:00&nbsp;</td>
<td>LEANDER&nbsp;</td>
<td>Dublin &nbsp;</td>
<td>Murco 2&nbsp;</td>
<td>Graypen&nbsp;</td>
</tr>

<tr>
<td>12/09/2013 at 07:00&nbsp;</td>
<td>THUN GLOBE&nbsp;</td>
<td>Cardiff&nbsp;</td>
<td>Valero 7&nbsp;</td>
<td>GAC&nbsp;</td>
</tr>

<tr>
<td>13/09/2013 at 06:00&nbsp;</td>
<td>THUN GENIUS&nbsp;</td>
<td>Dublin &nbsp;</td>
<td>Valero&nbsp;</td>
<td>GAC&nbsp;</td>
</tr>

<tr>
<td>13/09/2013 at 11:00&nbsp;</td>
<td>CAP LEON&nbsp;</td>
<td>Arzew&nbsp;</td>
<td>Valero 6&nbsp;</td>
<td>Graypen&nbsp;</td>
</tr>

<tr>
<td>13/09/2013 at 19:00&nbsp;</td>
<td>STOLT TEAL&nbsp;</td>
<td>TBA&nbsp;</td>
<td>Valero 1&nbsp;</td>
<td>Cory Bros&nbsp;</td>
</tr>

<tr>
<td>14/09/2013 at 16:00&nbsp;</td>
<td>B GAS LANRICK&nbsp;</td>
<td>Antwerp&nbsp;</td>
<td>Murco 3&nbsp;</td>
<td>S5 Agency World&nbsp;</td>
</tr>

<tr>
<td>15/09/2013 at 09:00&nbsp;</td>
<td>BAYAMO&nbsp;</td>
<td>TBA&nbsp;</td>
<td>Valero&nbsp;</td>
<td>Graypen&nbsp;</td>
</tr>

<tr>
<td>15/09/2013 at 09:00&nbsp;</td>
<td>AMADEA&nbsp;</td>
<td>Cork&nbsp;</td>
<td>Milford Shelf&nbsp;</td>
<td>Inchcape Shipping Services&nbsp;</td>
</tr>

<tr>
<td>15/09/2013 at 12:00&nbsp;</td>
<td>ARRAN&nbsp;</td>
<td>Dublin &nbsp;</td>
<td>Valero&nbsp;</td>
<td>Graypen&nbsp;</td>
</tr>

<tr>
<td>16/09/2013 at 01:00&nbsp;</td>
<td>EKFJORD&nbsp;</td>
<td>Gothenburg&nbsp;</td>
<td>Murco 1&nbsp;</td>
<td>Cory Bros&nbsp;</td>
</tr>

<tr>
<td>16/09/2013 at 21:30&nbsp;</td>
<td>MOZAH&nbsp;</td>
<td>Ras Laffan&nbsp;</td>
<td>South Hook No 1&nbsp;</td>
<td>S5 Agency World&nbsp;</td>
</tr>

<tr>
<td>17/09/2013 at 07:00&nbsp;</td>
<td>STRIDE&nbsp;</td>
<td>Murmansk&nbsp;</td>
<td>Valero 6&nbsp;</td>
<td>Graypen&nbsp;</td>
</tr>
</tbody>
</table>
</center>

As in above source code of html, I only want to search the 2nd column of every row for example only BAYAMO, STRIDE etc should be searched

Was it helpful?

Solution

You should consider using a proper HTML parser for that job.

If that's for a one time use though, you could try using a regex like this:

<tr>[^<]*<td[^>]*>[^<]*</td>[^<]*<td[^>]*>([^<]+)

regex101 demo

EDIT: And here's an ideone demo.

OTHER TIPS

A simple start:

<tr>.*?<td>.*?</td>.*?<td>(.*?)</td>

But that doesn't quite work, what if you have single-column rows? It will keep going until you get to the next row and match the first column there.

For this, we need something more complicated:

<tr>(?:.(?!<tr>))*?<td>.*?</td>(?:.(?!<tr>))*?<td>(.*?)</td>

It obviously won't work so nicely for nested tables and things, but this is the danger of trying to parse HTML with regex. For more on this, and just in general, I suggest you read this.

Test.

Explanation:

(?:...) is a non-capturing group, it doesn't change the match, just prevents it from being recorded in a group.

(?!...) is negative lookahead, matching if the following characters don't match some pattern.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top