正则熟悉的来帮个忙!!!

yue547283947 2011-09-30 10:20:27
<li>
• <a href="http://qq.com/nfdsb//content_30711112.htm" target="_blank" mon="a=1&p=1&pn=5">啊啊啊啊啊啊啊啊啊啊啊啊啊</a><span class="s">08:53</span><a href="/n?cmd=2&class=shyf&page=http%3A%2F%2Fnf.nfdaily.cn%2Fnfdsb%2Fcontent%2F2011-09%2F30%2Fcontent_30711112.htm&cls=shyf" target="_blank" class="rnews">47条相关>></a> </li>
<li>
• <a href="http://baidu.com/content_30711112.htm" target="_blank" mon="a=1&p=1&pn=5">噩噩噩噩噩噩噩噩噩噩噩噩噩</a><span class="s">08:53</span><a href="/n?cmd=2&class=shyf&page=http%3A%2F%2Fnf.nfdaily.cn%2Fnfdsb%2Fcontent%2F2011-09%2F30%2Fcontent_30711112.htm&cls=shyf" target="_blank" class="rnews">47条相关>></a>
</li>

问题是:

1.我想要 中间 “啊啊啊啊啊啊啊啊啊啊啊啊啊” 和“噩噩噩噩噩噩噩噩噩噩噩噩噩”!
2.我想要 http://qq.com/nfdsb//content_30711112.htm 想要这个链接地址
...全文
90 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
yue547283947 2011-09-30
  • 打赏
  • 举报
回复
谢谢大家哈!

我用字符串截取做的,刚改下2楼的做法,可行。所以下面的方法就不试了。

给分!!!
md5e 2011-09-30
  • 打赏
  • 举报
回复

string txt="<li>• <a href=\"http://qq.com/nfdsb//content_30711112.htm\" target=\"_blank\" mon=\"a=1&p=1&pn=5\">啊啊啊啊啊啊啊啊啊啊啊啊啊</a><span class=\"s\">08:53</span><a href=\"/n?cmd=2&class=shyf&page=http%3A%2F%2Fnf.nfdaily.cn%2Fnfdsb%2Fcontent%2F2011-09%2F30%2Fcontent_30711112.htm&cls=shyf\" target=\"_blank\" class=\"rnews\">47条相关>></a> </li><li>• <a href=\"http://baidu.com/content_30711112.htm\" target=\"_blank\" mon=\"a=1&p=1&pn=5\">噩噩噩噩噩噩噩噩噩噩噩噩噩</a><span class=\"s\">08:53</span><a href=\"/n?cmd=2&class=shyf&page=http%3A%2F%2Fnf.nfdaily.cn%2Fnfdsb%2Fcontent%2F2011-09%2F30%2Fcontent_30711112.htm&cls=shyf\" target=\"_blank\" class=\"rnews\">47条相关>></a> </li>";
Regex re = new Regex("<a href=\"([^\"]*?)\" target=\"_blank\" mon=\"a=1&p=1&pn=5\">([^>]*?)</a>", RegexOptions.IgnoreCase);
Match mc = re.Match(txt);
while (mc.Success)
{

Response.Write(mc.Groups[1]);
Response.Write("<br/>" + mc.Groups[2]);
Response.Write("<br/>*******************<br/>");
mc = mc.NextMatch();
}
Response.End();
md5e 2011-09-30
  • 打赏
  • 举报
回复

string txt="<li>• <a href=\"http://qq.com/nfdsb//content_30711112.htm\" target=\"_blank\" mon=\"a=1&p=1&pn=5\">啊啊啊啊啊啊啊啊啊啊啊啊啊</a><span class=\"s\">08:53</span><a href=\"/n?cmd=2&class=shyf&page=http%3A%2F%2Fnf.nfdaily.cn%2Fnfdsb%2Fcontent%2F2011-09%2F30%2Fcontent_30711112.htm&cls=shyf\" target=\"_blank\" class=\"rnews\">47条相关>></a> </li><li>• <a href=\"http://baidu.com/content_30711112.htm\" target=\"_blank\" mon=\"a=1&p=1&pn=5\">噩噩噩噩噩噩噩噩噩噩噩噩噩</a><span class=\"s\">08:53</span><a href=\"/n?cmd=2&class=shyf&page=http%3A%2F%2Fnf.nfdaily.cn%2Fnfdsb%2Fcontent%2F2011-09%2F30%2Fcontent_30711112.htm&cls=shyf\" target=\"_blank\" class=\"rnews\">47条相关>></a> </li>";
Regex re = new Regex("<a[^>].*href=\"([^\"]*)\".* mon=\"a=1&p=1&pn=5\">(.*?)</a>", RegexOptions.IgnoreCase);
Match mc = re.Match(txt);
while (mc.Success)
{

Response.Write(mc.Groups[1]);
Response.Write("<br/>" + mc.Groups[2]);
Response.Write("<br/>*******************<br/>");
mc = mc.NextMatch();
}
Response.End();
tuhezhu371520 2011-09-30
  • 打赏
  • 举报
回复
方法同三楼 得到A标签再进行区分 是在JS 中选择吧
那就可以用 jQuery对象获得之后区分
huangwenquan123 2011-09-30
  • 打赏
  • 举报
回复
            string str = File.ReadAllText(@"E:\1.txt", Encoding.GetEncoding("gb2312"));
Regex reg = new Regex("(?is)<li[^>]*?>.*?<a[^>]*?>(.*?)</a>");
foreach (Match m in reg.Matches(str))
Console.WriteLine(m.Groups[1].Value);
Regex href = new Regex(@"(?is)<li[^>]*?>.*?<a[^>]*?href=(['""]?)([^'""]+qq[^'""]+)\1[^>]*?>");
foreach (Match m in href.Matches(str))
Console.WriteLine(m.Groups[2].Value);
  • 打赏
  • 举报
回复
得到所有的a标签,然后在筛选吧 href:http://qq.com/nfdsb//content_30711112.htm text:啊啊啊啊啊啊啊啊啊啊啊啊啊

string tempStr = "<li>• <a href=\"http://qq.com/nfdsb//content_30711112.htm\" target=\"_blank\" mon=\"a=1&p=1&pn=5\">啊啊啊啊啊啊啊啊啊啊啊啊啊</a><span class=\"s\">08:53</span><a href=\"/n?cmd=2&class=shyf&page=http%3A%2F%2Fnf.nfdaily.cn%2Fnfdsb%2Fcontent%2F2011-09%2F30%2Fcontent_30711112.htm&cls=shyf\" target=\"_blank\" class=\"rnews\">47条相关>></a> </li><li>• <a href=\"http://baidu.com/content_30711112.htm\" target=\"_blank\" mon=\"a=1&p=1&pn=5\">噩噩噩噩噩噩噩噩噩噩噩噩噩</a><span class=\"s\">08:53</span><a href=\"/n?cmd=2&class=shyf&page=http%3A%2F%2Fnf.nfdaily.cn%2Fnfdsb%2Fcontent%2F2011-09%2F30%2Fcontent_30711112.htm&cls=shyf\" target=\"_blank\" class=\"rnews\">47条相关>></a> </li>";
string pattern = @"<a[^>]*href=(""(?<href>[^""]*)""|'(?<href>[^']*)'|(?<href>[^\s>]*))[^>]*>(?<text>[\s\S]*?)</a>";
MatchCollection mc = Regex.Matches(tempStr, pattern, RegexOptions.Multiline|RegexOptions.IgnoreCase);
for (int i = 0; i < mc.Count; i++)
{
string href = mc[i].Groups["href"].Value;//href
string text = mc[i].Groups["text"].Value;//得到标签内容

}
CalvinR 2011-09-30
  • 打赏
  • 举报
回复
帮顶啊

62,047

社区成员

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

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

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

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