473,385 Members | 2,013 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Date validation using Pattern matching

bytesonfire
Hi All,

can anyone suggest how to validate a date (yyyymmdd format) using pattern match?
Nov 14 '08 #1
5 10305
Ganon11
3,652 Expert 2GB
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
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
4,059 Expert 2GB
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
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
3,509 Expert Mod 2GB
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

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

Similar topics

8
by: Eric Linders | last post by:
Hi, I'm trying to figure out the most efficient method for taking the first character in a string (which will be a number), and use it as a variable to check to see if the other numbers in the...
8
by: gsv2com | last post by:
One of my weaknesses has always been pattern matching. Something I definitely need to study up on and maybe you guys can give me a pointer here. I'm looking to remove all of this code and just...
176
by: Thomas Reichelt | last post by:
Moin, short question: is there any language combining the syntax, flexibility and great programming experience of Python with static typing? Is there a project to add static typing to Python? ...
9
by: Xah Lee | last post by:
# -*- coding: utf-8 -*- # Python # Matching string patterns # # Sometimes you want to know if a string is of # particular pattern. Let's say in your website # you have converted all images...
14
by: Steve Wright | last post by:
I am trying to validate if an entry on a form is a date. I have adapted code I found here http://www.codingforums.com/archive/index.php/t-14325 as below but I can't seem the get the results that I...
67
by: Scott Meyers | last post by:
I have a web site that, due to maintenance by several people, some of whom are fairly clueless about HTML and CSS, etc. (notably me), has gotten to the point where I'm pretty sure it's suffering...
3
by: CyberLotus | last post by:
Hi, I wish to validate the date a user has entered against the format dd-mmm-yyyy using the CompareValidator, but it does not work. Does anybody have a simple solution please? Many thanks...
2
by: Ole Nielsby | last post by:
First, bear with my xpost. This goes to comp.lang.c++ comp.lang.functional with follow-up to comp.lang.c++ - I want to discuss an aspect of using C++ to implement a functional language, and...
4
by: Nathan Sokalski | last post by:
When determining whether a String can be converted to a DateTime, you can use the IsDate() method. However, I would also like to know whether the string is a date, a time, or both a date and a...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.