有一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;">能作到吗?
个人觉得用正则是不可能的!

解决方案 »

  1.   

    用递归,用String.Indexof(),String.Substring()一一提取,最近我的程序就是这么做的,十分不爽
      

  2.   

    mdhtml组件去解析就可以了。很方便
      

  3.   

    string  s = "(?<=<img\\s+.*src=[\"'])(?<urlstr>.+?)[\"']\\s+.*alt=[\"'](?<altstr>.+?)(?=[\"']?\\s+*[/]?>)";string b =  "(?<=<img\\s+.*alt=[\"'](?<altstr>.+?)[\"']\\s+.*src=[\"'])(?<urlstr>.+?)(?=[\"']?\\s+*[/]?>)"; Regex re = new Regex("("+s+")|("+b+")",RegexOptions.IgnoreCase);
    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();
      

  4.   

    henry3695() 好象不行<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;">用这个测试会提示错误.
      

  5.   

    ParadiseX mdhtml是什么?
    google/baidu 没找到
      

  6.   

    mshtml?
    你应该通过img将来取出放入一个img对像,然后alt就是它的属性。
      

  7.   

    string yourStr = ......;
    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
    }