直接上代码
1 public static string[] GetHtmlImageUrlList(string htmlText) 2 { 3 Regex regImg = new Regex(@"<img\b[^<>]*?\bsrc[\s\t\r\n]*=[\s\t\r\n]*[""‘]?[\s\t\r\n]*(?<imgUrl>[^\s\t\r\n""‘<>]*)[^<>]*?/?[\s\t\r\n]*>", RegexOptions.IgnoreCase); 4 5 //新建一个matches的MatchCollection对象 保存 匹配对象个数(img标签) 6 MatchCollection matches = regImg.Matches(htmlText); 7 int i = 0; 8 string[] sUrlList = matches.Count >= 3 ? new string[3] : new string[matches.Count]; 9 //遍历所有的img标签对象 10 foreach (Match match in matches) 11 { 12 //获取所有Img的路径src,并保存到数组中 13 if (i <= 2) 14 { 15 sUrlList[i++] = match.Groups["imgUrl"].Value.AddImgPathPrefix(); 16 } 17 } 18 return sUrlList; 19 }
1 string content = "<p>测试<img src=\"/Uploads/Images/UEditor/2020052417350496181.jpg\" title=\"pic.jpg\" alt=\"pic.jpg\"/></p><p><br/></p><p><img src=\"/Uploads/Images/UEditor/2020052417361071834.jpg\" title=\"pic2.jpg\" alt=\"pic2.jpg\"/></p><p><br/></p>"; 2 string[] array = Models.UtiServer.GetHtmlImageUrlList(content);