有一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;">能作到吗?
个人觉得用正则是不可能的!
解决方案 »
- 在DataGridView中,如何将数据导出到EXCEL呢?
- Server Error in '/nb_flag' Application. Cannot find table 0.
- 关于泛型类转换问题
- owc11使用问题,指定的维对当前图表类型而言是无效的,这么解决?
- 如何实现.ASPX文件里的“系统退出”功能?
- 一个C#和VC接口问题
- 在类内部定义的枚举类型的数据结构,在类的外部怎么引用?(调用与定义不在同一个文件中)
- 请问哪里有Interop.Excel.dll?
- 怎么樣反射出一個實體類的所有的屬性與方法?
- 请问怎么在StatueBar的Panel上加入其它的控件?
- DataGrid控件中每一列接受不同的 keyDown 事件,在線等,望賜教
- 如何调用存储过程?
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
}