Hi All,
can anyone suggest how to validate a date (yyyymmdd format) using pattern match?
5 10305
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).
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: }
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.
I did somthing like this (validating only MMDD) - (0[1-9]|1[012])(0[1-9]|[12][0-9]|3[01])
as year could be any value.;(
I did somthing like this (validating only MMDD) - (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
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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?
...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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$) {
}
...
|
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...
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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,...
|
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,...
| |