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

踏平扶桑 2013-12-20 05:31:52


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++;
}
Console.Write(matches.Count.ToString());
Console.Write(js.ToString());

...全文
263 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
再来壹串 2013-12-20
  • 打赏
  • 举报
回复
引用 5 楼 5653325 的回复:
[quote=引用 3 楼 mail_yq 的回复:] 这样?

<img[^>]*title=\\"(?<title>[^"]*)\\"[^>]*src=\\"(?<src>[^"]*)\\"
貌似还是匹配一条(但是是错的,开头是开头,尾部是最后一个img的尾部了。)。[/quote]

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=\""(?<longdesc>[^""]*)";
            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++;
            } 
我这里是可以的
踏平扶桑 2013-12-20
  • 打赏
  • 举报
回复
是贪婪模式的问题。多谢多谢
踏平扶桑 2013-12-20
  • 打赏
  • 举报
回复
引用 3 楼 mail_yq 的回复:
这样?

<img[^>]*title=\\"(?<title>[^"]*)\\"[^>]*src=\\"(?<src>[^"]*)\\"
貌似还是匹配一条(但是是错的,开头是开头,尾部是最后一个img的尾部了。)。
bingoexp 2013-12-20
  • 打赏
  • 举报
回复

string pattern = "<img.*?title=\"(?<Title>.+?)\".*?src=\"(?<SRC>.+?)\".*?longdesc=\"(?<DESC>.+?)\" />";
应该是 .*?
再来壹串 2013-12-20
  • 打赏
  • 举报
回复
这样?

<img[^>]*title=\\"(?<title>[^"]*)\\"[^>]*src=\\"(?<src>[^"]*)\\"
踏平扶桑 2013-12-20
  • 打赏
  • 举报
回复
引用 1 楼 bdmh 的回复:
先说清楚你想要什么结果
我就想把各个img分组提取出来 title src 和longdesc 这个正则写的有问题,总是只能匹配出来一条。
bdmh 2013-12-20
  • 打赏
  • 举报
回复
先说清楚你想要什么结果

110,533

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧