用正则表达式提取网页中的特定内容。。高分

ylonghome 2005-07-30 02:36:34
下面这代码怎么提取内容为:
昨收盘 8.280
今开盘 8.260
涨跌额 +0.060
最低价 8.200
最高价 8.340
涨跌幅 +0.72%

<TR bgColor=#ffffe0>
<TD class=pdLeft5b height=19><A
href="http://finance.sina.com.cn/roll/20040709/1848861925.shtml"
target=_blank>昨收盘</A></TD>
<TD class=pdLeft5b id=itemPrevious>0.000</TD></TR>
<TR bgColor=#999999>
<TD height=1></TD>
<TD></TD></TR>
<TR bgColor=#ffffff>
<TD height=1></TD>
<TD></TD></TR>
<TR bgColor=#ffffe0>
<TD class=pdLeft5b height=19><A
href="http://finance.sina.com.cn/roll/20040709/1846861924.shtml"
target=_blank>今开盘</A></TD>
<TD class=pdLeft5b id=itemOpen>0.000</TD></TR>
<TR bgColor=#999999>
<TD height=1></TD>
<TD></TD></TR>
<TR>
<TD class=pdLeft5b height=19><A
href="http://finance.sina.com.cn/roll/20040716/1430875772.shtml"
target=_blank>涨跌额</A></TD>
<TD class=pdLeft5b id=itemDiffamount>0.000</TD></TR>
<TR>
<TD class=pdLeft5b height=19><A
href="http://finance.sina.com.cn/roll/20040716/1433875777.shtml"
target=_blank>最低价</A></TD>
<TD class=pdLeft5b id=itemLow>0.000</TD></TR>
<TR>
<TD class=pdLeft5b height=19><A
href="http://finance.sina.com.cn/roll/20040716/1432875775.shtml"
target=_blank>最高价</A></TD>
<TD class=pdLeft5b id=itemHigh>0.000</TD></TR>
<TR>
<TD class=pdLeft5b height=19><A
href="http://finance.sina.com.cn/roll/20040716/1431875774.shtml"
target=_blank>涨跌幅</A></TD>
<TD class=pdLeft5b
id=itemDiffpercent2>0.000</TD></TR>

换行要不要考虑回车的啊。。。郁闷的啊
...全文
227 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
lnwuyaowei 2006-01-21
  • 打赏
  • 举报
回复
学习.
syeerzy 2005-07-30
  • 打赏
  • 举报
回复
<a[^>]*?>(?<name>.*?)</a></td>[\s\S]*?<td[^>]*?>(?<price>.*?)</td>

fancyf(凡瑞) 的正则够意思,呵呵
biggerboy 2005-07-30
  • 打赏
  • 举报
回复
你说清楚点,什么意思?这种“http://finance.sina.com.cn/roll/20040716/1431875774.shtml”怎么提取值?
fanruinet 2005-07-30
  • 打赏
  • 举报
回复
string content = @"<TR bgColor=#ffffe0>
<TD class=pdLeft5b height=19><A
href=""http://finance.sina.com.cn/roll/20040709/1848861925.shtml""
target=_blank>昨收盘</A></TD>
<TD class=pdLeft5b id=itemPrevious>0.000</TD></TR>
<TR bgColor=#999999>
<TD height=1></TD>
<TD></TD></TR>
<TR bgColor=#ffffff>
<TD height=1></TD>
<TD></TD></TR>
<TR bgColor=#ffffe0>
<TD class=pdLeft5b height=19><A
href=""http://finance.sina.com.cn/roll/20040709/1846861924.shtml""
target=_blank>今开盘</A></TD>
<TD class=pdLeft5b id=itemOpen>0.000</TD></TR>
<TR bgColor=#999999>
<TD height=1></TD>
<TD></TD></TR>
<TR>
<TD class=pdLeft5b height=19><A
href=""http://finance.sina.com.cn/roll/20040716/1430875772.shtml""
target=_blank>涨跌额</A></TD>
<TD class=pdLeft5b id=itemDiffamount>0.000</TD></TR>
<TR>
<TD class=pdLeft5b height=19><A
href=""http://finance.sina.com.cn/roll/20040716/1433875777.shtml""
target=_blank>最低价</A></TD>
<TD class=pdLeft5b id=itemLow>0.000</TD></TR>
<TR>
<TD class=pdLeft5b height=19><A
href=""http://finance.sina.com.cn/roll/20040716/1432875775.shtml""
target=_blank>最高价</A></TD>
<TD class=pdLeft5b id=itemHigh>0.000</TD></TR>
<TR>
<TD class=pdLeft5b height=19><A
href=""http://finance.sina.com.cn/roll/20040716/1431875774.shtml""
target=_blank>涨跌幅</A></TD>
<TD class=pdLeft5b
id=itemDiffpercent2>0.000</TD></TR>";

Regex htmlRegex = new Regex(@"<a[^>]*?>(?<name>.*?)</a></td>[\s\S]*?<td[^>]*?>(?<price>.*?)</td>", RegexOptions.IgnoreCase);


MatchCollection mc = htmlRegex.Matches(content);
foreach (Match m in mc)
{
Console.WriteLine(m.Groups["name"].Value + " " + m.Groups["price"].Value);
}

输出结果:
昨收盘 0.000
今开盘 0.000
涨跌额 0.000
最低价 0.000
最高价 0.000
涨跌幅 0.000
athossmth 2005-07-30
  • 打赏
  • 举报
回复
www.relib.com

62,041

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧