C# 常用正则表达式
/// <summary>
/// 判断是否为正确的IP地址,IP范围(0.0.0.0~255.255.255)
/// </summary>
/// <param name="ip">需验证的IP地址</param>
/// <returns></returns>
public bool IsIP(String ip)
{
return System.Text.RegularExpressions.Regex.Match(ip, @"^(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[0-9])$").Success;
}
/// <summary>
/// 判断是否为正确的MAC地址,地址中间不能有分隔符,如:“-”、“:”等。
/// </summary>
/// <param name="mac">需验证的MAC地址</param>
/// <returns></returns>
public bool IsMAC(String mac)
{
return System.Text.RegularExpressions.Regex.Match(mac, "^([0-9a-fA-F]{2})(([0-9a-fA-F]{2}){5})$").Success;
}
public bool IsMail(string mail)
{
return System.Text.RegularExpressions.Regex.Match(mail, @"^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$").Success;
}
/// <summary>
/// 是否为Double类型
/// </summary>
/// <param name="expression"></param>
/// <returns></returns>
public static bool IsDouble(object expression)
{
if (expression != null)
{
return Regex.IsMatch(expression.ToString(), @"^([0-9])[0-9]*(\.\w*)?$");
}
return false;
}
/// 判断是否为正确的IP地址,IP范围(0.0.0.0~255.255.255)
/// </summary>
/// <param name="ip">需验证的IP地址</param>
/// <returns></returns>
public bool IsIP(String ip)
{
return System.Text.RegularExpressions.Regex.Match(ip, @"^(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[0-9])$").Success;
}
/// <summary>
/// 判断是否为正确的MAC地址,地址中间不能有分隔符,如:“-”、“:”等。
/// </summary>
/// <param name="mac">需验证的MAC地址</param>
/// <returns></returns>
public bool IsMAC(String mac)
{
return System.Text.RegularExpressions.Regex.Match(mac, "^([0-9a-fA-F]{2})(([0-9a-fA-F]{2}){5})$").Success;
}
public bool IsMail(string mail)
{
return System.Text.RegularExpressions.Regex.Match(mail, @"^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$").Success;
}
/// <summary>
/// 是否为Double类型
/// </summary>
/// <param name="expression"></param>
/// <returns></returns>
public static bool IsDouble(object expression)
{
if (expression != null)
{
return Regex.IsMatch(expression.ToString(), @"^([0-9])[0-9]*(\.\w*)?$");
}
return false;
}