c# c#正则表达式 取指定<img id=id="lens_img" 的图片连接地址

解决方案 »

  1.   

    (?is)<img[^>]*?id="lens_img"[^>]*?href=(["']?)(?<href>[^"']*?)\1[^>]*?/>
    取Groups["href"].Value
      

  2.   

    string str = "<img id=\"lens_img\" src=\"1.jpg\">";                var result = Regex.Match(str, @"(?i)<img[^>]*?id=(['""]?)lens_img\1[^>]*?src=(['""]?)(?<src>[^'""]+?)\2[^>]*?>").Groups["src"].Value;
                    //1.jpg
      

  3.   

     
    给你段提取html元素的img标签的连接地址代码希望对你有用public static string[] GetHtmlImageUrlList(string sHtmlText)
            {            // 定义正则表达式用来匹配 img 标签             Regex regImg = new Regex(@"<img\b[^<>]*?\bsrc[\s\t\r\n]*=[\s\t\r\n]*[""']?[\s\t\r\n]*(?<src>[^\s\t\r\n""'<>]*)[^<>]*?/?[\s\t\r\n]*>", RegexOptions.IgnoreCase);            // 搜索匹配的字符串             MatchCollection matches = regImg.Matches(sHtmlText);            int i = 0;            string[] sUrlList = new string[matches.Count];            // 取得匹配项列表             foreach (Match match in matches)                sUrlList[i++] = match.Groups["src"].Value;            return sUrlList;        }