public string getImgurlAll(string content)//获取图片地址 参数:字符串,无图时默认的图片地址
{
string html = content;
string pattern = @"<IMG[^>]*?src/s*=/s*(""(?<src>[^""]+?)""|'(?<src>[^']+?)'|(?<src>[^/s>]+))[^>]*?>";
Regex regex = new Regex(pattern, RegexOptions.Singleline | RegexOptions.IgnoreCase);
Match m = regex.Match(html);
string str = "";
if (m.Success)
{
str = m.Groups[0].Value.ToString();
}
html = html.Remove(html.IndexOf(str), str.Length);
return html;
}
获取部分信息
public string getImgurl(string content, string strdef)//获取html中图片地址
{
string html = content;
string imgurl = "";
string pattern = @"<IMG[^>]*?src/s*=/s*(""(?<src>[^""]+?)""|'(?<src>[^']+?)'|(?<src>[^/s>]+))[^>]*?>";
// @"<img.*(src)=['""]?(?<link>[^'""/s])['""]?.*(//?>|[^>]*>)";
Regex regex = new Regex(pattern, RegexOptions.Singleline | RegexOptions.IgnoreCase);
foreach (Match m in regex.Matches(html))
{
imgurl += m.Groups["src"].Value.ToString();
break;
}
if (imgurl == "")
{
imgurl = strdef;
}
return imgurl;
}