RegExp.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. namespace ETD.Data
  2. {
  3. using System;
  4. using System.Text.RegularExpressions;
  5. public class RegExp
  6. {
  7. /// <summary>
  8. /// 判断字符串是否是yy-mm-dd字符串
  9. /// </summary>
  10. /// <param name="str">待判断字符串</param>
  11. /// <returns>判断结果</returns>
  12. public static bool IsDateString(string str)
  13. {
  14. return Regex.IsMatch(str, @"(\d{4})-(\d{1,2})-(\d{1,2})");
  15. }
  16. /// <summary>
  17. /// 检测是否符合email格式
  18. /// </summary>
  19. /// <param name="strEmail">要判断的email字符串</param>
  20. /// <returns>判断结果</returns>
  21. public static bool IsEmail(string strEmail)
  22. {
  23. 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})(\]?)$");
  24. }
  25. /// <summary>
  26. /// 判断是否是正常的ip格式
  27. /// </summary>
  28. /// <param name="ipAdress">验证字符串</param>
  29. /// <returns></returns>
  30. public static bool IsIP(string ipAdress)
  31. {
  32. 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?)$");
  33. }
  34. public static bool IsJsonSafety(string s)
  35. {
  36. string text1 = Regex.Replace(s.Replace("%20", " "), @"\s", " ");
  37. string text2 = @"select |insert |delete from |count\(|drop table|update |truncate |asc\(|mid\(|char\(|xp_cmdshell|exec master|net localgroup administrators|net user|\'| or ";
  38. return !Regex.IsMatch(text1, text2, RegexOptions.IgnoreCase);
  39. }
  40. /// <summary>
  41. /// 判断给定的字符串(strNumber)是否是数值型
  42. /// </summary>
  43. /// <param name="strNumber">要确认的字符串</param>
  44. /// <returns>是则返加true 不是则返回 false</returns>
  45. public static bool IsNumber(string strNumber)
  46. {
  47. return new Regex(@"^([0-9])[0-9]*(\.\w*)?$").IsMatch(strNumber);
  48. }
  49. /// <summary>
  50. /// 判断是否是qq号码
  51. /// </summary>
  52. /// <param name="str">要验证的字符串</param>
  53. /// <returns></returns>
  54. public static bool IsQQ(string str)
  55. {
  56. return Regex.IsMatch(str, "[1-9][0-9]{4,}");
  57. }
  58. /// <summary>
  59. /// 检测是否有Sql危险字符
  60. /// </summary>
  61. /// <param name="str">要判断字符串</param>
  62. /// <returns>判断结果</returns>
  63. public static bool IsSafeSqlString(string str)
  64. {
  65. return !Regex.IsMatch(str, @"[-|;|,|\/|\(|\)|\[|\]|\}|\{|%|@|\*|!|\']");
  66. }
  67. public static bool IsSafety(string s)
  68. {
  69. string text1 = Regex.Replace(s.Replace("%20", " "), @"\s", " ");
  70. string text2 = "select |insert |delete from |count\\(|drop table|update |truncate |asc\\(|mid\\(|char\\(|xp_cmdshell|exec master|net localgroup administrators|:|net user|\"|\\'| or ";
  71. return !Regex.IsMatch(text1, text2, RegexOptions.IgnoreCase);
  72. }
  73. /// <summary>
  74. /// 判断是否是时间格式
  75. /// </summary>
  76. /// <param name="strTime">判断字符串</param>
  77. /// <returns></returns>
  78. public static bool IsTimeString(string strTime)
  79. {
  80. return Regex.IsMatch(strTime, "^((([0-1]?[0-9])|(2[0-3])):([0-5]?[0-9])(:[0-5]?[0-9])?)$");
  81. }
  82. /// <summary>
  83. /// 判断是否是小数
  84. /// </summary>
  85. /// <param name="str">要验证的字符串</param>
  86. /// <returns></returns>
  87. public static bool IsValidDecimal(string str)
  88. {
  89. return Regex.IsMatch(str, @"[0].\d{1,2}|[1]");
  90. }
  91. /// <summary>
  92. /// 判断是否是电话号码格式
  93. /// </summary>
  94. /// <param name="str">要验证的字符串</param>
  95. /// <returns></returns>
  96. public static bool IsValidTel(string str)
  97. {
  98. return Regex.IsMatch(str, @"(\d+-)?(\d{4}-?\d{7}|\d{3}-?\d{8}|^\d{7,8})(-\d+)?");
  99. }
  100. }
  101. }