By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,710 Members | 1,970 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,710 IT Pros & Developers. It's quick & easy.

Date validation using Pattern matching

bytesonfire
P: 7
Hi All,

can anyone suggest how to validate a date (yyyymmdd format) using pattern match?
Nov 14 '08 #1
Share this Question
Share on Google+
5 Replies


Ganon11
Expert 2.5K+
P: 3,652
Use a regexp to match a series of 4 digits matched by a series of 2 digits followed by a series of 2 digits. Capture each of these matches, then make sure the month ($2) is between 1 and 12, and the day ($3) is between 1 and 28, 30, or 31 (depending on the month).
Nov 14 '08 #2

P: 2
A date-validation program
.
Expand|Select|Wrap|Line Numbers
  1. 1: #!/usr/local/bin/perl
  2. 2:
  3. 3: print ("Enter a date in the format YYYY-MM-DD:\n");
  4. 4: $date = <STDIN>;
  5. 5: chop ($date);
  6. 6:
  7. 7: # Because this pattern is complicated, we split it
  8. 8: # into parts, assign the parts to scalar variables,
  9. 9: # then substitute them in later.
  10. 10:
  11. 11: # handle 31-day months
  12. 12: $md1 = "(0[13578]|1[02])\\2(0[1-9]|[12]\\d|3[01])";
  13. 13: # handle 30-day months
  14. 14: $md2 = "(0[469]|11)\\2(0[1-9]|[12]\\d|30)";
  15. 15: # handle February, without worrying about whether it's
  16. 16: # supposed to be a leap year or not
  17. 17: $md3 = "02\\2(0[1-9]|[12]\\d)";
  18. 18:
  19. 19: # check for a twentieth-century date
  20. 20: $match = $date =~ /^(19)?\d\d(.)($md1|$md2|$md3)$/;
  21. 21: # check for a valid but non-20th century date
  22. 22: $olddate = $date =~ /^(\d{1,4})(.)($md1|$md2|$md3)$/;
  23. 23: if ($match) {
  24. 24: print ("$date is a valid date\n");
  25. 25: } elsif ($olddate) {
  26. 26: print ("$date is not in the 20th century\n");
  27. 27: } else {
  28. 28: print ("$date is not a valid date\n");
  29. 29: }
Nov 14 '08 #3

KevinADC
Expert 2.5K+
P: 4,059
A date-validation program
.
1: #!/usr/local/bin/perl
2:
3: print ("Enter a date in the format YYYY-MM-DD:\n");
4: $date = <STDIN>;
5: chop ($date);
6:
7: # Because this pattern is complicated, we split it
8: # into parts, assign the parts to scalar variables,
9: # then substitute them in later.
10:
11: # handle 31-day months
12: $md1 = "(0[13578]|1[02])\\2(0[1-9]|[12]\\d|3[01])";
13: # handle 30-day months
14: $md2 = "(0[469]|11)\\2(0[1-9]|[12]\\d|30)";
15: # handle February, without worrying about whether it's
16: # supposed to be a leap year or not
17: $md3 = "02\\2(0[1-9]|[12]\\d)";
18:
19: # check for a twentieth-century date
20: $match = $date =~ /^(19)?\d\d(.)($md1|$md2|$md3)$/;
21: # check for a valid but non-20th century date
22: $olddate = $date =~ /^(\d{1,4})(.)($md1|$md2|$md3)$/;
23: if ($match) {
24: print ("$date is a valid date\n");
25: } elsif ($olddate) {
26: print ("$date is not in the 20th century\n");
27: } else {
28: print ("$date is not a valid date\n");
29: }
looks like the code from Sams Teach Yourself Perl in 24 hours.
Nov 14 '08 #4

bytesonfire
P: 7
I did somthing like this (validating only MMDD)

Expand|Select|Wrap|Line Numbers
  1. (0[1-9]|1[012])(0[1-9]|[12][0-9]|3[01])
as year could be any value.;(
Nov 17 '08 #5

numberwhun
Expert Mod 2.5K+
P: 3,503
I did somthing like this (validating only MMDD)

Expand|Select|Wrap|Line Numbers
  1. (0[1-9]|1[012])(0[1-9]|[12][0-9]|3[01])
as year could be any value.;(
Ok, then change it to make it validate to what you want. Whatever you would consider a valid date, modify the regex to allow for it.

How well versed are you in regex's? If you aren't, this is a good time to get up to speed.

Also, there are other ways to do it. You must remember, Google is your friend.

Regards,

Jeff
Nov 17 '08 #6

Post your reply

Sign in to post your reply or Sign up for a free account.