首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

请教正则表达式的匹配

2012-11-07 
请问正则表达式的匹配string s_contentp第一张图img height225 altaa width300 src/uploa

请问正则表达式的匹配
string s_content="<p>第一张图<img height="225" alt="aa" width="300" src="/upload/UserFiles/Blue hills.jpg" />第二张图测试<img height="225" width="300" alt="bb" src="/upload/UserFiles/Winter.jpg" />得到图测试</p>
";

public void pict(string input) 
  {

  MatchCollection mc = Regex.Matches(input, @"(?<alt>(?<=<img[^>]*?alt="")[^""]+(?="")*(?<src>(?<=<img[^>]*?src="")[^""]+(?="")*)", RegexOptions.Multiline);

  foreach (Match m in mc)
  {

  Response.Write("alt: \n" + m.Groups["alt"].Value + "\n");

  Response.Write("src: \n" + m.Groups["src"].Value + "\n");

  }


  }

调用出现错误
正在分析“(?<alt>(?<=<img[^>]*?alt=")[^"]+(?=")*(?<src>(?<=<img[^>]*?src=")[^"]+(?=")*)”- ) 不足。


[解决办法]

探讨

MatchCollection mc = Regex.Matches(input, @"(?<alt>((?<=<img[^>]*?alt="")[^""]+(?="")*(?<src>(?src="")[^""]+(?="")*)", RegexOptions.Multiline);

正在分析“(?<alt>((?<=<img[^>]*?alt=")[^"]+(?=")*(?<src>(……

[解决办法]
" 要转义啊。。。

MatchCollection mc = Regex.Matches(input, @"(?i)<img\b[^>]*?alt=(['""]?)(?<alt>[^'""]+)\1[^>]*?src=(['""]?)(?<src>[^'""]+)\2[^>]*>");

热点排行