正则表达式提取html代码中的数据

zkj513703929 2010-07-02 09:17:10
<table>
<tr>
<td valign=center align=middle width=87 bgcolor=#CBF1FC height=20>
<div align="center"><span
style="FONT-SIZE: 9pt; FONT-FAMILY: 宋体, Verdana"><script language="JavaScript">tranCode("840")</script></span></div>
</td>
<td valign=center align=right width=79 height=20>
<div align="center"><span
style="FONT-SIZE: 9pt; FONT-FAMILY: 宋体, Verdana">6.7674</span></div>
</td>
<td valign=center align=right width=79 height=20>
<div align="center"><span
style="FONT-SIZE: 9pt; FONT-FAMILY: 宋体, Verdana">6.7946</span></div>
</td>
<td valign=center align=right width=79 height=20>
<div align="center"><span
style="FONT-SIZE: 9pt; FONT-FAMILY: 宋体, Verdana">6.7132</span></div>
</td>
<td valign=center align=right width=79 height=20>
<div align="center"><span
style="FONT-SIZE: 9pt; FONT-FAMILY: 宋体, Verdana">6.7946</span></div>
</td>

</tr></table>
我从一个网站上获取的信息,下面是获取信息的一行,我要提取每一行中每一列的数据
6.7674 6.7946 6.7946 6.7132
用正则表达式怎么提取,请指点一下,谢谢!
...全文
260 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
wangkun9999 2010-07-02
  • 打赏
  • 举报
回复

MatchCollection mc = Regex.Matches(html,@"(?is)"><span\s+style="FONT-SIZE: 9pt; FONT-FAMILY: 宋体, Verdana">(?<value>\d+(.\d+)?)</span>");
foreach(Match m in mc)
{
//m.Groups["value"].Value;
}

地下室小红叔 2010-07-02
  • 打赏
  • 举报
回复
//应该是double型
double yourData=double.Parse(mc.Groups[1].Value);
地下室小红叔 2010-07-02
  • 打赏
  • 举报
回复
            string content = "html 文本";
string regStr = "<span\\s+style=\"FONT-SIZE: 9pt; FONT-FAMILY: 宋体, Verdana\">\\s*([\\d\\.]*)\\s*</span>";
MatchCollection matches = Regex.Matches(content, regStr, RegexOptions.Multiline);
foreach (Match mc in matches)
{
//按你的原文,这里应依次得到4个mc
int yourData=int.Parse(mc.Groups[1].Value);
}
nocallstle 2010-07-02
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 nocallstle 的回复:]

干嘛一定要用正则

HTML code

<head runat="server">
<title>无标题页</title>

<script src="App_common/scripts/jquery-1.4.2.min.js" type="text/javascript"></script>

<script type="text/javascript">
……
[/Quote]

加个
<input type="hidden" raunt="server" id="hdTemp">

js

$("hdTemp").val(temp);

cs后台:

this.hdTemp.vaule;//再做字符串操作,不就一样么
zkj513703929 2010-07-02
  • 打赏
  • 举报
回复
我要放在服务器端自动的去执行,并更新数据库
nocallstle 2010-07-02
  • 打赏
  • 举报
回复
干嘛一定要用正则


<head runat="server">
<title>无标题页</title>

<script src="App_common/scripts/jquery-1.4.2.min.js" type="text/javascript"></script>

<script type="text/javascript">
$(function(){

var Htmls=$("table").find("span");
var temp="";
$(Htmls).each(function(entrtyIndex,entrty){
if(Htmls.eq(parseInt(entrtyIndex)).html()!="")
temp+=Htmls.eq(parseInt(entrtyIndex)).html()+",";
})
alert(temp);
});
</script>
</head>
<body>
<form id="form1" runat="server">
<table>
<tr>
<td valign="center" align="middle" width="87" bgcolor="#CBF1FC" height="20">
<div align="center">
<span style="font-size: 9pt; font-family: 宋体, Verdana">

</span>
</div>
</td>
<td valign="center" align="right" width="79" height="20">
<div align="center">
<span style="font-size: 9pt; font-family: 宋体, Verdana">6.7674</span></div>
</td>
<td valign="center" align="right" width="79" height="20">
<div align="center">
<span style="font-size: 9pt; font-family: 宋体, Verdana">6.7946</span></div>
</td>
<td valign="center" align="right" width="79" height="20">
<div align="center">
<span style="font-size: 9pt; font-family: 宋体, Verdana">6.7132</span></div>
</td>
<td valign="center" align="right" width="79" height="20">
<div align="center">
<span style="font-size: 9pt; font-family: 宋体, Verdana">6.7946</span></div>
</td>
</tr>
</table>
</form>
</body>
</html>
mm51221 2010-07-02
  • 打赏
  • 举报
回复
public string wipescript(string html)
{
System.Text.RegularExpressions.Regex regex1 = new System.Text.RegularExpressions.
Regex(@"<script[\s\s]+</script *>", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
System.Text.RegularExpressions.Regex regex2 = new System.Text.RegularExpressions.Regex
(@" href *= *[\s\s]*script *:", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
System.Text.RegularExpressions.Regex regex3 = new System.Text.RegularExpressions.Regex
(@" on[\s\s]*=", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
System.Text.RegularExpressions.Regex regex4 = new System.Text.RegularExpressions.Regex
(@"<iframe[\s\s]+</iframe *>", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
System.Text.RegularExpressions.Regex regex5 = new System.Text.RegularExpressions.Regex
(@"<frameset[\s\s]+</frameset *>", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
System.Text.RegularExpressions.Regex regex6 = new System.Text.RegularExpressions.Regex
(@"<\/*[^<>]*>", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
// html = regex1.Replace(html, ""); //过滤<script></script>标记
// html = regex2.Replace(html, ""); //过滤href=javascript: (<a>) 属性
// html = regex3.Replace(html, " _disibledevent="); //过滤其它控件的on...事件
// html = regex4.Replace(html, ""); //过滤iframe
// html = regex5.Replace(html, ""); //过滤frameset
html = regex6.Replace(html, ""); //过滤html
//html通用标签:"/<(?:(?:\/?[A-Za-z]\w+\b(?:[=\s](['"]?)[\s\S]*?\1)*)|(?:!--[\s\S]*?--))>/g"
return html;
}
zkj513703929 2010-07-02
  • 打赏
  • 举报
回复
不正确!
ganlianter 2010-07-02
  • 打赏
  • 举报
回复
>(-?\d+)(\.\d+)<$
may it be


zkj513703929 2010-07-02
  • 打赏
  • 举报
回复
按顺序获取 6.7674 6.7946 6.7132 6.7946

62,072

社区成员

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

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

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

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