您的位置:首页 > 编程语言 > C#

c#常见的正则表达式

2020-07-14 06:08 549 查看

 

public static class ValidateHelper
{
/// <summary>
/// 验证是否为Email
/// </summary>
/// <param name="strEmail">验证字符串</param>
/// <returns>true-是,false-否</returns>
public static bool IsEmail(this string strEmail)
{
return Regex.IsMatch(strEmail, @"^\w+((-w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$");
}

/// <summary>
/// 验证是否为正数
/// </summary>
/// <param name="strNum">验证字符串</param>
/// <returns>true-是,false-否</returns>
public static bool IsUInt(this string strNum)
{
return Regex.IsMatch(strNum, @"^[0-9]\d*|0$");
}

/// <summary>
/// 验证是否为整数
/// </summary>
/// <param name="strNum">验证字符串</param>
/// <returns>true-是,false-否</returns>
public static bool IsInt(this string strNum)
{
return Regex.IsMatch(strNum, @"^-?[1-9]\d*$");
}

/// <summary>
/// 验证是否为小数
/// </summary>
/// <param name="strNum">验证字符串</param>
/// <returns>true-是,false-否</returns>
public static bool IsFloat(this string strNum)
{
return Regex.IsMatch(strNum, @"^-?([1-9]\d*|0(?!\.0+$))\.\d+?$");
}

/// <summary>
/// 验证是否为正的小数
/// </summary>
/// <param name="strNum">验证字符串</param>
/// <returns>true-是,false-否</returns>
public static bool IsUFloat(this string strNum)
{
return Regex.IsMatch(strNum, @"^[1-9]\d*.\d*|0.\d*[1-9]\d*$");
}

/// <summary>
/// 验证是否为日期(匹配规则为:2013.12.23)
/// </summary>
/// <param name="strDate">验证字符串</param>
/// <returns>true-是,false-否</returns>
public static bool IsDate(this string strDate)
{
DateTime dt = DateTime.MinValue;
return DateTime.TryParse(strDate, out dt);
}

/// <summary>
/// 验证是否为大写字母
/// </summary>
/// <param name="str">验证字符串</param>
/// <returns>true-是,false-否</returns>
public static bool IsUpper(this string str)
{
return Regex.IsMatch(str, @"^[A-Z]+$");
}

/// <summary>
/// 验证是否为小写字母
/// </summary>
/// <param name="str">验证字符串</param>
/// <returns>true-是,false-否</returns>
public static bool IsLower(this string str)
{
return Regex.IsMatch(str, @"^[a-z]+$");
}

/// <summary>
/// 验证是否为色码值
/// </summary>
/// <param name="strColor">验证字符串</param>
/// <returns>true-是,false-否</returns>
public static bool IsColorCode(this string strColor)
{
return Regex.IsMatch(strColor, @"^#[a-fA-F0-9]{6}$");
}

/// <summary>
/// 验证是否为手机号码
/// </summary>
/// <param name="strMobile">验证字符串</param>
/// <returns>true-是,false-否</returns>
public static bool IsMobile(this string strMobile)
{
return Regex.IsMatch(strMobile, @"^(1(([0-9][0-9])|(47)|[8][012356789]))\d{8}$");
}

/// <summary>
/// 验证是否为座机号码
/// </summary>
/// <param name="strPhone">验证字符串</param>
/// <returns>true-是,false-否</returns>
public static bool IsPhone(this string strPhone)
{
return Regex.IsMatch(strPhone, @"^0\d{2,3}-\d{5,9}|0\d{2,3}-\d{5,9}$");
}

/// <summary>
/// 验证是否为IP地址
/// </summary>
/// <param name="strIPAddress">验证字符串</param>
/// <returns>true-是,false-否</returns>
public static bool IsIP(this string strIPAddress)
{
return Regex.IsMatch(strIPAddress, @"^((?:(?:25[0-5]|2[0-4]\d|[01]?\d?\d)\.){3}(?:25[0-5]|2[0-4]\d|[01]?\d?\d))$");
}

/// <summary>
/// 验证是否为有效的登录密码(6到16位任意数字与字母组合)
/// </summary>
/// <param name="strPwd">验证字符串</param>
/// <returns>true-是,false-否</returns>
public static bool IsLoginPwd(this string strPwd)
{
// var regex = new Regex(@"
// (?=.*\d) #必须包含数字
// (?=.*[a-zA-Z]) #必须包含小写或大写字母
// .{6,16} #至少6个字符,最多16个字符
// ", RegexOptions.Multiline | RegexOptions.IgnorePatternWhitespace);

var regex = new Regex(@"^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{6,16}$", RegexOptions.Multiline | RegexOptions.IgnorePatternWhitespace);
return regex.IsMatch(strPwd);
}

/// <summary>
/// 验证是否为中文
/// </summary>
/// <param name="strCn">验证字符串</param>
/// <returns>true-是,false-否</returns>
public static bool IsChinese(this string strCn)
{
return Regex.IsMatch(strCn, @"^[\u4e00-\u9fa5]+$");
}

/// <summary>
/// 验证是否为中文姓名
/// </summary>
/// <param name="strCnName">验证字符串</param>
/// <returns>true-是,false-否</returns>
public static bool IsCnName(this string strCnName)
{
return Regex.IsMatch(strCnName, @"^[\u4E00-\u9FA5]+(?:((·|\.|\.)[\u4E00-\u9FA5]+))*$");
}

转载于:https://www.cnblogs.com/ycl258/p/7808257.html

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: