日期:2014-05-20  浏览次数:20759 次

正则表达式的问题
<li><a href="http://www.itravelqq.com/2011/0407/126739.html">四五月成高端蜜月游旺季私密</a> <i>[04-0708:41]</i></li><li><a href="http://www.itravelqq.com/2011/0407/126730.html">去年10万客赴西</a><i>[04-0708:34]</i></li><li>

我只想取出herf里的值,与日期 要怎么写啊?

------解决方案--------------------
C# code

void Main()
{
           
      string html=@"<li><a href=""http://www.itravelqq.com/2011/0407/126739.html"">四五月成高端蜜月游旺季私密</a> <i>[04-0708:41]</i></li><li><a href=""http://www.itravelqq.com/2011/0407/126730.html"">去年10万客赴西</a><i>[04-0708:34]</i></li><li>";
    foreach(Match m in Regex.Matches(html,@"(?i)<a[^>]*href=(['""]?)(?<href>[^'""]+)\1[^>]*>[^<]*</a>\s*<i>\[(?<time>[^\]]+)\]"))
    {
        Console.WriteLine(string.Format("href: {0}\ttime: {1}",m.Groups["href"].Value,m.Groups["time"].Value));
    }
}

/*
href: http://www.itravelqq.com/2011/0407/126739.html    time: 04-0708:41
href: http://www.itravelqq.com/2011/0407/126730.html    time: 04-0708:34
*/

------解决方案--------------------
string str = @"<li><a href=""http://www.itravelqq.com/2011/0407/126739.html"">四五月成高端蜜月游旺季私密</a> <i>[04-0708:41]</i></li><li><a href=""http://www.itravelqq.com/2011/0407/126730.html"">去年10万客赴西</a><i>[04-0708:34]</i></li><li>";

MatchCollection mc = Regex.Matches(str, @"(?<=href=\""?)([^\""]+)[^\[]+\[([^\]]+)");
foreach (Match m in mc)
{
Console.WriteLine(m.Groups[1]);
Console.WriteLine(m.Groups[2]);
}