请教一个简单的正则表达式问题,用于html解析(问题3)

poweruser 2006-09-14 05:00:39
求一个正则表达式,这次难度有所提升,需要去掉中间一些超链接。
解析如下内容:
<td id="colE3E">Windows XP Service Pack 1</td><td id="colE6E">Windows XP Service Pack 2</td><td id="colECF">Windows Server 2003</td><td id="colEFF" style="border-right: 1px solid rgb(204, 204, 204);">Windows Server 2003 Service Pack 1</td></tr></thead><tbody><tr class="record" valign="top"><td><p class="lastInCell">用户配置文件特权提升漏洞 - <a href="http://www.cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2006-3443" target="_blank">CVE-2006-3443</a></p></td><td><p class="lastInCell">特权提升</p></td><td><p class="lastInCell">重要</p></td><td><p class="lastInCell">低</p></td><td><p class="lastInCell">低</p></td><td><p class="lastInCell">低</p></td><td style="border-right: 1px solid rgb(204, 204, 204);"><p class="lastInCell">低</p></td></tr><tr class="evenRecord" valign="top"><td><p class="lastInCell">未处理的异常漏洞 - <a href="http://www.cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2006-3648" target="_blank">CVE-2006-3648</a></p></td>

要获取的内容为:
用户配置文件特权提升漏洞 - CVE-2006-3443
未处理的异常漏洞 - CVE-2006-3648

多谢多谢!
...全文
317 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
RexZheng 2006-09-15
  • 打赏
  • 举报
回复
楼主客气了

下回我手脚快点,嘿
poweruser 2006-09-15
  • 打赏
  • 举报
回复
to 0009(夏天以南)

可惜这个帖子我已经结贴了,这帖不能给你分了。
不过我还会问问题,到时候你进来我给你分。
谢谢:)
RexZheng 2006-09-15
  • 打赏
  • 举报
回复
string str = "<td id=\"colE3E\">Windows XP Service Pack 1</td><td id=\"colE6E\">Windows XP Service Pack 2</td><td id=\"colECF\">Windows Server 2003</td><td id=\"colEFF\" style=\"border-right: 1px solid rgb(204, 204, 204);\">Windows Server 2003 Service Pack 1</td></tr></thead><tbody><tr class=\"record\" valign=\"top\"><td><p class=\"lastInCell\">用户配置文件特权提升漏洞 - <a href=\"http://www.cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2006-3443\" target=\"_blank\">CVE-2006-3443</a></p></td><td><p class=\"lastInCell\">特权提升</p></td><td><p class=\"lastInCell\">重要</p></td><td><p class=\"lastInCell\">低</p></td><td><p class=\"lastInCell\">低</p></td><td><p class=\"lastInCell\">低</p></td><td style=\"border-right: 1px solid rgb(204, 204, 204);\"><p class=\"lastInCell\">低</p></td></tr><tr class=\"evenRecord\" valign=\"top\"><td><p class=\"lastInCell\">未处理的异常漏洞 - <a href=\"http://www.cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2006-3648\" target=\"_blank\">CVE-2006-3648</a></p></td>";

MatchCollection mc = Regex.Matches(str, @"\<p class=""lastInCell""\>([^<]*)\<a [^\>]+\>([^\<]*)\</a\>\</p\>");
for (int i = 0; i < mc.Count; i++)
{
Console.WriteLine(mc[i].Groups[1].Value + mc[i].Groups[2].Value);
}
poweruser 2006-09-15
  • 打赏
  • 举报
回复
to petshop4(开始混正则了)
OK了,只是最后多了一个",用replace处理一下就可以了,非常感谢!

谢谢大家,结贴给分。
petshop4 2006-09-15
  • 打赏
  • 举报
回复
<p class="lastInCell">([^<]*)?<a href=".*?name=([^<]*)? target="_blank">
组1和组2就是你想要的值
测试通过
poweruser 2006-09-14
  • 打赏
  • 举报
回复
如果只用这个条件
@"<p class=""lastInCell"">(?<part1>.*?\s*-\s*)<a href=.*?>(?<part2>.*?)</a></p></td>"

第一条获取结果为:
用户配置文件特权提升漏洞 -
CVE-2006-3443
正确。

第二条信息获取结果为:
特权提升</p></td><td><p class="lastInCell">重要</p></td><td><p class="lastInCell
">低</p></td><td><p class="lastInCell">低</p></td><td><p class="lastInCell">低</
p></td><td style="border-right: 1px solid rgb(204, 204, 204);"><p class="lastInC
ell">低</p></td></tr><tr class="evenRecord" valign="top"><td><p class="lastInCel
l">未处理的异常漏洞 -
CVE-2006-3648

不正确,第二条前面多了一些其他的数据内容。

测试代码为:
string r2 = @"<p class=""lastInCell"">(?<part1>.*?\s*-\s*)<a href=.*?>(?<part2>.*?)</a></p></td>";
Regex reg = new Regex(r2);
MatchCollection matches = reg.Matches(htmlCode);

System.Collections.IEnumerator enumerator = matches.GetEnumerator();
while (enumerator.MoveNext() && enumerator.Current != null)
{
Match match = (Match)enumerator.Current;
details = match.Value;
Console.WriteLine(match.Groups["part1"].Value);
Console.WriteLine(match.Groups["part2"].Value);
}
poweruser 2006-09-14
  • 打赏
  • 举报
回复
to wthorse()
我的测试代码是这样的
string pat = @"<p class=""lastInCell"">(?<part1>.*?) - <a href=.*?>(?<part2>.*?)</a></p></td><td><p class=.*?>.*?<tr.*?><td><p class=.*?>(.*?) - <a href=.*?>(.*?)</a>";
Regex reg = new Regex(r);
Match match = reg.Match(htmlCode); // htmlCode为输入数据
Console.WriteLine(match.Groups["part1"].Value);
Console.WriteLine(match.Groups["part2"].Value);

如果有多条,我怎么动态编写这个表达式,并怎么动态获取我关注的数据呢?
谢谢!
poweruser 2006-09-14
  • 打赏
  • 举报
回复

to wthorse()
你的方法可以获取这个示例中的内容:)
但如果html中包含的信息不固定,有可能是1条、2条、3条,或者更多条数据时应该怎么处理?
谢谢!
poweruser 2006-09-14
  • 打赏
  • 举报
回复
to wthorse()
我先试试,在C#中,<p class="lastInCell">(.*?) - <a href=.*?>(.*?)</a></p></td><td><p class=.*?>.*?<tr.*?><td><p class=.*?>(.*?) - <a href=.*?>(.*?)</a>,这样的正则应该如何获取其中的值呢?我C#是初学,谢谢啊!

to BeRush(艾威)
能举个例子吗?没用过document对象,谢谢!

to bejon(阿牛[千分散尽还复来])
有C#版本的吗,呵呵:)
不用正则也可以,但这个html代码中可能不只一处<p class=""lastInCell"">,别的地方也有<p class=""lastInCell"">的。谢谢!
阿牛138588 2006-09-14
  • 打赏
  • 举报
回复
不用正则行不行?
Dim str1 As String = webtext
Dim str2() As String
str2 = str1.Split("<p class=""lastInCell"">")
For i As Integer = 0 To UBound(str2) - 1
Debug.Print(Strings.Left(str2(i), InStr(str2(i), "<a href=") - 1))
Next
BeRush 2006-09-14
  • 打赏
  • 举报
回复
用document对象取得吧
wthorse 2006-09-14
  • 打赏
  • 举报
回复
<p class="lastInCell">(.*?) - <a href=.*?>(.*?)</a></p></td><td><p class=.*?>.*?<tr.*?><td><p class=.*?>(.*?) - <a href=.*?>(.*?)</a>
得出1234
poweruser 2006-09-14
  • 打赏
  • 举报
回复
谢谢楼上的老兄,但你提供的这个不行啊,这个只是把html标识去掉了,返回的内容是
"Windows XP Service Pack 1Windows XP Service Pack 2Windows Server 2003Windows Server 2003 Service Pack 1用户配置文件特权提升漏洞 - CVE-2006-3443特权提升重要低低低低未处理的异常漏洞 - CVE-2006-3648"

跟我想要的不相同。

我想要的应该是有两条记录,分别为:
用户配置文件特权提升漏洞 - CVE-2006-3443
未处理的异常漏洞 - CVE-2006-3648
Eddie005 2006-09-14
  • 打赏
  • 举报
回复
试试:

string tmp = "<td id=\"colE3E\">Windows XP Service Pack 1</td><td id=\"colE6E\">Windows XP Service Pack 2</td><td id=\"colECF\">Windows Server 2003</td><td id=\"colEFF\" style=\"border-right: 1px solid rgb(204, 204, 204);\">Windows Server 2003 Service Pack 1</td></tr></thead><tbody><tr class=\"record\" valign=\"top\"><td><p class=\"lastInCell\">用户配置文件特权提升漏洞 - <a href=\"http://www.cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2006-3443\" target=\"_blank\">CVE-2006-3443</a></p></td><td><p class=\"lastInCell\">特权提升</p></td><td><p class=\"lastInCell\">重要</p></td><td><p class=\"lastInCell\">低</p></td><td><p class=\"lastInCell\">低</p></td><td><p class=\"lastInCell\">低</p></td><td style=\"border-right: 1px solid rgb(204, 204, 204);\"><p class=\"lastInCell\">低</p></td></tr><tr class=\"evenRecord\" valign=\"top\"><td><p class=\"lastInCell\">未处理的异常漏洞 - <a href=\"http://www.cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2006-3648\" target=\"_blank\">CVE-2006-3648</a></p></td>";

tmp = System.Text.RegularExpressions.Regex.Replace(tmp,"<(.|\n)+?>","");

110,533

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

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