足力健后端,使用.netcore版本,合并1个项目使用

RegexHelper.cs 2.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.Text.RegularExpressions;
  5. namespace System.Common
  6. {
  7. /// <summary>
  8. /// 正则表达式公共类
  9. /// </summary>
  10. public class RegexHelper
  11. {
  12. /// <summary>
  13. /// 检查是否匹配
  14. /// </summary>
  15. /// <param name="str"></param>
  16. /// <param name="pattern"></param>
  17. /// <param name="options"></param>
  18. /// <returns></returns>
  19. public static bool IsMatch(string str, string pattern, RegexOptions options = RegexOptions.IgnoreCase)
  20. {
  21. return Regex.IsMatch(str, pattern, options);
  22. }
  23. /// <summary>
  24. /// 获取匹配对象
  25. /// </summary>
  26. /// <param name="str"></param>
  27. /// <param name="pattern"></param>
  28. /// <param name="options"></param>
  29. /// <returns></returns>
  30. public static Match GetMatch(string str, string pattern, RegexOptions options = RegexOptions.IgnoreCase)
  31. {
  32. return Regex.Match(str, pattern, options);
  33. }
  34. /// <summary>
  35. /// 从字符串中找到匹配的字符
  36. /// </summary>
  37. /// <param name="str">原始字符</param>
  38. /// <param name="pattern">正则</param>
  39. /// <param name="spliter">分隔符</param>
  40. /// <returns></returns>
  41. public static List<Match> FindMatchs(string str, string pattern, RegexOptions options = RegexOptions.IgnoreCase)
  42. {
  43. var list = new List<Match>();
  44. var matchs = Regex.Matches(str, pattern, options);
  45. foreach (Match matcher in matchs)
  46. {
  47. if (matcher == null || !matcher.Success) { continue; }
  48. list.Add(matcher);
  49. }
  50. return list;
  51. }
  52. /// <summary>
  53. /// 用正则替换
  54. /// </summary>
  55. /// <param name="str"></param>
  56. /// <param name="replacement"></param>
  57. /// <param name="pattern"></param>
  58. /// <returns></returns>
  59. public static string Replace(string str, string replacement, string pattern, RegexOptions options = RegexOptions.IgnoreCase)
  60. {
  61. return Regex.Replace(str, pattern, replacement, options);
  62. }
  63. }
  64. }