using System;
using System.Text.RegularExpressions;
public class Example
{
public static void Main()
{
string pattern = @"(?<day>0?[1-9]|[12][0-9]|3[01])(?>.{1})(?(?=0?[469]|11)(?<!31(?>.{1}))|)(?(?=0?2)(?<!3[01](?>.{1})))(?(?=0?2(?>.{1})\d\d(?!00)(?:[02468][048]|[13579][26]))|(?<!29(?>.{1})))(?<month>0?[1-9]|1[12])(?>.{1})(?<year>19[0-9][0-9]|20[0-9][0-9]|[0-9]{2,4})(?!\d)";
string input = @"29.02.1704
01.01.1987 // ok
101.01.19870 // bad- too many numbers
1.1.1987 // ok
32.02.1987 // bad- 32 feb
29.02.2001 // bad
0.0.1987 // bad
0.13.1987 // bad
8.8.8.8 // bad
31.04.1987 // bad
31.06.1987 // bad
31.09.1987 // bad
31.11.1987 // bad
30.02.1987 // bad
29.02/1987 // bad
29.02.1988 // ok
29.02.2000 // bad
29.02.2408
";
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