问题描述
这样的正则怎么写啊!获取标题和URL获取标题和连接杨若兮公开情感故事称和刘威“可以做朋友”http://www.hljnews.cn/xw_whyl/system/2008/02/01/010117839.shtml感人至深的男女情感故事http://gentleman.zgjrw.com/News/2008129/man/219847457600.html<SPAN>• <Ahref="http://www.hljnews.cn/xw_whyl/system/2008/02/01/010117839.shtml"target=_blankrel=&a=5&pn=24>杨若兮公开<FONTcolor=#c60a00>情感故事</FONT>称和刘威“可以做朋友”</A> <FONTclass=gsize=1>黑龙江新闻网2008-2-110:19</FONT><BR></SPAN><SPAN>• <Ahref="http://gentleman.zgjrw.com/News/2008129/man/219847457600.html"target=_blankrel=&a=5&pn=25>感人至深的男女<FONTcolor=#c60a00>情感故事</FONT>(1)</A> <FONTclass=gsize=1>中国金融网2008-1-2911:32</FONT> <A
解决方案
解决方案二:
MatchCollectionmc=Regex.Matches(str,@"<a[^>]*href=""(?<url>[^""]*)""[^>]*>(?<text>[sS]*?)</a>",RegexOptions.IgnoreCase);foreach(Matchminmc){MessageBox.Show(Regex.Replace(m.Groups["text"].Value,@"<[^>]*>","")+"n"+m.Groups["url"].Value);}
解决方案三:
好象不可以
解决方案四:
textBox1.Text内容就是你给的例子,实现:usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Data;usingSystem.Drawing;usingSystem.Text;usingSystem.Text.RegularExpressions;usingSystem.Windows.Forms;namespaceWindowsApplication1{publicpartialclassForm1:Form{publicForm1(){InitializeComponent();}privatevoidbutton1_Click(objectsender,EventArgse){textBox1.Text=Regex.Replace(textBox1.Text,"<font[^<>]*>|</font>","",RegexOptions.IgnoreCase);MatchCollectionmc=Regex.Matches(textBox1.Text,@"<as*href=""(?<url>http[^""]+)""[^<>]*>(?<title>[^<>]*)</a>",RegexOptions.IgnoreCase);foreach(Matchminmc){MessageBox.Show(m.Groups["url"].Value+"rn"+m.Groups["title"].Value);}}}}
应该说这样无法得到你要的内容,如果你要去除空格,可以考虑在结果那里m.Groups["title"].Value.Replace("","")
解决方案五:
怎么个不可以,倒是说说看usingSystem.Text.RegularExpressions;staticvoidMain(string[]args){stringtest=@"<SPAN>• <Ahref=""http://www.hljnews.cn/xw_whyl/system/2008/02/01/010117839.shtml""target=_blankrel=&a=5&pn=24>杨若兮公开<FONTcolor=#c60a00>情感故事</FONT>称和刘威“可以做朋友”</A> <FONTclass=gsize=1>黑龙江新闻网2008-2-110:19</FONT><BR></SPAN><SPAN>• <Ahref=""http://gentleman.zgjrw.com/News/2008129/man/219847457600.html""target=_blankrel=&a=5&pn=25>感人至深的男女<FONTcolor=#c60a00>情感故事</FONT>(1)</A> <FONTclass=gsize=1>中国金融网2008-1-2911:32</FONT> <A";MatchCollectionmc=Regex.Matches(test,@"<a[^>]*href=""(?<url>[^""]*)""[^>]*>(?<text>[sS]*?)</a>",RegexOptions.IgnoreCase);foreach(Matchminmc){Console.WriteLine(Regex.Replace(m.Groups["text"].Value,@"<[^>]*>",""));//标题Console.WriteLine(m.Groups["url"].Value); //URL}Console.ReadLine();}
输出:杨若兮公开情感故事称和刘威“可以做朋友”http://www.hljnews.cn/xw_whyl/system/2008/02/01/010117839.shtml感人至深的男女情感故事(1)http://gentleman.zgjrw.com/News/2008129/man/219847457600.html