aspose file tools*
The moose likes Beginning Java and the fly likes Regex date matching Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Regex date matching" Watch "Regex date matching" New topic
Author

Regex date matching

Chad Clites
Ranch Hand

Joined: Aug 16, 2005
Posts: 134
This has to be so simple. All I am trying to do is validate a date formatted as either 00/00/00 or 00-00-00, which is what I thought I was trying to do.



I was also trying the following expressions:

Could somebody slap me with a cold fish and tell me what it is that I am really doing? For some reason I am completely baffled..
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

The regex patterns seem okay (as far as just matching patterns, without validating whether the numbers are within range).

The problem looks like it's the 2 logical not's (!) in your if statement. You seem to have "no match or no match --> valid date." But I think you just want "match or match --> valid date."

[ May 12, 2006: Message edited by: marc weber ]

"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
Chad Clites
Ranch Hand

Joined: Aug 16, 2005
Posts: 134
I guess what I was trying to get at was that if it didn't match the pattern, then display an error message. So I was thinking that if not pattern a or not pattern b, then display an error... That was supposed to say "date not valid". The problem I was having was that no matter what I put in for the date, it was dropping into the if/then.

Maybe I have a rogue space in there somewhere that is confusing things...Thanks for giving a look. Your way is much cleaner.

[ May 12, 2006: Message edited by: Chad Clites ]
[ May 12, 2006: Message edited by: Chad Clites ]
Garrett Rowe
Ranch Hand

Joined: Jan 17, 2006
Posts: 1296
Logical boolean operator give me fits too. According to DeMorgans Law:

((!a) || (!b)) is equivalent to (!(a && b));

Since your string could not possibly match both regular expressions, the condition always evaluated to true, and consequently fell through the the then block. I think what you wanted was:

((!a) && (!b));

but you're right, marc's solution is a little clearer.
[ May 12, 2006: Message edited by: Garrett Rowe ]

Some problems are so complex that you have to be highly intelligent and well informed just to be undecided about them. - Laurence J. Peter
Chad Clites
Ranch Hand

Joined: Aug 16, 2005
Posts: 134
That worked quite nicely Garrett. I'm not sure I understand the difference between (not a or not b) and (not a and not b).. too Aristotlean for my current mindset maybe. It appears I have forgotten my principles of logic.

Thank you both.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18896
    
  40

Another option is to match it with a single regular expression.


String reg = "(\\d{2}/\\d{2}/\\d{2})|(\\d{2}-\\d{2}-\\d{2})";


Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
Or


"I'm not back." - Bill Harding, Twister
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18896
    
  40

Originally posted by Jim Yingst:
Or


Nice...
 
Don't get me started about those stupid light bulbs.
 
subject: Regex date matching