1. 程式人生 > >c#常見的正則表達式

c#常見的正則表達式

new space 手機號 etime log ram rdate string parse

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]+))*$");
}

c#常見的正則表達式