RegexService.cs 1.1 KB

123456789101112131415161718192021222324252627282930313233
  1. using System.Text.RegularExpressions;
  2. namespace Alchemy.Core.Services
  3. {
  4. public class RegexService
  5. {
  6. /// <summary>
  7. /// 判断正则表达式
  8. /// </summary>
  9. /// <param name="strRaw"></param>
  10. /// <param name="strFormat"></param>
  11. /// <returns></returns>
  12. public static bool IsMatch(string strRaw, string strFormat)
  13. {
  14. Regex regex = new Regex(strFormat, RegexOptions.None);
  15. return regex.IsMatch(strRaw);
  16. }
  17. /// <summary>
  18. ///
  19. /// </summary>
  20. /// <param name="strInput"></param>
  21. /// <param name="strStart"></param>
  22. /// <param name="strEnd"></param>
  23. /// <returns></returns>
  24. public static string GetStringBetween(string strInput, string strStart, string strEnd)
  25. {
  26. string pattern = Regex.Escape(strStart) + "(.*?)" + Regex.Escape(strEnd);
  27. Match match = Regex.Match(strInput, pattern);
  28. if (match.Success)
  29. return match.Groups[1].Value;
  30. return string.Empty;
  31. }
  32. }
  33. }