正则表达式的日期

时间:2011-03-21 06:18:13

标签: c# regex

  • Montag 28.03.2011
  • Mi,23.03.11
  • 21.03.2011 - 27.03.2011

如何使用REGEX 28.03.2011,23.03.11和21.03.2011?

最后一个我可以得到分裂和“ - ”,但前两个怎么样?我可以以某种方式阅读数字吗?

2 个答案:

答案 0 :(得分:3)

对于那种问题,我使用Expresso - free和.NET兼容。 (http://www.ultrapico.com/Expresso.htm

\ d {2} [。] \ d {2} [。] \ d {4} - 没有值检查的最简单模式

您也可以告诉我们您实际尝试做的更多内容:)

对于数字提取:

Regex regex = new Regex("(?<day>\d{2})[.](?<month>\d{2})\[.](?<year>(\d{4}|\d{2}))", RegexOptions.CultureInvariant);

Match match = regex.Match("20.03.2011");
if (match.Success)
{
   if (match.Groups["day"] != null)                           
   {
        string day = match.Groups["day"].value;
   }
   //etc.
}

答案 1 :(得分:3)

您可以使用以下内容获取各个日期:

(?<Day>\d{1,2})\.(?<Month>\d{1,2})\.(?<Year>(?:\d{4}|\d{2}))

我认为这比试图剥离“Montag”等更有用。