读书人

求去百度html一正则表达式,该如何解决

发布时间: 2012-04-26 14:01:31 作者: rapoo

求去百度html一正则表达式
</DIV>
</td></tr></table>
<p style="margin-left:15px">
<table border=0><tr><td><font size=-1>
<a href="http://news.baidu.com/ns?cl=2&rn=20&tn=news&word=title%3A%28%B0%D9%B6%C8%29&web=5" target="_blank"><font color="#c60a00">百度</font>的相关新闻</a>  -
<a href="http://news.baidu.com/view.html?from=web" target="_blank"><font color="#8D88F0">今日焦点新闻</font></a>
</font></td></tr><tr><td><font><font size=-1>  <a href="http://news.baidu.com/ns[0]_http://www.nbd.com.cn/newShow.asp?D_ID=91186&web=5&query=%B0%D9%B6%C8" target="_blank"><font color=#C60A00>百度</font>VS阿里巴巴=最强VS最大</a> <font class=c>每日经济新闻 4小时58分钟前</font></font><br></font></td></tr></table>
</p>
<table border="0" cellpadding="0" cellspacing="0"><tr><td class=f><a href="http://www.baidu.com/" target="_blank"><font size="3"><font color=#C60A00>百度</font>一下,你就知道</font></a><br><font size=-1>新闻网页贴吧知道MP3图片 帮助 高级 空间 | 更多>> 把<font color=#C60A00>百度</font>设为首页 企业推广 | 搜索风云榜 | 关于<font color=#C60A00>百度</font> | About Baidu 2007 Baidu 使用<font color=#C60A00>百度</font>前必读 京ICP证030173号... <br><font color=#008000>www.baidu.com/ 2K 2007-12-20 </font> <br><a class="m" target="_blank" href="s?lm=0&si=&rn=100&tn=baiduadv&ie=gb2312&ct=0&wd=%B0%D9%B6%C8%20site%3Awww%2Ebaidu%2Ecom+&cl=2">www.baidu.com 上的更多结果</a></font></td></tr></table><br><table border="0" cellpadding="0" cellspacing="0"><tr><td class=f><a href="http://site.baidu.com/" target="_blank"><font size="3"><font color=#C60A00>百度</font>网址大全</font></a><br><font size=-1><font color=#C60A00>百度</font> 新浪 搜狐 网易 腾讯QQ 更多>> 新闻 新浪新闻 人民网 新华网 中央电视台 凤凰网 联合早报 更多>> 邮箱 163...<font color=#C60A00>百度</font>视频搜索 更多>> 企业推广 | 搜索风云榜 | 关于<font color=#C60A00>百度</font> | 网友反馈 2007 Baidu 使用<font color=#C60A00>百度</font>前必读 京ICP证030173号... <br><font color=#008000>site.baidu.com/ 25K 2007-12-24 </font> <br></font></td></tr></table><br><table border="0" cellpadding="0" cellspacing="0"><tr><td class=f><a href="http://mp3.baidu.com/" target="_blank"><

-----------------------------------------

我通过百度的高级搜索提取到搜索到的html代码,现在要做的是将信息文字如(搜索关键字“百度”就出现
第一条:百度一下,你就知道
新闻网页贴吧知道MP3图片 帮助 高级 空间 | 更多>> 把百度设为首页 企业推广 | 搜索风云榜 | 关于百度 | About Baidu 2007 Baidu 使用百度前必读 京ICP证030173号...


第2条:百度网址大全
百度 新浪 搜狐 网易 腾讯QQ 更多>> 新闻 新浪新闻 人民网 新华网 中央电视台 凤凰网 联合早报 更多>> 邮箱 163...百度视频搜索 更多>> 企业推广 | 搜索风云榜 | 关于百度 | 网友反馈 2007 Baidu 使用百度前必读 京ICP证030173号...
)提取出来,自己对正则表达式不太熟,望大家帮忙,谢谢!

[解决办法]
http://topic.csdn.net/u/20071220/14/7e73c0a7-dd43-44da-ad07-b9a79a46a680.html
[解决办法]
顶一下算了.
[解决办法]
调试通过了~~

C# code
using System;using System.Collections.Generic;using System.Text;using System.IO;using System.Text.RegularExpressions;namespace ConsoleApplication1{    class BaiduDataInfo    {        public string Title;        public string Content;        public BaiduDataInfo(string title, string content)        {            this.Title = title;            this.Content = content;        }        public override string ToString()        {            return string.Format("标题:{0}\n内容:{1}\n", Title, Content);        }    }    class Program    {        static void Main(string[] args)        {            TextReader tr = new StreamReader(@"d:\baidu.html",Encoding.GetEncoding("gb2312"));            string text = tr.ReadToEnd();            tr.Close();            tr.Dispose();            Regex reg = new Regex(@".*?<td\sclass=f>((?:(?!</td>).)+)</td>(?:(?!<td\sclass=f>).)+", RegexOptions.IgnoreCase | RegexOptions.Singleline | RegexOptions.Compiled);            text = reg.Replace(text, "$1★");            Regex reg2 = new Regex(@"</?(?:(?!br|>).)+[^>]*>", RegexOptions.IgnoreCase | RegexOptions.Singleline | RegexOptions.Compiled);            text = reg2.Replace(text, "");            //替换掉html空格&nbsp 和 < >括号&lt &gt            text = Regex.Replace(text, @"&[nN][bB][sS][pP];", " ");            text = Regex.Replace(text, @"&[lL][tT];", "<");            text = Regex.Replace(text, @"&[gG][tT];", ">");            Regex reg3 = new Regex(@"(?<title>(?:(?!<br>).)+)<br>(?<content>(?:(?!<br>).)+)<br>[^★]*★", RegexOptions.IgnoreCase | RegexOptions.Singleline | RegexOptions.Compiled);            List<BaiduDataInfo> list = new List<BaiduDataInfo>();            foreach (Match m in reg3.Matches(text))            {                string title = m.Groups["title"].Value;                string content = m.Groups["content"].Value;                list.Add(new BaiduDataInfo(title, content));            }            foreach (BaiduDataInfo data in list)            {                Console.WriteLine(data);            }            Console.Read();        }    }} 

读书人网 >C#

热点推荐