一些验证的正则表达式
1.邮箱匹配
/// <summary> /// 匹配邮箱是否合法 /// </summary> /// <param name="source">待匹配字符串</param> /// <returns>匹配结果true是邮箱反之不是邮箱</returns> public bool CheckEmailByString(string source) { Regex rg = new Regex("^\\s*([A-Za-z0-9_-]+(\\.\\w+)*@(\\w+\\.)+\\w{2,5})\\s*$", RegexOptions.IgnoreCase); return rg.IsMatch(source); }
2.URL匹配
/// <summary> /// 匹配URL是否合法 /// </summary> /// <param name="source">待匹配字符串</param> /// <returns>匹配结果true是URL反之不是URL</returns> public bool CheckURLByString(string source) { Regex rg = new Regex(@"^(https?|s?ftp):\/\/(((([a-z]|\d|-|\.|_|~|
[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:)*@)?
(((\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.
(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5]))|
((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|
[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|
[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)
+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])
([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|
[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?)(:\d*)?)(\/((([a-z]|\d|-|\.|_|~|
[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)+
(\/(([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|
[!\$&'\(\)\*\+,;=]|:|@)*)*)?)?(\?((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])
|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)|[\uE000-\uF8FF]|\/|\?)*)?(#((([a-z]|\d|-|\.|_|~|
[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)|\/|\?)*)?$" , RegexOptions.IgnoreCase); return rg.IsMatch(source); }
3.日期匹配
/// <summary> /// 匹配日期是否合法 /// </summary> /// <param name="source">待匹配字符串</param> /// <returns>匹配结果true是日期反之不是日期</returns> public bool CheckDateByString(string source) { Regex rg = new Regex(@"^(\d{4}[\/\-](0?[1-9]|1[0-2])[\/\-]((0?[1-9])|((1|2)[0-9])|30|31))|((0?[1-9]|1[0-2])[\/\-]((0?[1-9])|((1|2)[0-9])|30|31)[\/\-]\d{4})$"); return rg.IsMatch(source); }
4.邮箱匹配
/// <summary> /// 匹配邮编是否合法 /// </summary> /// <param name="source">待匹配字符串</param> /// <returns>邮编合法返回true,反之不合法</returns> public bool CheckPostcodeByString(string source) { Regex rg = new Regex(@"^\d{6}$"); return rg.IsMatch(source); }
5.电话匹配
/// <summary> /// 匹配电话号码是否合法 /// </summary> /// <param name="source">待匹配字符串</param> /// <returns>电话号码合法返回true,反之不合法</returns> public bool CheckTelephoneByString(string source) { Regex rg = new Regex(@"^(\(\d{3,4}-)|\d{3.4}-)?\d{7,8}$"); return rg.IsMatch(source); }
6.手机号匹配
/// <summary> /// 匹配手机号码是否合法 /// </summary> /// <param name="source">待匹配字符串</param> /// <returns>手机号码合法返回true,反之不合法</returns> public bool CheckMobilephoneByString(string source) { Regex rg = new Regex(@"^[1]+[3,5,7,8]+\d{9}$"); return rg.IsMatch(source); }
7.身份证匹配
/// <summary> /// 匹配身份证号码是否合法 /// </summary> /// <param name="source">待匹配字符串</param> /// <returns>身份证号码合法返回true,反之不合法</returns> public bool CheckIDCardByString(string source) { Regex rg = new Regex(@"^(^\d{15}$|^\d{18}$|^\d{17}(\d|X|x))$"); return rg.IsMatch(source); }