当前位置: 代码迷 >> 综合 >> C# 正则 Groups 用法
  详细解决方案

C# 正则 Groups 用法

热度:77   发布时间:2023-12-06 01:17:33.0

用法

            string str = "date={LastMonths=3}";string bracketsPattern = @"{(?<name>\w+)=(?<value>\w+)}";var bracketsMatch = new Regex(bracketsPattern, RegexOptions.IgnoreCase).Matches(str);foreach (Match item in bracketsMatch){var groupName = item.Groups["name"].Value;var groupValue = item.Groups["value"].Value;switch (groupName){case "LastMonths":str = str.Replace("{" + groupName +"="+ groupValue + "}", string.Format("{0:yyyy-MM-dd}", DateTime.Now.AddMonths(-int.Parse(groupValue))));break;}};

结果:“date=2020-10-23";

  相关解决方案