读书人

救灾跪求一正则表达式根据条件获取

发布时间: 2013-01-04 10:04:18 作者: rapoo

救急,跪求一正则表达式,根据条件获取超链接的链接字符串。
比如:<a href="http://itunes.apple.com/cn/app/the-abolition-man-the-great/id392965200?mt=8">
The Abolition of Man and The Great Divorce (by C.S. Lewis) (UNABRIDGED AUDIOBOOK) : Blackstone Audio...
</a>
请问我要获取所有链接地址以 http://itunes.apple.com/cn/app/开头的链接的链接地址的正则表达式怎么写?
我最终要获取的就是 http://itunes.apple.com/cn/app/the-abolition-man-the-great/id392965200?mt=8这个字符串
[解决办法]


void Main()
{
string html=@"<a href=""http://itunes.apple.com/cn/app/the-abolition-man-the-great/id392965200?mt=8"">
The Abolition of Man and The Great Divorce (by C.S. Lewis) (UNABRIDGED AUDIOBOOK) : Blackstone Audio...
</a>";

foreach(Match m in Regex.Matches(html,@"http://itunes.apple.com/cn/app/[^""]*"))
{
Console.WriteLine(m.Value);
}
}

/*
http://itunes.apple.com/cn/app/the-abolition-man-the-great/id392965200?mt=8
*/

[解决办法]
MatchCollection mc= Regex.Matches(str, @" <a[^> ]*href=([ ' " "]?)(? <url> [^ ' " "> \s]*)\1?[^> ]*> (? <text> [^ <]*) </a> ", RegexOptions.IgnoreCase);
foreach (Match m in mc)
{
Response.Write(m.Groups[ "url "].Value);
Response.Write(m.Groups[ "text "].Value);
}

[解决办法]
http://itunes.apple.com/cn/app/[^"'>]*

读书人网 >C#

热点推荐