常用正則表達判别式
代碼:

//驗證電話号碼
public bool IsTelephone(string str_telephone)
{
return System.Text.RegularExpressions.
Regex.IsMatch(str_telephone, @"^(\d{3,4}-)?\d{6,8}$");
}
//驗證密碼
public bool IsPassword(string str_password)
{
return System.Text.RegularExpressions.
Regex.IsMatch(str_password, @"[A-Za-z]+[0-9]");
}
//驗證郵政編碼
public bool IsPostalcode(string str_postalcode)
{
return System.Text.RegularExpressions.
Regex.IsMatch(str_postalcode, @"^\d{6}$");
}
//驗證手機号碼
public bool IsHandset(string str_handset)
{
return System.Text.RegularExpressions.Regex.
IsMatch(str_handset, @"^[1][3-5]\d{9}$");
}
//驗證身份證
public bool IsIDcard(string str_idcard)
{
return System.Text.RegularExpressions.Regex.
IsMatch(str_idcard, @"(^\d{18}$)|(^\d{15}$)");
}
//驗證小數格式
public bool IsDecimal(string str_decimal)
{
return System.Text.RegularExpressions.Regex.
IsMatch(str_decimal, @"^[0-9]+\.[0-9]{2}$");
}
//驗證月份
public bool IsMonth(string str_Month)
{
return System.Text.RegularExpressions.Regex.
IsMatch(str_Month, @"^(0?[[1-9]|1[0-2])$");
}
//驗證天數
public bool IsDay(string str_day)
{
return System.Text.RegularExpressions.Regex.
IsMatch(str_day, @"^((0?[1-9])|((1|2)[0-9])|30|31)$");
}
//驗證是否為數字
public bool IsNumber(string str_number)
{
return System.Text.RegularExpressions.Regex.
IsMatch(str_number, @"^[0-9]*$");
}
//驗證密碼長度
public bool IsPasswLength(string str_Length)
{
return System.Text.RegularExpressions.Regex.
IsMatch(str_Length, @"^\d{6,18}$");
}
//驗證正整數
public bool IsIntNumber(string str_intNumber)
{
return System.Text.RegularExpressions.Regex.
IsMatch(str_intNumber, @"^\+?[1-9][0-9]*$");
}
//驗證大小寫
public bool IsUpChar(string str_UpChar)
{
return System.Text.RegularExpressions.Regex.
IsMatch(str_UpChar, @"^[A-Z]+$");
}
public bool IsLowerChar(string str_UpChar)
{
return System.Text.RegularExpressions.Regex.
IsMatch(str_UpChar, @"^[a-z]+$");
}
//驗證是否為字母
public bool IsLetter(string str_Letter)
{
return System.Text.RegularExpressions.Regex.
IsMatch(str_Letter, @"^[A-Za-z]+$");
}
//驗證是否為中文
public bool IsChinese(string str_chinese)
{
return System.Text.RegularExpressions.Regex.
IsMatch(str_chinese, @"^[\u4e00-\u9fa5]{1,}$");
}
//驗證郵箱
public bool IsEmail(string str_Email)
{
return System.Text.RegularExpressions.Regex.IsMatch(str_Email,
@"^(([\w\.]+)@(([[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}))|((\w+\.?)+)@([a-zA-Z]{2,4}|[0-9]{1,3})(\.[a-zA-Z]{2,4}))$");
}
//驗證IP
public bool IPCheck(string IP)
{
string num = @"(25[0-5]|2[0-4]\d|[0-1]\d{2}|[1-9]?\d)";
return Regex.IsMatch(IP,
("^" + num + "\\." + num + "\\." + num + "\\." + num + "$"));
}
//驗證Url
public bool IsUrl(string str_url)
{
return System.Text.RegularExpressions.Regex.IsMatch(str_url,
@"http(s)?://([\w-]+\.)+[\w-]+(/[\w- ./?%&=]*)?");
}
常用正則小結
本文來自部落格園,作者:農碼一生,轉載請注明原文連結:https://www.cnblogs.com/wml-it/p/14844365.html
技術的發展日新月異,随着時間推移,無法保證本部落格所有内容的正确性。如有誤導,請大家見諒,歡迎評論區指正! 個人開源代碼連結: GitHub:https://github.com/ITMingliang
Gitee:https://gitee.com/mingliang_it
GitLab:https://gitlab.com/ITMingliang
進開發學習交流群: