Okay, this is amazingly cloddish, but it does work and may point out where some of your regexes are going wrong. Whenever I have difficulty, I try to simplify it down and then analyze.
@strings = qw/xxxx 2004 200412 20041201 2004-12 2004-12-01/;
for (@strings){
if (/^\d{4}$|^\d{4}-\d{2}-\d{2}$|^\d{4}-\d{2}$/){
print "$_ matches\n";
}
else{
print "$_ does not match\n";
}
}
Sam Gardner
GTO Application Development
Keefe, Bruyette & Woods, Inc.
212-887-6753
-----Original Message-----
From: Dirk Bremer [mailto:[EMAIL PROTECTED]]
Sent: Thursday, January 27, 2005 11:43 AM
To: perl-win32-users
Subject: Regex Help
I need to test for the following date-type strings:
2004
2004-12
2004-12-01
All of the above would be legal, any other combination would be illegal.
I have been using the following data to test the regex:
'xxxx' Should produce a false result.
'2004' Should produce a true result.
'200412' Should produce a false result.
'20041201' Should produce a false result.
'2004-12' Should produce a true result.
'2004-12-01' Should produce a true result.
My first attempt at a regex is /\d{4}(-\d{2})*/. This produces the following result:
xxxx is false
2004 is true
200412 is true
20041201 is true
2004-12 is true
2004-12-01 is true
I would prefer that '200412' and '20041201' would fail, i.e. be reported as false. I'm not sure what's going on with this regex as I would expect the following:
match the first four numeric characters, then match zero or more occurrences of a dash character followed by two numeric characters
The next regex /\d{4}(-\d{2})+/ produces the following results:
xxxx is false
2004 is false
200412 is false
20041201 is false
2004-12 is true
2004-12-01 is true
This is closer, but I also want '2004' to be true.
The next regex /\d{4}(?=-\d{2})/ produces the same results as the previous regex:
xxxx is false
2004 is false
200412 is false
20041201 is false
2004-12 is true
2004-12-01 is true
I've done quite a few regexes in the past but have never used the positive lookahead assertion before because I have never really understood it.
Out of the three regexes, I would have expected the first to fulfill my requirements. I do not understand why it is not, although I suspect it has something to do with the dash character.
What am I doing wrong here? I should be able to accomplish this test using a single regex, right?
Dirk Bremer - Systems Programmer II - ESS/AMS - NISC St. Peters USA Central Time Zone 636-922-9158 ext. 8652 fax 636-447-4471
[EMAIL PROTECTED]
www.nisc.cc
_______________________________________________
Perl-Win32-Users mailing list Perl-Win32-Users@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
_______________________________________________ Perl-Win32-Users mailing list Perl-Win32-Users@listserv.ActiveState.com To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs