利用正则表达式提取网页中Table内的数据

来源:互联网 发布:图书馆借书数据流程图 编辑:程序博客网 时间:2024/04/29 02:12

利用正则表达式提取网页中Table内的数据

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text.RegularExpressions;

public partial class Default2 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {


        string pagedata = @"<table class=""tabContent"" style=""width:681px;"">
     <tr><td width=""60"" id=""n001"">周六010</td>
    <td width=""100"">英格兰甲级联赛</td>
    <td id=""t036""><a href='http://info.sporttery.cn/football/history/information.php?match_id=7844' target='_blank'>诺维奇<strong> VS </strong>利兹联</a></td>
    <td width=""100"">03-27 22:59</td>
    <td width=""50""><input type=""checkbox"" id=""m036001"" name=""m036001"" value=""49465653"" onclick=""select_match('036001')""/>1.85</td><td width=""50""><input type=""checkbox"" id=""m036002"" name=""m036002"" value=""51465053"" onclick=""select_match('036002')""/>3.25</td><td width=""50""><input type=""checkbox"" id=""m036003"" name=""m036003"" value=""51465148"" onclick=""select_match('036003')""/>3.30</td>    <td width=""50""><input type=""checkbox"" id=""all036"" onclick=""bao('036',3)""/></td></tr>
   </tr>   <tr><td width=""60"" id=""n037"">周六046</td>
    <td width=""100"">苏格兰超级联赛</td>
    <td id=""t037""><a href='http://info.sporttery.cn/football/history/information.php?match_id=7845' target='_blank'>阿伯丁<strong> VS </strong>圣米伦</a></td>
    <td width=""100"">03-27 22:59</td>
    <td width=""50""><input type=""checkbox"" id=""m037001"" name=""m037001"" value=""49465656"" onclick=""select_match('037001')""/>1.88</td><td width=""50""><input type=""checkbox"" id=""m037002"" name=""m037002"" value=""51464948"" onclick=""select_match('037002')""/>3.10</td><td width=""50""><input type=""checkbox"" id=""m037003"" name=""m037003"" value=""51465153"" onclick=""select_match('037003')""/>3.35</td>    <td width=""50""><input type=""checkbox"" id=""all037"" onclick=""bao('037',3)""/></td></tr>
   </tr>   <tr><td width=""60"" id=""n038"">周六047</td>
    <td width=""100"">苏格兰超级联赛</td>
    <td id=""t038""><a href='http://info.sporttery.cn/football/history/information.php?match_id=7846' target='_blank'>凯尔特人(-1)<strong> VS </strong>基马诺克</a></td>
    <td width=""100"">03-27 22:59</td>
    <td width=""50""><input type=""checkbox"" id=""m038001"" name=""m038001"" value=""49465253"" onclick=""select_match('038001')""/>1.45</td><td width=""50""><input type=""checkbox"" id=""m038002"" name=""m038002"" value=""52464853"" onclick=""select_match('038002')""/>4.05</td><td width=""50""><input type=""checkbox"" id=""m038003"" name=""m038003"" value=""52465448"" onclick=""select_match('038003')""/>4.60</td>    <td width=""50""><input type=""checkbox"" id=""all038"" onclick=""bao('038',3)""/></td></tr>
   </tr>
</table>
";

        string result="";

        Regex re = new Regex(@"<tr><td.*?>(?<text1>.*?)</td>\s+?<td.*?>(?<text2>.*?)</td>\s+?<td.*?><a.*?>(?<text3>.*?)<strong> VS </strong>(?<text4>.*?)</a></td>\s+?<td.*?>(?<text5>.*?)</td>\s+?<td .*?><input .*?/>(?<text6>.*?)</td><td .*?><input .*?/>(?<text7>.*?)</td><td .*?><input .*?/>(?<text8>.*?)</td>"); ----*? 或+?表示非贪婪模式;\s+?决定了可以换行;使用RegexBuddy工具验证


        int i = 0;

        for (Match m = re.Match(pagedata); m.Success && i < 10; m = m.NextMatch())
        {

            result += m.Groups["text1"].Value.Trim() + "|";
            result += m.Groups["text2"].Value.Trim() + "|";
            result += m.Groups["text3"].Value.Trim() + "|";
            result += m.Groups["text4"].Value.Trim() + "|";
            result += m.Groups["text5"].Value.Trim() + "|";
            result += m.Groups["text6"].Value.Trim() + "|";
            result += m.Groups["text7"].Value.Trim() + "|";
            result += m.Groups["text8"].Value.Trim() + "|";
            result += "<br>";

            i++;

        }

        Response.Write(result);

    }
}

原创粉丝点击