读书人

请教正则表达式的匹配

发布时间: 2012-11-07 09:56:10 作者: rapoo

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

读书人网 >C#

热点推荐