Việc lấy các thuộc tính, thẻ img trong asp.net thường xuyên cần dùng khi bạn lập trình web asp.net. Nếu tự code tay để lấy các thuộc tính src, thẻ <img /> trong 1 chuỗi html truyền vào rất lâu và khó khăn. Để giải quyết vấn đề này ta sử dụng các hàm có sẵn trong thư viên asp.net là Regex để lấy các thẻ src, img này một cách nhanh chóng. 1. Lấy toàn bộ thẻ <img /> trong chuỗi html truyền vào asp.net. Lấy toàn bộ thẻ <img /> truyền vào bao gồm toàn bộ thuộc tình src, alt, style,.. có trong thẻ <img /> Demo: Mã: string str = "Thẻ hình ảnh <img style='width: 48%;' alt='Link Nofollow' src='https://itseovn.com/images/Link-Nofollow-Dofollow-la-gi-khac-biet-giua-Nofollow-va-Dofollow.jpg' />, <span>lấy thẻ src, thẻ hình ảnh toàn bộ trong asp.net</span>, <img style='width: 48%;' alt='Link Nofollow' src='https://itseovn.com/attachments/google-webmaster-tools-logo-jpg.26/' />"; Kêt quả lấy : Mã: <img style='width: 48%;' alt='Link Nofollow' src='https://itseovn.com/images/Link-Nofollow-Dofollow-la-gi-khac-biet-giua-Nofollow-va-Dofollow.jpg' /> Và <img style='width: 48%;' alt='Link Nofollow' src='https://itseovn.com/attachments/google-webmaster-tools-logo-jpg.26/' /> Code lấy toàn bộ thẻ img trong html asp.net Mã: using System.Text.RegularExpressions; using System.Collections.Generic; private List<string> GetImagesInHTMLString(string htmlString) { List<string> images = new List<string>(); string pattern = @"<(img)\b[^>]*>"; Regex rgx = new Regex(pattern, RegexOptions.IgnoreCase); MatchCollection matches = rgx.Matches(htmlString); for (int i = 0, l = matches.Count; i < l; i++) { if (matches[i].Value.Trim() != "") { images.Add(matches[i].Value); } } return images; } 2. Lấy toàn bộ thẻ thuộc tính SRC trong thẻ <img /> của chuỗi html truyền vào asp.net Lấy toàn bộ thuộc tính src trong thẻ <img /> truyền vào bao gồm toàn bộ thuộc tình src, alt, style,.. có trong thẻ <img /> Demo: Mã: string str = "Thẻ hình ảnh <img style='width: 48%;' alt='Link Nofollow' src='https://itseovn.com/images/Link-Nofollow-Dofollow-la-gi-khac-biet-giua-Nofollow-va-Dofollow.jpg' />, <span>lấy thẻ src, thẻ hình ảnh toàn bộ trong asp.net</span>, <img style='width: 48%;' alt='Link Nofollow' src='https://itseovn.com/attachments/google-webmaster-tools-logo-jpg.26/' />"; Kêt quả lấy : https://itseovn.com/images/Link-Nofollow-Dofollow-la-gi-khac-biet-giua-Nofollow-va-Dofollow.jpg https://itseovn.com/attachments/google-webmaster-tools-logo-jpg.26/ Code lấy toàn bộ thẻ src trong html asp.net Mã: private List<string> GetSrcImagesInHTMLString(string htmlString) { List<string> images = new List<string>(); var imgreg = Regex.Matches(htmlString, @"(?<=<img\s+[^>]*?src=(?<q>['""]))(?<url>.+?)(?=\k<q>)"); var list = new HashSet<String>(); //HashSet trùng nhau thì không lấy foreach (var img in imgreg) { if (img.ToString() != "") { images.Add(img.ToString()); } } return images; } Từ khóa tiếng anh: Regex get the src of img attribute in html asp.net, Extract image tags from HTML in C#, Regular Expression to manipulare "src " attribute in HTML <img, Get all the <img src=""> tag in html, How can i get the img element and src attribute from the html text, Using a Regular Expression to Match HTML get src img in asp.net, regex - Regular Expression to get the SRC of images in C#, Regular Expression to manipulare "src " attribute in HTML, How to get the source of Image using c# regular Expression