Forum Moderators: coopster & phranque

Message Too Old, No Replies

Perl Regex for Date Validation

Has anybody something in his/her drawer?

         

waldemar

9:40 am on Aug 11, 2003 (gmt 0)

10+ Year Member



Well I'm sitting on this homework far too long I can't figure it out; hopefully there is some perl pros out here?

I'm looking for a simple regex that verifies a date in the format DD/MM/YYYY.

^([0123][0-9])\/([01][0-2])\/[12][09][0-9][0-9] seems to be the right way... Is it possible to validate that the 00 < DAY <= 31 and 00 < MONTH <=12?

claus

9:58 am on Aug 11, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



^(0[1-9]¦[12][0-9]¦3[01])\/(0[1-9]¦1[0-2])\/[12][0-9][0-9][0-9]$

-i put in a trailing dollar sign to mark the end of the string. It does just what you asked for and no more (ie. this string will not match for dates in year 3000 and beyond.)

You may want to replace "\/" with a dot "." to allow for other separators, ie. "-" or "."

/claus


Edit: typo

Added: included this link, there's a very advanced regexp on top that also validates the dates (leap years and month leghts) from 1/1/1600 - 12/31/9999:

[regexlib.com...]


Added2

Note: Remember to replace the broken pipe "¦" with one you enter from your keyboard - this board replaces the real pipes with a character that just looks like them but is not the same (it should not be broken)

[edited by: claus at 10:12 am (utc) on Aug. 11, 2003]

waldemar

10:08 am on Aug 11, 2003 (gmt 0)

10+ Year Member



Hi claus, and thanks!

I understand now how the pipe can be used. Unfortunately it seems that the environment I working in (Interwoven Teamsite) does not seem to accept that OR :-(( -> can't create valid dates...

But thanks anyway for the lesson :-) and... that great link!

claus

10:13 am on Aug 11, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I think my "added2" note just fixed that - i wrote it while you were answering it seems :)

Added:
Validate regexps here: [regexlib.com...]

/claus

waldemar

4:13 pm on Aug 11, 2003 (gmt 0)

10+ Year Member



Interesting... no matter which one of your regexes I am trying out with that later link... they all result in "no match"... :-(
am I missing something essential?

waldemar

4:29 pm on Aug 11, 2003 (gmt 0)

10+ Year Member



Annoying enough, the only regex that seems to work in this environ. is ^[0-3][0-9]\/[0-1][0-9]\/[1-2][0-9][0-9][0-9]$ :-))))

claus

9:40 pm on Aug 11, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



>> no matter which one of your regexes I am trying out with that later link

- what? It works perfectly fine for me, i'm really sad to hear this... otherwise i wouldn't have posted it in the first place... are you sure:

(1) you remembered to replace all three broken pipes "¦" with a real one entered from your keyboard?

(2) that you have also removed any preceding or trailing spaces?

(3) that you are using the format "DD/MM/YYYY"?

(4) that you are using dates in the range you specified?

(5) that these dates are between 01/01/1000 and 31/12/2999?

(6) that you write no other thinghs in the "Text To Check:" field than a date following (3), (4), and (5) - not even spaces.

I am sure that one of these six is the problem. I do not know Interwowen Teamsite, but i am alomost 100% sure that it does not interfere with the processes taking place at regexplib.com's servers, so that's probably not the reason. Try very carefully to examine each of these six steps, i'm sure it works, and i just re-tested it without problems.

>> only regex that seems to work in this environ. is ^[0-3][0-9]\/[0-1][0-9]

Not just annoying, but really annoying, because that means that you will accept dates like 39/19/2003 ... we must get this solved for you somehow...

/claus