初学正则,问个问题.

vfan2010 2007-10-16 05:01:00
            string str = @" <img src=http://images.gg-art.com/auction/images1/13/13325.jpg >
<img src=http://images.gg-art.com/auction/images1/13/13326.jpg >
<img src=http://images.gg-art.com/auction/images1/13/13327.jpg >";
Regex r = new Regex(@"<img[\s]*src[\s]*=(?<src>http://[\w\W]*.jpg)[\s]*>$", RegexOptions.Multiline);
string oStr = "";
if (r.IsMatch(str))
{
oStr += r.Match(str).Groups["src"].Value.ToString()+",";
}
Response.Write(oStr);

我是想把三个img的scr都输出来.如果只出现了一次就是正确的.
...全文
87 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
王集鹄 2007-10-16
  • 打赏
  • 举报
回复
$这个表示最末的位置,而最末的位置只有一个哈...
没有看到循环哈-_-!!!!!!

参考如下代码:
string str = 
@"<img src=http://images.gg-art.com/auction/images1/13/13325.jpg >
<img src=http://images.gg-art.com/auction/images1/13/13326.jpg >
<img src=http://images.gg-art.com/auction/images1/13/13327.jpg >";
Regex r = new Regex(@"<img\s+src\s*=\s*(?<src>[^\>]*?)\s*>");
string oStr = "";
foreach(Match vMatch in r.Matches(str))
{
oStr += vMatch.Result("${src}") + ",";
}
JGood 2007-10-16
  • 打赏
  • 举报
回复
其实楼主的html代码不是格式良好的,src的属性值最好加上引号。如果属性有引号,下面的正则就出不起作用了

string str = @" <img src=http://images.gg-art.com/auction/images1/13/13325.jpg > <img src=http://images.gg-art.com/auction/images1/13/13326.jpg > <img src=http://images.gg-art.com/auction/images1/13/13327.jpg >";
string pattern = @"<img\ssrc=([^\s]*).*?>";
//Regex r = new Regex(@"<img[\s]*src[\s]*=(?<src>http://[\w\W]*.jpg)[\s]*>$", RegexOptions.Multiline);
Regex r = new Regex(pattern, RegexOptions.IgnoreCase | RegexOptions.Multiline);
//string oStr = "";
bool isMatch = r.IsMatch(str);

if (isMatch)
{
MatchCollection mc = r.Matches(str);
foreach (Match m in mc)
{
Response.Write(m.Groups[1].Value + "<br />");
}
}
vfan2010 2007-10-16
  • 打赏
  • 举报
回复
UP.
vfan2010 2007-10-16
  • 打赏
  • 举报
回复
你的不行啊,找不到匹配
windboyzsj 2007-10-16
  • 打赏
  • 举报
回复
修改了一下你的表达式
<img[\s]*src[\s]*=(?<src>http://[\w/\.-]*\.jpg)[\s]*>
vfan2010 2007-10-16
  • 打赏
  • 举报
回复
UP.

110,538

社区成员

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

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

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