C#正则表达式提取HTML中IMG标签的URL地址 .
/// <summary>
/// 取得HTML中所有圖片的 URL。
/// </summary>
/// <param name="sHtmlText">HTML代碼</param>
/// <returns>圖片的URL列表</returns>
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]*(?<imgUrl>[^\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["imgUrl"].Value;
? return sUrlList;
}
轉載于:https://www.cnblogs.com/wangchuang/archive/2012/05/23/2515282.html
總結
以上是生活随笔為你收集整理的C#正则表达式提取HTML中IMG标签的URL地址 .的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: PHP学习系列(1)——字符串处理函数(
- 下一篇: 梦到电线烧起来了是什么意思