// Each month has a different number of days.
// More over, we have different number of days in February which depends on whether
// the year is a leap year. Here is how to solve this problem:
// 1) if the first digit is even, the second digit must be 0, 4, or 8
// 2) if the first digit is odd, the second digit must be 2 or 6
var pattern = "^([02468][048]|[13579][26])$";
// Deal with months with 31 days
var d1 = @"(3[01]|[012]\d|\d)\.(0?[13578]|10|12)\.\d{4}";
// Deak with months with 30 days
var d2 = @"(30|[012]\d|\d)\.(0?[469]|11)\.\d{4}";
// This portion deals with February 29 in leap years
var d3 = @"29\.(0?2)\.\d{2}([02468][048]|[13579][26])";
// Deal with other days in February
var d4 = @"(2[0-8]|[01]\d|\d)\.(0?2)\.\d{4}";
// Full pattern
var date_pattern = $"^({d1}|{d2}|{d3}|{d4})$";
var dates = new[]
{
"11.12.2001",
"15.99.2021",
"14.01.2010",
"29.02.2020", //leap year
"29.02.2019", //not a leap year
"28.02.2019"
};
foreach (var date in dates)
{
WriteLine($"Date: {date}, Is match: {Regex.IsMatch(date, date_pattern)}");
}
// Output:
// Date: 11.12.2001, Is match: True
// Date: 15.99.2021, Is match: False
// Date: 14.01.2010, Is match: True
// Date: 29.02.2020, Is match: True
// Date: 29.02.2019, Is match: False
// Date: 28.02.2019, Is match: True