C#正则表达式提取HTML中IMG标签的URL地址 .
/// <summary>
/// 取得HTML中所有圖片的 URL。
/// </summary>
/// <param name="sHtmlText">HTML代碼</param>
/// <returns>圖片的URL列表</returns>
public static string[] GetHtmlImageUrlList(string sHtmlText)
{
? // 定義正則表達(dá)式用來(lái)匹配 img 標(biāo)簽
? 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];
?
? // 取得匹配項(xiàng)列表
? foreach (Match match in matches)
? sUrlList[i++] = match.Groups["imgUrl"].Value;
? return sUrlList;
}
轉(zhuǎn)載于:https://www.cnblogs.com/wangchuang/archive/2012/05/23/2515282.html
總結(jié)
以上是生活随笔為你收集整理的C#正则表达式提取HTML中IMG标签的URL地址 .的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: PHP学习系列(1)——字符串处理函数(
- 下一篇: [收藏转载]C# GDI+ 简单绘图(一