网页源码: 
<tr>
          <td valign="top"><table width="743" border="0" align="center" cellpadding="0" cellspacing="0">
              <tr>
                <td width="743" height="38" background="pic/z10.jpg"><table width="96%" border="0" align="center" cellpadding="0" cellspacing="0" class="unnamed1">
                    <tr> 
                      <td height="30" align="left"><font color="555555" size="2"><strong>游戏区域</strong></font></td>
                      <td width="134" align="right"><font color="555555" size="2"><strong>目前价格</strong></font></td>
                      <td width="132" align="right"><font color="555555" size="2"><strong>收购量</strong></font></td>
                      <td width="80" align="center"><font color="555555" size="2"><strong>状态</strong></font></td>
                      <td width="58" align="center"><font color="555555" size="2"><strong>操作</strong></font></td>
                    </tr>
                  </table>
                </td>
              </tr>
              <tr> 
                <td height="500" valign="top" background="pic/z12.jpg" align="center">
                    
                      <table cellpadding="0" cellspacing="0" border="0" width="95%" align="center">
                    
                        <tr>
                            <td align="left"> 
                              <table width="700" border="0" align="center" cellpadding="0" cellspacing="0" class="unnamed1">
                                <tr align="left"> 
                                  <td >
                                  Aegwynn US-Alliance   //需提取
                                  </td>
                                  <td width="134" align="right">
                                  0.88
                                 元/                    //需提取
                                Gold
                                  
                                  </td>
                                  <td width="132" align="right"><font color="535353">
                                  0                 //需提取
</font></td>          
                                  <td width="80" align="center"><font color="535353"> 
                                  满仓               //需提取
                                  </font></td>
                                  <td width="58" height="25" align="center">
                                  </td>
                                </tr>
                         </table>
                        </td></tr>
                        <tr><td>
                            <img src="pic/line1.jpg" width="100%" height="1" alt="" /></td>
                        </tr>
                    
                        <tr>
                            <td align="left"> 
                              <table width="700" border="0" align="center" cellpadding="0" cellspacing="0" class="unnamed1">
                                <tr align="left"> 
                                  <td >
                                  Aegwynn US-Horde
                                  </td>
                                  <td width="134" align="right">
                                  0.94
                                 元/
                                Gold
                                  
                                  </td>
                                  <td width="132" align="right"><font color="535353">
                                  0</font></td>
                                  <td width="80" align="center"><font color="535353"> 
                                  满仓
                                  </font></td>
                                  <td width="58" height="25" align="center">
                                  </td>
                                </tr>
                         </table>
                        </td></tr>
                        <tr><td>
                            <img src="pic/line1.jpg" width="100%" height="1" alt="" /></td>
                        </tr>
                    
                        
网页格式:
游戏区域              目前价格       收购量   状态 操作 
 
Aegwynn US-Alliance  0.88 元/ Gold    0        满仓   
 Aegwynn US-Horde  0.94 元/ Gold       0        满仓   
 
 
Aerie'peak US-Alliance  0.94 元/ Gold  0       满仓   
 
 
Aerie'peak US-Horde  0.99 元/ Gold     0       满仓   
 
 
Agamaggan US-Alliance  0.82 元/ Gold   0        满仓  
 
 
请问提取:游戏区域  ,目前价格   ,收购量  ,状态  的正则表达式. 

解决方案 »

  1.   

    太长了,就采取了点偷懒的办法,假定格式都是这样的,楼主试下吧,如果有不符合的,我再看看吧string yourStr = richTextBox1.Text;
    MatchCollection mc = Regex.Matches(yourStr, @"<table[^>]*?>\s+<tr[^>]*?>\s+<td[^>]*?>(?<area>[^<]*?)</td>\s+<td[^>]*?>(?<price>[^<]*?)</td>\s+<td[^>]*?><font[^>]*?>(?<num>[^<]*?)</font></td>\s+<td[^>]*?><font[^>]*?>(?<state>[^<]*?)</font></td>", RegexOptions.IgnoreCase);
    foreach (Match m in mc)
    {
        richTextBox2.Text += m.Groups["area"].Value + "\n";     //游戏区域
        richTextBox2.Text += m.Groups["price"].Value + "\n";    //目前价格
        richTextBox2.Text += m.Groups["num"].Value + "\n";      //收购量
        richTextBox2.Text += m.Groups["state"].Value + "\n";    //状态
    }
      

  2.   

    谢谢哈
    你看从<tr align="left"> 这里开始匹配是不是也可以啊
      

  3.   

    那是我一开始写的时候加上的,其实如果格式固定的话,把前面的<table[^>]*?>\s+<tr[^>]*?>\s+都去掉,从<td开始匹配也可以,只不过我这样偷懒的写法,通用性会差好多,不过正则就是适用就好,只要能满足你的要求,就不用这么计较了
      

  4.   

    谢谢lxcnn(过客)  抓取成功了.
    还想问问.
    我把结果连接成这么一个字符串后
    temp = temp + m.Groups["area"].Value + " " + m.Groups["price"].Value + " " + m.Groups["num"].Value +" "+m.Groups["state"].Value怎么把这些数据分别写入数据库.或者不这样连接,能提供点意见.谢谢
      

  5.   

    暈 要是保證HTML格式不會錯
    (?<=[\>])+[^\>\<]+(?=[\<])
    這樣也可以阿
      

  6.   

    谢谢
    我测试了一下,
    结果:448个
    第一个用了26.08秒
    第二个用了25.67秒
    等测试一下chinnel() 兄的
      

  7.   

    chinnel() 兄,用你的.在那里读值出来啊?
    还想请问一下你们,把这些数据写入数据库是怎样的?用数据集?还是怎么?
      

  8.   

    后续处理我做得比较少,可以在foreach里直接写入数据库的
    如果想抓取完再一起处理,可以分别存数组,全部抓取完成后,for循环写数据库
    另外如果数据量比较大,存在效率问题时,可以在后面加个 RegexOptions.Compiled参数,这样再测试下
    MatchCollection mc = Regex.Matches(yourStr, @"<td[^>]*?>(?<area>[^<]*?)</td>\s+<td[^>]*?>(?<price>[^<]*?)</td>\s+<td[^>]*?><font[^>]*?>(?<num>[^<]*?)</font></td>\s+<td[^>]*?><font[^>]*?>(?<state>[^<]*?)</font></td>", RegexOptions.IgnoreCase | RegexOptions.Compiled);
      

  9.   

    谢谢:lxcnn(过客)
    这次主要做几十个这样的网页的采集.打算分别写正则表达式.存在数据库中,然后定时采集.