Regexp for date?
-
Hi! I'm relativ new to RegExp and writing patterns. I wan't to write the pattern for (danish) dates between 01-01-1970 and 31-12-2099. How do I do that? I'm a little confused with the | op. I think. My best try is: string strPattern = @"^([0-2][1-9]|[3][0-1]){1}-{1}([0-1][1-2]){1}-{1}([1][9][7-9][0-9]|[2][0][0-9][0-9]){1}$"; Where (...){1} should be exactly 1 of the exclusive choices in (...) It seems to work validating it here: http://www.sweeting.org/mark/html/revalid.php#calc[^] But in c# it do not work! Whay is that? Regex rx = new Regex(strPattern); rx.IsMatch(test) - seems to be false most of the time! I can't figure it out - help!
Michael Mogensen
-
Hi! I'm relativ new to RegExp and writing patterns. I wan't to write the pattern for (danish) dates between 01-01-1970 and 31-12-2099. How do I do that? I'm a little confused with the | op. I think. My best try is: string strPattern = @"^([0-2][1-9]|[3][0-1]){1}-{1}([0-1][1-2]){1}-{1}([1][9][7-9][0-9]|[2][0][0-9][0-9]){1}$"; Where (...){1} should be exactly 1 of the exclusive choices in (...) It seems to work validating it here: http://www.sweeting.org/mark/html/revalid.php#calc[^] But in c# it do not work! Whay is that? Regex rx = new Regex(strPattern); rx.IsMatch(test) - seems to be false most of the time! I can't figure it out - help!
Michael Mogensen
Should the - be enclosed in \\ like: \-\ ?
Michael Mogensen
-
Hi! I'm relativ new to RegExp and writing patterns. I wan't to write the pattern for (danish) dates between 01-01-1970 and 31-12-2099. How do I do that? I'm a little confused with the | op. I think. My best try is: string strPattern = @"^([0-2][1-9]|[3][0-1]){1}-{1}([0-1][1-2]){1}-{1}([1][9][7-9][0-9]|[2][0][0-9][0-9]){1}$"; Where (...){1} should be exactly 1 of the exclusive choices in (...) It seems to work validating it here: http://www.sweeting.org/mark/html/revalid.php#calc[^] But in c# it do not work! Whay is that? Regex rx = new Regex(strPattern); rx.IsMatch(test) - seems to be false most of the time! I can't figure it out - help!
Michael Mogensen
the regex works fine in Expresso, and since Expresso uses .net regex engine, it should work fine, Maybe the problem is in an another part of you code? 01-01-1970 <- Validates 31-12-2099 <- Validates 31-12-2100 <- Doesn't validate 01-01-1969 <- Doesn't validate
-
the regex works fine in Expresso, and since Expresso uses .net regex engine, it should work fine, Maybe the problem is in an another part of you code? 01-01-1970 <- Validates 31-12-2099 <- Validates 31-12-2100 <- Doesn't validate 01-01-1969 <- Doesn't validate
Thanx' I'll check it out...
Michael Mogensen
-
Hi! I'm relativ new to RegExp and writing patterns. I wan't to write the pattern for (danish) dates between 01-01-1970 and 31-12-2099. How do I do that? I'm a little confused with the | op. I think. My best try is: string strPattern = @"^([0-2][1-9]|[3][0-1]){1}-{1}([0-1][1-2]){1}-{1}([1][9][7-9][0-9]|[2][0][0-9][0-9]){1}$"; Where (...){1} should be exactly 1 of the exclusive choices in (...) It seems to work validating it here: http://www.sweeting.org/mark/html/revalid.php#calc[^] But in c# it do not work! Whay is that? Regex rx = new Regex(strPattern); rx.IsMatch(test) - seems to be false most of the time! I can't figure it out - help!
Michael Mogensen
{1} is entirely redundant. Anything without a specifier is required exactly once. Also, a character class with one element is just extra typing, so [1][9] should just be 19. The only obvious problem I see is the month section only gets 01, 02, 11, and 12. I would try this:
^([0-2][1-9]|3[0-1])-(0[1-9]|1[0-2])-(19[7-9][0-9]|20[0-9][0-9])$
-
{1} is entirely redundant. Anything without a specifier is required exactly once. Also, a character class with one element is just extra typing, so [1][9] should just be 19. The only obvious problem I see is the month section only gets 01, 02, 11, and 12. I would try this:
^([0-2][1-9]|3[0-1])-(0[1-9]|1[0-2])-(19[7-9][0-9]|20[0-9][0-9])$
Also this works: private readonly string STR_DDMMYYYY_PATTERN = @"^([0][1-9]|[1-2][0-9]|[3][0-1])-([0][1-9]|[1][0-2])-([1][9][7-9][0-9]|[2][0][0-9][0-9])$"; I figured the {1} problem out also. Seems that the validator-site is OK with dealing with apparently redundant chars - but class Regex are not? So now this is OK:
Regex regexpDDMMYYYY = new Regex(STR_DDMMYYYY_PATTERN);
bool bIsValid = regexpDDMMYYYY.IsMatch(tbGeneral.Text);
if (bIsValid)
{
...
etc.I'll test the [1][9] replacement also. Thanx' again.
Michael Mogensen
-
Hi! I'm relativ new to RegExp and writing patterns. I wan't to write the pattern for (danish) dates between 01-01-1970 and 31-12-2099. How do I do that? I'm a little confused with the | op. I think. My best try is: string strPattern = @"^([0-2][1-9]|[3][0-1]){1}-{1}([0-1][1-2]){1}-{1}([1][9][7-9][0-9]|[2][0][0-9][0-9]){1}$"; Where (...){1} should be exactly 1 of the exclusive choices in (...) It seems to work validating it here: http://www.sweeting.org/mark/html/revalid.php#calc[^] But in c# it do not work! Whay is that? Regex rx = new Regex(strPattern); rx.IsMatch(test) - seems to be false most of the time! I can't figure it out - help!
Michael Mogensen
A while back, I wrote a blog entry[^] on a general purpose regular expression date validator that you might want to take a look at. The code should give you some ideas, especially as it can show you why regular expressions for dates aren't enough on their own.
"WPF has many lovers. It's a veritable porn star!" - Josh Smith
As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.