C# 利用正则表达式获取富文本框中所有图片路劲
直接上代码
public static string[] GetHtmlImageUrlList(string htmlText) { 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); //新建一个matches的MatchCollection对象 保存 匹配对象个数(img标签) MatchCollection matches = regImg.Matches(htmlText); int i = 0; string[] sUrlList = matches.Count >= 3 ? new string[3] : new string[matches.Count]; //遍历所有的img标签对象 foreach (Match match in matches) { //获取所有Img的路径src,并保存到数组中 if (i <= 2) { sUrlList[i++] = match.Groups["imgUrl"].Value.AddImgPathPrefix(); } } return sUrlList; }
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);
相关推荐
flyingssky 2020-08-18
Lzs 2020-10-23
聚合室 2020-11-16
零 2020-09-18
Justhavefun 2020-10-22
jacktangj 2020-10-14
ChaITSimpleLove 2020-10-06
Andrea0 2020-09-18
周游列国之仕子 2020-09-15
afanti 2020-09-16
88234852 2020-09-15
YClimb 2020-09-15
风雨断肠人 2020-09-04
卖口粥湛蓝的天空 2020-09-15
stulen 2020-09-15
pythonxuexi 2020-09-06
abfdada 2020-08-26