• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Regex date matching

 
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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..
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Or
 
Henry Wong
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Jim Yingst:
Or



Nice...
 
Lasagna is spaghetti flvored cake. Just like this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic