开发者

regular expression - find and split links in a table

another question of me about regular expression, its so complicated for me :S So I'm happy for an additional help.

I have a table and I like to read all links inside this table and split it to groups.

The goal should be

  1. Person 1
  2. Status of person 1

  1. Person 2
  2. Status of Person 2

So i have to get the values inside the links in this table

<a class="darklink" href="testlink">Person 2, - Status of Person 2</a>

Is it possible to search just in a table which has a specific

Tag before? like this <p>title</p>(because there are other similar tables at my site)

<p>title</p>
<table cellspacing="0" cellpadding="0" border="0" width="95%">
 <tbody>
  <tr>
   <td bgcolor="#999999" colspan="2"><img height="1" border="0" width="1" src="images/dot_transp.gif" alt=" "/> </td>
  </tr>
  <tr>
   <td><a class="darklink" href="asdfer">Person1, - Status of Person1 </a> </td>
   <td valign="bottom"></td>
  </tr>
  <tr>
   <td bgcolor="#999999" colspan="2"><img height="1" border="0" width="1" src="images/dot_t开发者_开发技巧ransp.gif" alt=" "/> </td>
  </tr>
  <tr>
   <td><a class="darklink" href="aeraseraesr">Person 2, - Status of Person 2</a></td>
   <td valign="bottom"><a href="aeraeraer"> <img hspace="0" height="16" border="0" align="right" width="12" vspace="0" alt=" " src="images/ico_link.gif"/> </a> </td>
  </tr>
  <tr>
   <td bgcolor="#999999" colspan="2"><img height="1" border="0" width="1" src="images/dot_transp.gif" alt=" "/> </td>
  </tr>
  <tr>
   <td><a class="darklink" href="asdfasdf">Person 3. - Status of Person 3</a></td>
   <td valign="bottom"><a href="aerere"> </a> </td>
  </tr>
  <tr> </tr>
 </tbody>
</table>


Your regular expression should be:

<a class="darklink" .*?>(.*?). - (.*?)</a>

or if you get line breaks inside your <a> tag:

<a class="darklink" [\s\S]*?>([\s\S]*?). - *([\s\S]*?)</a>

So, following code should works:

Regex person = new Regex(@"<a class=""darklink"" .*?>(.*?). - (.*?)</a>");
foreach (Match m in person.Matches(input))
{
    Console.WriteLine("First group : {0}", m.Groups[1]);
    Console.WriteLine("Second group: {0}", m.Groups[2]);
};
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜