• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Regex date matching

 
Chad Clites
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Chad Clites
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Chad Clites
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 21021
78
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Or
 
Henry Wong
author
Marshal
Pie
Posts: 21021
78
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Jim Yingst:
Or


Nice...
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic