获取html里面的所有image(src属性)
/// <summary>
/// 获取html,img标签url地址
/// </summary>
/// <param name="html"></param>
/// <returns></returns>
public static string[] GetImgUrlList(string html)
{
// 定义正则表达式用来匹配 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(html);
int i = 0;
string[] sUrlList = new string[matches.Count];
// 取得匹配项列表
foreach (Match match in matches)
sUrlList[i++] = match.Groups["imgUrl"].Value;
return sUrlList;
}
正则表达式整理:
//验证电话号码
public static bool IsTelephone(string str_telephone)
{
return System.Text.RegularExpressions.Regex.IsMatch(str_telephone, @"^(\d{3,4}-)?\d{6,8}$");
}
//验证手机号码
public static bool IsHandset(string str_handset)
{
return System.Text.RegularExpressions.Regex.IsMatch(str_handset, @"^[1]+[3,5]+\d{9}");
}
//验证身份证号
public static bool IsIDcard(string str_idcard)
{
return System.Text.RegularExpressions.Regex.IsMatch(str_idcard, @"(^\d{18}$)|(^\d{15}$)");
}
//验证输入为数字
public static bool IsNumber(string str_number)
{
return System.Text.RegularExpressions.Regex.IsMatch(str_number, @"^[0-9]*$");
}
//验证邮编
public static bool IsPostalcode(string str_postalcode)
{
return System.Text.RegularExpressions.Regex.IsMatch(str_postalcode, @"^\d{6}$");
}
//验证邮箱
public static bool IsEmail(string str_Email)
{
return System.Text.RegularExpressions.Regex.IsMatch(str_Email, @"^[0-9a-z][a-z0-9\._-]{1,}@[a-z0-9-]{1,}[a-z0-9]\.[a-z\.]{1,}[a-z]$");
}
//验证手机号码+电话
public static bool IsPhone(string phone)//^(\d{3,4}-)?\d{7,8}$|(1[3-9]d{9}$)
{
//((\d{11})|^((\d{7,8})|(\d{4}|\d{3})-(\d{7,8})|(\d{4}|\d{3})-(\d{7,8})-(\d{4}|\d{3}|\d{2}|\d{1})|(\d{7,8})-(\d{4}|\d{3}|\d{2}|\d{1}))$)
//
return System.Text.RegularExpressions.Regex.IsMatch(phone, @"^1\d{10}$|^[^0]\d{6,7}$|^0\d{2,3}-?\d{7,8}$");
}