c#常见的正则表达式
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
- C#正则表达式常见使用收集
- C#(正则表达式)Regex类的一些常见使用方式
- C#常见正则表达式
- C# 常见Url操作实例(一)、正则表达式匹配URl
- C#常见正则表达式
- C#(正则表达式)Regex类的一些常见使用方式
- 常见正则表达式
- C#中利用正则表达式检测文件路径的合法性
- c# 中关于正则表达式的用法总结(很全面)
- C#正则表达式 — 语言元素
- C#正则表达式编程(二)转致周公
- 常见正则表达式总结
- C#中的正则表达式 转
- [转]C#正则表达式小结
- C# 正则表达式示例
- C# .Net使用正则表达式去除HTML标记和空格
- asp.net(C#)正则表达式实例
- C# 正则表达式语法的相关规则
- C#正则表达式提取HTML中IMG标签的SRC地址
- C# 使用正则表达式判断字符串是否由英文或数字组成