.NET 正则表达式通用方法
正则表达式验证数据类型通用方法
/// <summary> /// 处理数据类型转换,数制转换、编码转换相关的类 /// </summary> public static class ValidateHelper { #region 正则表达式 //邮政编码 public static readonly System.Text.RegularExpressions.Regex RegPostCode = new System.Text.RegularExpressions.Regex("^\\d{6}$"); //中国身份证验证 public static readonly System.Text.RegularExpressions.Regex RegCardId = new System.Text.RegularExpressions.Regex("^\\d{17}[\\d|X]|\\d{15}|\\d{18}$"); //数字 public static readonly System.Text.RegularExpressions.Regex RegNumber = new System.Text.RegularExpressions.Regex("^\\d+$"); //固定电话 public static readonly System.Text.RegularExpressions.Regex RegTel = new System.Text.RegularExpressions.Regex("^\\d{3,4}-\\d{7,8}|\\d{7,8}$"); //手机号 public static readonly System.Text.RegularExpressions.Regex RegPhone = new System.Text.RegularExpressions.Regex("^[1][3-8]\\d{9}$"); //电话号码(包括固定电话和手机号) public static readonly System.Text.RegularExpressions.Regex RegTelePhone = new System.Text.RegularExpressions.Regex("^(\\d{3,4}-\\d{7,8}|\\d{7,8})|([1][3-8]\\d{9})$"); //邮箱 public static readonly System.Text.RegularExpressions.Regex RegEmail = new System.Text.RegularExpressions.Regex("^[\\w-]+@[\\w-]+\\.(com|net|org|edu|mil|tv|biz|info)$"); //中文 public static readonly System.Text.RegularExpressions.Regex RegChzn = new System.Text.RegularExpressions.Regex("[\u4e00-\u9fa5]"); //IP地址 public static readonly System.Text.RegularExpressions.Regex RegIp = new System.Text.RegularExpressions.Regex("((25[0-5]|2[0-4]\\d|1?\\d?\\d)\\.){3}(25[0-5]|2[0-4]\\d|1?\\d?\\d)"); #endregion #region 类型判断 /// <summary> /// 判断是否不为空 /// </summary> /// <param name="data">输入值</param> /// <returns></returns> public static bool IsNotNull(this object data) { return data != null; } /// <summary> /// 判断是否为空 /// </summary> /// <param name="data">输入值</param> /// <returns></returns> public static bool IsNull(this object data) { return data == null; } /// <summary> /// 判断是否为空 /// </summary> /// <param name="data">输入值</param> /// <returns></returns> public static bool IsNotNullOrEmpty(this object data) { if (data == null) { return false; } return !string.IsNullOrWhiteSpace(data.ToString()); } /// <summary> /// 判断是否为空 /// </summary> /// <param name="data">输入值</param> /// <returns></returns> public static bool IsNullOrEmpty(this object data) { if (data == null) { return true; } return string.IsNullOrWhiteSpace(data.ToString()); } /// <summary> /// 判断dataset是否有行数 /// </summary> /// <param name="ds"></param> /// <returns></returns> public static bool HasMoreRow(this DataSet ds) { if (ds == null || ds.Tables.Count == 0 || ds.Tables[0].Rows.Count == 0) { return false; } else { return true; } } /// <summary> /// 判断datatable是否有行数 /// </summary> /// <param name="dt"></param> /// <returns></returns> public static bool HasMoreRow(this DataTable dt) { if (dt == null || dt.Rows.Count == 0) return false; else return true; } /// <summary> /// 判断是否是数字 /// </summary> /// <param name="data">输入值</param> /// <returns></returns> public static bool IsInt(this object data) { long num; return long.TryParse(data.ToString(), out num); } /// <summary> /// 判断是否是小数 /// </summary> /// <param name="data">输入值</param> /// <returns></returns> public static bool IsDouble(this object data) { double dValue; return double.TryParse(data.ToString(), out dValue); } /// <summary> /// 判断是否浮点型 /// </summary> /// <param name="data">输入值</param> /// <returns></returns> public static bool IsFloat(this object data) { float fValue; return float.TryParse(data.ToString(), out fValue); } /// <summary> /// 判断是否是时间 /// </summary> /// <param name="data">输入值</param> /// <returns></returns> public static bool IsDateTime(this object data) { DateTime fValue; return DateTime.TryParse(data.ToString(), out fValue); } /// <summary> /// 判断是否是小数 /// </summary> /// <param name="data">输入值</param> /// <returns></returns> public static bool IsDecimal(this object data) { decimal dValue; return decimal.TryParse(data.ToString(), out dValue); } /// <summary> /// 判断字符串是否为Email /// </summary> /// <param name="data">输入值</param> /// <returns></returns> public static bool IsEmail(this object data) { var match = RegEmail.Match(data.ToString()); return match.Success; } /// <summary> /// 判断字符串是否为固话 /// </summary> /// <param name="data">输入值</param> /// <returns></returns> public static bool IsTel(this object data) { var match = RegTel.Match(data.ToString()); return match.Success; } /// <summary> /// 判断字符串是否为手机号 /// </summary> /// <param name="data">输入值</param> /// <returns></returns> public static bool IsPhone(this object data) { var match = RegPhone.Match(data.ToString()); return match.Success; } /// <summary> /// 判断字符串是否为电话号码 /// (包含固定电话和手机号) /// </summary> /// <param name="data">输入值</param> /// <returns></returns> public static bool IsTelePhone(this object data) { var match = RegTelePhone.Match(data.ToString()); return match.Success; } /// <summary> /// 判断字符串是否为IP地址 /// </summary> /// <param name="data">输入值</param> /// <returns></returns> public static bool IsIp(this object data) { var match = RegIp.Match(data.ToString()); return match.Success; } /// <summary> /// 判断字符串是否为邮编 /// </summary> /// <param name="data">输入值</param> /// <returns></returns> public static bool IsPostCode(this object data) { var match = RegPostCode.Match(data.ToString()); return match.Success; } /// <summary> /// 判断字符串是否为身份证 /// </summary> /// <param name="data">输入值</param> /// <returns></returns> public static bool IsCardId(this object data) { var match = RegCardId.Match(data.ToString()); return match.Success; } /// <summary> /// 判断字符串是否为中文 /// </summary> /// <param name="data">输入值</param> /// <returns></returns> public static bool IsChzn(this object data) { var match = RegChzn.Match(data.ToString()); return match.Success; } /// <summary> /// 判断字符串是否为数字 /// </summary> /// <param name="data">输入值</param> /// <returns></returns> public static bool IsNumber(this object data) { var match = RegNumber.Match(data.ToString()); return match.Success; } #endregion }
作者:apegu
地址:http://www.cnblogs.com/jiangqw
如果,您认为阅读这篇博客让您有些收获,不妨点击一下推荐按钮。