using System;
using System.Text.RegularExpressions;
public class Example
{
public static void Main()
{
string pattern = @"^((\d{2}(?!0229))|([02468][048]|[13579][26])(?=0229))(0[1-9]|1[0-2])(0[1-9]|[12]\d|(?<!02)30|(?<!02|4|6|9|11)31)\-(\d{2})\-(\d{4})$";
string input = @"Case 1:
Correct
Month with 31days
000131-01-1728
Month with 30days
000430-01-1728
Feb with 28days
010228-01-1728
Feb with 29days
200229-01-1728
Feb with 29days
960229-01-1728
Case 2:
Invalid
Invalid date 32nd
990132-01-1728
Invalid date 31st for april
000431-01-1728
Invalid date 29th for feb in non leap year
010229-01-1728
Invalid date 30th for feb in leap year
200230-01-1728
Invalid month 13th
201301-01-1728
Invalid month 00th
200001-01-1728
Invalid date 00th
200100-01-1728";
RegexOptions options = RegexOptions.Multiline;
foreach (Match m in Regex.Matches(input, pattern, options))
{
Console.WriteLine("'{0}' found at index {1}.", m.Value, m.Index);
}
}
}
Please keep in mind that these code samples are automatically generated and are not guaranteed to work. If you find any syntax errors, feel free to submit a bug report. For a full regex reference for C#, please visit: https://msdn.microsoft.com/en-us/library/system.text.regularexpressions.regex(v=vs.110).aspx