namespace ETD.Data { using System; using System.Text.RegularExpressions; public class RegExp { /// /// 判断字符串是否是yy-mm-dd字符串 /// /// 待判断字符串 /// 判断结果 public static bool IsDateString(string str) { return Regex.IsMatch(str, @"(\d{4})-(\d{1,2})-(\d{1,2})"); } /// /// 检测是否符合email格式 /// /// 要判断的email字符串 /// 判断结果 public static bool IsEmail(string strEmail) { return Regex.IsMatch(strEmail, @"^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$"); } /// /// 判断是否是正常的ip格式 /// /// 验证字符串 /// public static bool IsIP(string ipAdress) { return Regex.IsMatch(ipAdress, @"^((2[0-4]\d|25[0-5]|[01]?\d\d?)\.){3}(2[0-4]\d|25[0-5]|[01]?\d\d?)$"); } public static bool IsJsonSafety(string s) { string text1 = Regex.Replace(s.Replace("%20", " "), @"\s", " "); string text2 = @"select |insert |delete from |count\(|drop table|update |truncate |asc\(|mid\(|char\(|xp_cmdshell|exec master|net localgroup administrators|net user|\'| or "; return !Regex.IsMatch(text1, text2, RegexOptions.IgnoreCase); } /// /// 判断给定的字符串(strNumber)是否是数值型 /// /// 要确认的字符串 /// 是则返加true 不是则返回 false public static bool IsNumber(string strNumber) { return new Regex(@"^([0-9])[0-9]*(\.\w*)?$").IsMatch(strNumber); } /// /// 判断是否是qq号码 /// /// 要验证的字符串 /// public static bool IsQQ(string str) { return Regex.IsMatch(str, "[1-9][0-9]{4,}"); } /// /// 检测是否有Sql危险字符 /// /// 要判断字符串 /// 判断结果 public static bool IsSafeSqlString(string str) { return !Regex.IsMatch(str, @"[-|;|,|\/|\(|\)|\[|\]|\}|\{|%|@|\*|!|\']"); } public static bool IsSafety(string s) { string text1 = Regex.Replace(s.Replace("%20", " "), @"\s", " "); string text2 = "select |insert |delete from |count\\(|drop table|update |truncate |asc\\(|mid\\(|char\\(|xp_cmdshell|exec master|net localgroup administrators|:|net user|\"|\\'| or "; return !Regex.IsMatch(text1, text2, RegexOptions.IgnoreCase); } /// /// 判断是否是时间格式 /// /// 判断字符串 /// public static bool IsTimeString(string strTime) { return Regex.IsMatch(strTime, "^((([0-1]?[0-9])|(2[0-3])):([0-5]?[0-9])(:[0-5]?[0-9])?)$"); } /// /// 判断是否是小数 /// /// 要验证的字符串 /// public static bool IsValidDecimal(string str) { return Regex.IsMatch(str, @"[0].\d{1,2}|[1]"); } /// /// 判断是否是电话号码格式 /// /// 要验证的字符串 /// public static bool IsValidTel(string str) { return Regex.IsMatch(str, @"(\d+-)?(\d{4}-?\d{7}|\d{3}-?\d{8}|^\d{7,8})(-\d+)?"); } } }