日期:2014-05-17  浏览次数:20399 次

正则问题,为何只能匹配到一条数据

string sss = "<img width=\"499\" height=\"323\" title=\"asdfsadfasdf\" alt=\"\" src=\"http://images.xxxxxx.cn/2013/0522/20130522113854351.jpg\" border=\"0\" longdesc=\"asdfasdfsadfasdf\" /><img width=\"499\" height=\"323\" title=\"asdfsadfasdf\" alt=\"\" src=\"http://images.xxxxxx.cn/2013/0522/20130522113854351.jpg\" border=\"0\" longdesc=\"asdfasdfsadfasdf\" /><img width=\"499\" height=\"323\" title=\"asdfsadfasdf\" alt=\"\" src=\"http://images.xxxxxx.cn/2013/0522/20130522113854351.jpg\" border=\"0\" longdesc=\"asdfasdfsadfasdf\" /><img width=\"499\" height=\"323\" title=\"asdfsadfasdf\" alt=\"\" src=\"http://images.xxxxxx.cn/2013/0522/20130522113854351.jpg\" border=\"0\" longdesc=\"asdfasdfsadfasdf\" /><img width=\"499\" height=\"323\" title=\"asdfsadfasdf\" alt=\"\" src=\"http://images.xxxxxx.cn/2013/0522/20130522113854351.jpg\" border=\"0\" longdesc=\"asdfasdfsadfasdf\" />";

        string pattern = "<img.*title=\"(?<Title>.+?)\".*src=\"(?<SRC>.+?)\".*longdesc=\"(?<DESC>.+?)\" />";
        string title = string.Empty;
        string src = string.Empty;
        string longdesc = string.Empty;
        StringBuilder js = new StringBuilder();

        Regex rr = new Regex(pattern, RegexOptions.IgnoreCase);
        MatchCollection matches = rr.Matches(sss);
        int i = 1;
        foreach (Match match in matches)
        {
            title = match.Groups["Title"].Value;
            src = match.Groups["SRC"].Value;
            longdesc = match.Groups["DESC"].Value;

            js.Append("<li>");
            js.Append("<a href=\"javascript:void(0);\">");
            js.Append("<img src=\"\" width=\"120\" height=\"80\" alt=\"" + title + "\" />");
            js.Append("</a>");
            js.Append("</li>");
            i++;
        }
        Response.Write(matches.Count.ToString()+"<br />");
        Response.Write(js.ToString());

只能匹配到字符串的第一个img,后面的匹配不到,请高手做下修改。谢谢!