C# 使用MaskedTextBox实现日期时间选择器功能。使用RegEx完成验证

C# 使用MaskedTextBox实现日期时间选择器功能。使用RegEx完成验证,c#,.net,regex,winforms,perl,C#,.net,Regex,Winforms,Perl,我正在尝试为我的应用程序专门创建一个自定义控件,该控件将使用maskedTextBox来限制输入的数据 现在我想在C#中实现这一点 我看到了一个正则表达式,它通过捕获enterleave和keypress事件来限制日期,从而验证我的日期 现在我的RegExp是这样的 string regYear =@"(200[8,9]|201[0-9])"; //for year from 2008-2019 Plz correct this RegEx if wrong. string

我正在尝试为我的应用程序专门创建一个自定义控件,该控件将使用maskedTextBox来限制输入的数据

现在我想在C#中实现这一点

我看到了一个正则表达式,它通过捕获enterleave和keypress事件来限制日期,从而验证我的日期

现在我的RegExp是这样的

    string regYear  =@"(200[8,9]|201[0-9])";  //for year from 2008-2019  Plz correct this RegEx if wrong.
    string regMonth =@"(0[1-9]|1[012])";
    string regDate  =@"(0[1-9]|[12][0-9]|3[01])";
    string seperator=@"[- /]";

    string ddmmyyyy=regDate+seperator+regMonth+seperator+regYear;
我看到了一个关于检查日期格式的正则表达式的。 现在我想在C#中使用我在上面的链接中提供的代码。这段代码是用Perl编写的,我想用C#实现同样的功能。但我不知道如何从下面给出的正则表达式中检索日期、月份、年份,例如从$1、$2、$3中提取

sub isvaliddate {
  my $input = shift;
  if ($input =~ m!^((?:19|20)\d\d)[- /.](0[1-9]|1[012])[- /.](0[1-9]|[12][0-9]|3[01])$!) {
    # At this point, $1 holds the year, $2 the month and $3 the day of the date entered
    if ($3 == 31 and ($2 == 4 or $2 == 6 or $2 == 9 or $2 == 11)) {
      return 0; # 31st of a month with 30 days
    } elsif ($3 >= 30 and $2 == 2) {
      return 0; # February 30th or 31st
    } elsif ($2 == 2 and $3 == 29 and not ($1 % 4 == 0 and ($1 % 100 != 0 or $1 % 400 == 0))) {
      return 0; # February 29th outside a leap year
    } else {
      return 1; # Valid date
    }
  } else {
    return 0; # Not a date
  }
}
我想使用this.DateOnly、this.MonthOnly、this.YearOnly返回用户日期部分、月份部分和年份部分,我需要提取这些值

我的主要担忧


保存在三个变量中输入的日期的年、月和日,这三个变量来自Perl的
maskedTextBox
$1
$2
,和
$3
相当于C#的
m.Groups[1]。Value
m.Groups[2]。Value
,等等

要在示例中提取它们,可以使用

Match m = Regex.Match(ddmmyyyy);
if (m.Success) {
    string day = m.Groups[1];
    string month = m.Groups[2];
    string year = m.Groups[3];
}

$1、$2、$3等是正则表达式捕获的值,以位置命名。@匿名:那么我应该如何在C#中捕获相同的值,位置没有问题,请参阅我的正则表达式ddmmyyyy。在这种情况下,我如何捕获这些值
Match m = Regex.Match(ddmmyyyy);
if (m.Success) {
    string day = m.Groups[1];
    string month = m.Groups[2];
    string year = m.Groups[3];
}