获取以下网址里面的表格里的公告内容:http://www.hkexnews.hk/listedco/listconews/mainindex/SEHK_LISTEDCO_DATETIME_TODAY_C.HTM要求是通过正则表达式将公告的内容“匹配”出来,以写入数据库中有以下字段的数据表中发放时间 代码 股份名称 文件类型 文件标题 文件网址 看似挺简单的,但一写正则表达式头都大了,而且上面的网页当中,表格嵌套的太多,实在不知道怎么写呀,求高手帮忙

解决方案 »

  1.   

    HttpWebRequest hw = (HttpWebRequest)WebRequest.Create("http://www.hkexnews.hk/listedco/listconews/mainindex/SEHK_LISTEDCO_DATETIME_TODAY_C.HTM");
                WebResponse wr = hw.GetResponse();            Stream stream = wr.GetResponseStream();
                StreamReader sr = new StreamReader(stream, Encoding.GetEncoding("BIG5"));
                string html = sr.ReadToEnd();
                string pattern = @"(?i)<tr[^>]*?class=(['""])row\d+\1[^>]*?>\s*?<td[^>]*?>((?:(?!</?td>)[\s\S])*?)</td>\s*?\s*?";
                pattern+=@"<td[^>]*?>([^<>]*?)</td>\s*?\s*?<td[^>]*?>\s*?<nobr>([^>]*?)\s*?</nobr>\s*?</td>\s*?\s*?<td[^>]*?>";
                pattern += @"\s*?<div[^>]*?>\s*?([^<>]+)(?:(?!</?div>)[\s\S])*?</div>\s*?<a[^>]*?>([^<]*?)</a>[\s\S]*?[((]([^(())]*?)[))]";
                pattern+=@"\s*?</td>\s*?</tr>";
                var result = Regex.Matches(html, pattern).Cast<Match>().Select(a => new
                {
                    发放时间 = Regex.Replace(a.Groups[2].Value,@"(?i)<[^>]*?>|\s+",string.Empty),
                    代码 = a.Groups[3].Value,
                    股份名称 = a.Groups[4].Value,
                    文件类型 = a.Groups[5].Value,
                    文件标题 = a.Groups[6].Value,
                    文件大小 = a.Groups[7].Value
                });
      

  2.   

    HttpWebRequest hw = (HttpWebRequest)WebRequest.Create("http://www.hkexnews.hk/listedco/listconews/mainindex/SEHK_LISTEDCO_DATETIME_TODAY_C.HTM");
                WebResponse wr = hw.GetResponse();            Stream stream = wr.GetResponseStream();
                StreamReader sr = new StreamReader(stream, Encoding.GetEncoding("BIG5"));
                string html = sr.ReadToEnd();
                string pattern = @"(?i)<tr[^>]*?class=(['""])row\d+\1[^>]*?>\s*?<td[^>]*?>((?:(?!</?td>)[\s\S])*?)</td>\s*?\s*?";
                pattern += @"<td[^>]*?>([^<>]*?)</td>\s*?\s*?<td[^>]*?>\s*?<nobr>([^>]*?)\s*?</nobr>\s*?</td>\s*?\s*?<td[^>]*?>";
                pattern += @"\s*?<div[^>]*?>\s*?([^<>]+)(?:(?!</?div>)[\s\S])*?</div>\s*?<a[^>]*?href=(['""]?)([^'""]*?)\6[^>]*?>([^<]*?)</a>[\s\S]*?[((]([^(())]*?)[))]";
                pattern += @"\s*?</td>\s*?</tr>";
                var result = Regex.Matches(html, pattern).Cast<Match>().Select(a => new
                {
                    发放时间 = Regex.Replace(a.Groups[2].Value, @"(?i)<[^>]*?>|\s+", string.Empty),
                    代码 = a.Groups[3].Value,
                    股份名称 = a.Groups[4].Value,
                    文件类型 = a.Groups[5].Value,
                    文件标题 = a.Groups[8].Value,
                    文件大小 = a.Groups[9].Value,
                    文件网址=a.Groups[7].Value
                });