简体   繁体   English

正则表达式匹配当前年份日期

[英]Regex matching current year date

I just started to learn regex.我刚开始学习正则表达式。 I created a regex for my project username where I want:我为我想要的项目用户名创建了一个正则表达式:

  • 2 digits of day日期的 2 位数字

  • 2 digits of month月份的 2 位数字

  • last 2 digits of current year当前年份的最后 2 位数字

  • finally last 2 digits of the year after 4 years 4 年后终于是年份的最后 2 位数字

ie I need to match output something like that:即我需要匹配这样的输出:

23071923

where:在哪里:

  • 23 is the current day(dd) 23 是当天(dd)

  • 07 is the month (mm) 07是月份(mm)

  • 19 is current year(YY) 19 是当前年份(YY)

  • 23 is the year after 4 years(yy) 23 是 4 年后的那一年(yy)

But I am confused how will it match the current year and only take last 2 digits of the year.但我很困惑它如何匹配当前年份并且只取年份的最后 2 位数字。 and how i can match the end of regex is correct and is 4 added in the year or not?以及我如何匹配正则表达式的结尾是否正确并且是否在一年中添加了 4 个?

You could use the trick with replacing the input string with modified string, which does math:您可以使用该技巧将输入字符串替换为修改后的字符串,它进行数学运算:

var x = "23071923";
var x2 = Regex.Replace(x, @"^(\d{2})(\d{2})(\d{2})(\d{2})$", m =>
         m.Groups[1].Value + m.Groups[2].Value + m.Groups[3].Value +
         (int.Parse(m.Groups[3].Value) + 4));
bool is_match = x == x2;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM