大家帮忙写个正则 本帖最后由 jasonliangbiz 于 2010-05-23 11:18:00 编辑 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 src="(?'url'/UpFiles/[^"]*)"结果是一个数组,如果中间要加|||的话,可以用for循环了去加上去. try... Regex reg = new Regex(@"(?i)<img[^>]*?\ssrc=(['""]?)(?<src>/UpFiles/[^'""\s>]+)\1[^>]*>"); MatchCollection mc = reg.Matches(yourStr); foreach (Match m in mc) { richTextBox2.Text += m.Groups["src"].Value + "\n"; } 这个匹配后连http://www.163.com/UpFiles/sy/444.jpg" />中的/UpFiles/sy/444.jpg也匹配上去了.不需要匹配这个.谢谢kkbac 经测试,我这个不匹配http://www.163.com/UpFiles/sy/444.jpg 忽略大小写,在.NET中等价于RegexOptions.IgnoreCase 哈哈,学习了.如果我想匹配111.jpg 222.jpg 333.jpg也就是把原来匹配的去掉前面部分.只留图片的文件名.又如何写呢? try... Regex reg = new Regex(@"(?i)<img[^>]*?\ssrc=(['""]?)/UpFiles/([^'""\s>/]*/)*(?<name>[^'""\s>.]+\.[^'""\s>]+)\1[^>]*>"); MatchCollection mc = reg.Matches(yourStr); foreach (Match m in mc) { richTextBox2.Text += m.Groups["name"].Value + "\n"; } new regex(@"....", RegexOptions.Singleline);试试. 给下你的测试代码 string test = @"<img onload=javascript:DrawImage(this); alt='111' height=""640"" width=""480"" border=""0"" src=""/UpFiles/sy/111.jpg"" /><img onload=javascript:DrawImage(this); alt='222' height=""640"" width=""480"" border=""0"" src=""/UpFiles/sy/222.jpg"" /><img onload=javascript:DrawImage(this); alt='333' height=""640"" width=""480"" border=""0"" src=""/UpFiles/sy/333.jpg"" /><img onload=javascript:DrawImage(this); alt='444' height=""450"" width=""600"" border=""0"" src=""http://www.163.com/UpFiles/sy/444.jpg"" /><img onload=javascript:DrawImage(this); alt='555' height=""640"" width=""480"" border=""0"" src=""http://www.163.com/UpFiles/sy/UpFiles/sy/555.jpg"" />"; Regex reg = new Regex(@"(?i)<img[^>]*?\ssrc=(['""]?)/UpFiles/([^'""\s>/]*/)*(?<name>[^'""\s>.]+\.[^'""\s>]+)\1[^>]*>"); MatchCollection mc = reg.Matches(test); foreach (Match m in mc) { richTextBox2.Text += m.Groups["name"].Value + "\n"; }/*-------输出---------111.jpg222.jpg333.jpg*/ 哈哈.可以了.原来是我这里 m.Groups["src"].Value应该是m.Groups["name"].Value + "\n";非常感谢! C+如何调用JS方法 [请教]请教一下asp.net多语言系统的解决方案 labview中的列表控件的使用 如何避免GridView换页时执行RowCommand()事件 如何注册第三方控件? 框架问题 谁能帮我区分了下Application,Cache,Session变量之间的区别 哪里有asp.net(vb.net)的新闻系统啊?要有源码提供 报表查看器 Web 控件 HTTP 处理程序尚未在应用程序的 web.config 文件中注册 如何把一个字符串中的内容放到CheckBoxList中? ASP 有哪个好的教程? ADO.net能对存储过程,进行事务处理么? 回滚 提交
MatchCollection mc = reg.Matches(yourStr);
foreach (Match m in mc)
{
richTextBox2.Text += m.Groups["src"].Value + "\n";
}
不需要匹配这个.
谢谢kkbac
经测试,我这个不匹配http://www.163.com/UpFiles/sy/444.jpg
忽略大小写,在.NET中等价于RegexOptions.IgnoreCase
如果我想匹配111.jpg 222.jpg 333.jpg
也就是把原来匹配的去掉前面部分.只留图片的文件名.
又如何写呢?
MatchCollection mc = reg.Matches(yourStr);
foreach (Match m in mc)
{
richTextBox2.Text += m.Groups["name"].Value + "\n";
}
<img onload=javascript:DrawImage(this); alt='111' height=""640"" width=""480"" border=""0"" src=""/UpFiles/sy/111.jpg"" />
<img onload=javascript:DrawImage(this); alt='222' height=""640"" width=""480"" border=""0"" src=""/UpFiles/sy/222.jpg"" />
<img onload=javascript:DrawImage(this); alt='333' height=""640"" width=""480"" border=""0"" src=""/UpFiles/sy/333.jpg"" />
<img onload=javascript:DrawImage(this); alt='444' height=""450"" width=""600"" border=""0"" src=""http://www.163.com/UpFiles/sy/444.jpg"" />
<img onload=javascript:DrawImage(this); alt='555' height=""640"" width=""480"" border=""0"" src=""http://www.163.com/UpFiles/sy/UpFiles/sy/555.jpg"" />
";
Regex reg = new Regex(@"(?i)<img[^>]*?\ssrc=(['""]?)/UpFiles/([^'""\s>/]*/)*(?<name>[^'""\s>.]+\.[^'""\s>]+)\1[^>]*>");
MatchCollection mc = reg.Matches(test);
foreach (Match m in mc)
{
richTextBox2.Text += m.Groups["name"].Value + "\n";
}
/*-------输出---------
111.jpg
222.jpg
333.jpg
*/
应该是m.Groups["name"].Value + "\n";
非常感谢!