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

请问正则表达式的匹配
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[^>]*>");