0

Is there any regex pattern storage where I can find patterns like dd.MM.yyyy or maybe you can help me with that?

I'm using data validation with data annotations:

[RegularExpression(@"pattern", ErrorMessage = "incorrect date format")]
public string MyDate { get; set; }
Sergejs
  • 2,540
  • 6
  • 32
  • 51
  • What language are you using? What platform? What are you actually trying to achieve? Please post some code examples so we can help with those. – Oded Jan 22 '13 at 10:41
  • Sorry, thought regex is same for all languages. Will add some info now – Sergejs Jan 22 '13 at 10:45
  • Regular expressions have different "flavours" they are not uniform (though most of the _basic_ ones are). Can you post _examples_ of inputs and expected results? – Oded Jan 22 '13 at 10:47
  • So, you are using ASP.NET MVC? Why do you make `MyDate` a _string_ instead of a `DateTime`? – Oded Jan 22 '13 at 10:53
  • I have a reason(when I'm using datetime, I can't replace default validation message if entering something like: "Azzzz" what can't be converted to a datetime) I've asked: http://stackoverflow.com/questions/14397098/mvc3-how-to-define-custom-error-messages-using-annotations – Sergejs Jan 22 '13 at 10:55
  • 1
    http://stackoverflow.com/questions/4896632/uk-date-regular-expression – Oded Jan 22 '13 at 10:57

5 Answers5

0

You can always try to use a regex tool, such like 'RegExBuddy' or 'Rad Software Regular Expression Designer'.

But a regular expression you wanted is maybe like this:

\d{2}.\d{2}.\d{4}?
Emma
  • 27,428
  • 11
  • 44
  • 69
wsplinter
  • 1,021
  • 1
  • 7
  • 7
0

As per ECMAScript specification, a quick answer could be

^(\d{2}).\d{2}.(\d{4})$
Emma
  • 27,428
  • 11
  • 44
  • 69
0

You can use something like this:

\d{1,2}\\.\d{1,2}\\.(19\d\d|20\d\d)
Emma
  • 27,428
  • 11
  • 44
  • 69
Oleg Ignatov
  • 877
  • 2
  • 8
  • 22
0

The pattern that doesn't check for short month and leap year:

string pattern = @"(0[1-9]|[12][0-9]|3[01])\.(0[1-9]|1[012])\.(19|20)\d\d"
Sergejs
  • 2,540
  • 6
  • 32
  • 51
0
// 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
JohnyL
  • 6,894
  • 3
  • 22
  • 41
  • 1
    Handling leap years in regex will be tough because of the "years that are a multiple of 4 unless they're a multiple of 100 except when they're a multiple of 400" rule. This returns `true` for `"29.02.1600"`, `"29.02.1700"`, `"29.02.1800"`, `"29.02.1900"`, `"29.02.2000"`, and `"29.02.2100"` even though only 1600 and 2000 were leap years. – Lance U. Matthews Apr 02 '20 at 21:04