<a target="_blank" href="http://ppp.hhhh.com/afd_01_02/20070409141647540.shtml" title="abcdefg">abcdefg</a> <span >2007-4-9</span><a target="_blank" href="http://ppp.hhhh.com/afd_01_02/20130465141647549.shtml" title="abcdefg">abcdefg</a> <span >2007-5-9</span>列出2条例子如何得到URl 和他标题我写MatchCollection mc = Regex.Matches(content,@"<A\s+target=_blank\s+href=(?<url>.*shtml)\\s+title=(.*)>(?<subject>.*)</a>",RegexOptions.IgnoreCase);
无法获得 是不是因为"的关系 我提供的地址抓不出
正确正则怎么出?PS
我参考
<a href=/c/2006-02-19/19568247131s.shtml TARGET=_blank>巴基斯坦总统穆沙拉夫抵京访华</a>例子
MatchCollection mc = Regex.Matches(content,@"<A\s+href=(?<url>.*shtml)\s*TARGET=_blank>(?<subject>.*)</a>",RegexOptions.IgnoreCase);可以获得
无法获得 是不是因为"的关系 我提供的地址抓不出
正确正则怎么出?PS
我参考
<a href=/c/2006-02-19/19568247131s.shtml TARGET=_blank>巴基斯坦总统穆沙拉夫抵京访华</a>例子
MatchCollection mc = Regex.Matches(content,@"<A\s+href=(?<url>.*shtml)\s*TARGET=_blank>(?<subject>.*)</a>",RegexOptions.IgnoreCase);可以获得
MatchCollection mc = Regex.Matches(yourStr, @"<a\s[^>]*?href=""(?<url>[^""]*?)""[^>]*?>(?<title>[\s\S]*?)</a>", RegexOptions.IgnoreCase);
foreach(Match m in mc)
{
richTextBox2.Text += m.Groups["url"].Value + "\n";
richTextBox2.Text += m.Groups["title"].Value + "\n";
}这个只考虑楼主所给例子里href=后为"的情况,如果可能为'或是直接接网址,需要改一下
MatchCollection mc = Regex.Matches(yourStr, @"<a\s[^>]*?href=([""']?)(?<url>[^'""\s]*)\1?[^>]*?title=([""']?)(?<title>[^'""\s]*)\2?[^>]*?>([\s\S]*?)</a>", RegexOptions.IgnoreCase);foreach(Match m in mc)
{
richTextBox2.Text += m.Groups["url"].Value + "\n";
richTextBox2.Text += m.Groups["title"].Value + "\n";
}
现在写成
Regex.Matches(content, @"<a\s[^>]*?href=([""']?)(?<url>[^'""\s]*)\1?[^>]*?title=([""']?)(?<title>[^'""\s]*)\2?[^>]*?>(?<subject>[\s\S]*?)</a>", RegexOptions.IgnoreCase);
但是我想取的内容是subject里的正文标题
但这样也好 又多了个可以提取的内容原先想如果不行就把"全替换
用了Regex.Matches(content,@"<A\s+target=_blank\s+href=(?<url>.*shtml)\s+title=(.*)>(?<subject>.*)</a>",RegexOptions.IgnoreCase);
也可以看来lxcnn正则很强啊,可以加我QQ 教教我吗?新闻采集用到正则真的很方便就是不会用啊~~
ps:595954928
http://www.regexlab.com/zh/regref.htm
match tracer 1.2有了这两个再加上大量的练习就可以了