aspose file tools*
The moose likes HTML, CSS and JavaScript and the fly likes problem with regular expression for date formate Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Bookmark "problem with regular expression for date formate " Watch "problem with regular expression for date formate " New topic
Author

problem with regular expression for date formate

sirisha makkapati
Ranch Hand

Joined: Dec 10, 2006
Posts: 83
Hi to all,

Can any body give me regular expression for date formate
mm/dd/yyyy hh:mm:ss A.M or P.M
Eric Pascarello
author
Rancher

Joined: Nov 08, 2001
Posts: 15376
    
    6
what have you come up with?

Eric
javark rkjava
Greenhorn

Joined: Jan 25, 2008
Posts: 4
(?n:^(?=\d)((?<month>(0?[13578])|1[02]|(0?[469]|11)
(?!.31)|0?2(?(.29)(?=.29.((1[6-9]|[2-9]\d)
(0[48]|[2468][048]|[13579][26])|(16|[2468][048]|[3579][26])00))
|(?!.3[01])))(?<sep>[-./])
(?<day>0?[1-9]|[12]\d|3[01])
\k<sep>(?<year>(1[6-9]|[2-9]\d)\d{2})
(?(?=\x20\d)\x20|$))?(?<time>((0?[1-9]|1[012])
(:[0-5]\d){0,2}(?i:\x20[AP]M))|([01]\d|2[0-3])(:[0-5]\d){1,2})?$)


[I added line breaks so the page is not billion characters wide - Eric]
[ January 25, 2008: Message edited by: Eric Pascarello ]
Eric Pascarello
author
Rancher

Joined: Nov 08, 2001
Posts: 15376
    
    6
Persoanlly I would not try to figure out the 30/31/leap year stuff in the regular expression. I would let a second check figure it out. If you HAVE to do it in a regular expression it will be yucky. I will code the whole thing here for fun durning my lunch.

mm/dd/yyyy

space

hh:mm:ss

space

A.M or P.M [I left the . there, I do not know anyone that does that!]


From here I will build a function that tests for a valid date. It will test to make sure that someone does not set 31 days in the wrong month.



With that function, we can combine it with our regular expression and build another function that can do the total valid date/string format



So now we have the function. How do we use this thing?



I just copied all of the code I wrote here over to a test page to see if it works and I got true, false, false, false so it is looking good.

If you need to have the regular expression validate everything without a second check, it will be hard, but if we break it up into parts like I did above it maybe a little easier. Need to figure out patterns for 29/30/31 and for leap years.

Eric
[ January 25, 2008: Message edited by: Eric Pascarello ]
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15299
    
    6

Eric is insane.


GenRocket - Experts at Building Test Data
Brian Cole
Author
Ranch Hand

Joined: Sep 20, 2005
Posts: 862
Originally posted by Eric Pascarello:
A.M or P.M [I left the . there, I do not know anyone that does that!]


This is mostly off-topic, I know, but some people use dots and some don't. If you're going to use a dot, though, be sure to use two of them (A.M. or P.M.).

I think everyone agrees that a dot after A (or P) but not after the M would be in error.

references: one, two


bitguru blog
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: problem with regular expression for date formate