RegexJudgment.cs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. using System;
  2. using System.Text.RegularExpressions;
  3. using HandyControl.Data;
  4. namespace HandyControl.Tools;
  5. /// <summary>
  6. /// 包含一些正则验证操作
  7. /// </summary>
  8. public static class RegexJudgment
  9. {
  10. private static readonly RegexPatterns RegexPatterns = new();
  11. /// <summary>
  12. /// 判断字符串格式是否符合某种要求
  13. /// </summary>
  14. /// <param name="str">需要判断的字符串</param>
  15. /// <param name="pattern">正则表达式</param>
  16. /// <returns></returns>
  17. public static bool IsKindOf(this string str, string pattern)
  18. {
  19. return Regex.IsMatch(str, pattern);
  20. }
  21. /// <summary>
  22. /// 判断字符串是否满足指定的格式
  23. /// </summary>
  24. /// <param name="text">需要判断的字符串</param>
  25. /// <param name="textType">指定格式的文本</param>
  26. /// <returns></returns>
  27. public static bool IsKindOf(this string text, TextType textType)
  28. {
  29. if (textType == TextType.Common) return true;
  30. return Regex.IsMatch(text,
  31. RegexPatterns.GetValue(Enum.GetName(typeof(TextType), textType) + "Pattern").ToString());
  32. }
  33. /// <summary>
  34. /// 判断字符串格式是否为电子邮件
  35. /// </summary>
  36. /// <param name="email">需要判断的Email字符串</param>
  37. /// <returns>方法返回布尔值</returns>
  38. public static bool IsEmail(this string email)
  39. {
  40. return Regex.IsMatch(email, RegexPatterns.MailPattern);
  41. }
  42. /// <summary>
  43. /// 判断字符串格式是否为指定类型的IP地址
  44. /// </summary>
  45. /// <param name="ip">需要判断的IP字符串</param>
  46. /// <param name="ipType">指定的IP类型</param>
  47. /// <returns>方法返回布尔值</returns>
  48. public static bool IsIp(this string ip, IpType ipType)
  49. {
  50. return ipType switch
  51. {
  52. IpType.A => Regex.IsMatch(ip, RegexPatterns.IpAPattern),
  53. IpType.B => Regex.IsMatch(ip, RegexPatterns.IpBPattern),
  54. IpType.C => Regex.IsMatch(ip, RegexPatterns.IpCPattern),
  55. IpType.D => Regex.IsMatch(ip, RegexPatterns.IpDPattern),
  56. IpType.E => Regex.IsMatch(ip, RegexPatterns.IpEPattern),
  57. _ => false
  58. };
  59. }
  60. /// <summary>
  61. /// 判断字符串格式是否为IP地址
  62. /// </summary>
  63. /// <param name="ip">需要判断的IP字符串</param>
  64. /// <returns>方法返回布尔值</returns>
  65. public static bool IsIp(this string ip)
  66. {
  67. return Regex.IsMatch(ip, RegexPatterns.IpPattern);
  68. }
  69. /// <summary>
  70. /// 判断字符串格式是否为单个汉字
  71. /// </summary>
  72. /// <param name="str">需要判断的单个汉字字符串</param>
  73. /// <returns>方法返回布尔值</returns>
  74. public static bool IsChinese(this string str)
  75. {
  76. return Regex.IsMatch(str, RegexPatterns.ChinesePattern);
  77. }
  78. /// <summary>
  79. /// 判断字符串格式是否为url
  80. /// </summary>
  81. /// <param name="str">需要判断的url字符串</param>
  82. /// <returns>方法返回布尔值</returns>
  83. public static bool IsUrl(this string str)
  84. {
  85. return Regex.IsMatch(str, RegexPatterns.UrlPattern);
  86. }
  87. }