有一string类型,里面有一个或多个类似 <img src="http://www.xxx.com/aa.gif" border="0" width="100" alt="test"> 的字符串,有的没办法把里面的src值和alt值一一对应的取出来?说明:
这个string可能很复杂,上面只是个例子,实际中可能是
<img src="http://www.xxx.com/aa.gif" border="0" width="100" alt="test" />
<img alt="test" src="http://www.xxx.com/aa.gif" border="0" width="100">
<img src="http://www.xxx.com/aa.gif" border="0" width="100">
甚至是
<img src="http://www.xxx.com/aa.gif" border=0 vspace=5 hspace=5 alt='test' onload="javascript:if(this.width>screen.width-500)this.style.width=screen.width-500;">能作到吗?
个人觉得用正则是不可能的!
这个string可能很复杂,上面只是个例子,实际中可能是
<img src="http://www.xxx.com/aa.gif" border="0" width="100" alt="test" />
<img alt="test" src="http://www.xxx.com/aa.gif" border="0" width="100">
<img src="http://www.xxx.com/aa.gif" border="0" width="100">
甚至是
<img src="http://www.xxx.com/aa.gif" border=0 vspace=5 hspace=5 alt='test' onload="javascript:if(this.width>screen.width-500)this.style.width=screen.width-500;">能作到吗?
个人觉得用正则是不可能的!
System.Text.RegularExpressions.MatchCollection
mc = re.Matches(yourstr); foreach(Match m in mc)
{
//m.Value
Console.WriteLine("------"+m.Groups["urlstr"].Value+"---------"+m.Groups["altstr"].Value);
}
Console.ReadLine();
google/baidu 没找到
你应该通过img将来取出放入一个img对像,然后alt就是它的属性。
MatchCollection mc = Regex.Matches(yourStr, "<img\\s+(alt=\"(?<alt>.+?)\"\\s+)?src=\"(?<src>.+?)\".+?>");
foreach(Match m in mc)
{
m.Groups["alt"].Value;//alt
m.Groups["src"].Value;//src
}