匹配这个图片名BG-header.jpg<img alt="" src="/WebUI/Background/images/BG-header.jpg" style="width:150px;height:120px; " />

解决方案 »

  1.   


     string str = "<img alt=\"\" src=\"/WebUI/Background/images/BG-header.jpg\" style=\"width:150px;height:120px; \" />";
     Regex re = new Regex("(?<=src=\\\").*?(?=\\\")", RegexOptions.None);
     str = re.Match(str).Value.Substring(re.Match(str).Value.LastIndexOf('/') + 1);//结果:BG-header.jpg
      

  2.   


     public string Image(string str)
        {
            string result = "";
            Regex re = new Regex("(?<=src=\\\").*?(?=\\\")", RegexOptions.None);
            MatchCollection mc = re.Matches(str);
            foreach (Match ma in mc)
            {
               result += ma.Value + ",";
               if (result.Length > 0)
               {
                  break;
               }
            }
            return result.TrimEnd(',');
        } 这方法有问题,输出/WebUI/Background/images/BG-header.jpg
      

  3.   

    补充下
    <img alt="" src="/WebUI/Background/images/BG-header.jpg" style="width:150px;height:120px; " />
    前后有很多内容~
      

  4.   

                string str = "<img alt=\"\" src=\"/WebUI/Background/images/BG-header.jpg\" style=\"width:150px;height:120px; \" />";
                Regex reg = new Regex(@"(?i)<img[^>]*?src=(['""]?).*/((?:(?!\1).)*?)\1[^>]*?>");
                Response.Write(reg.Match(str).Groups[2].Value);
    //BG-header.jpg