Link to home
Start Free TrialLog in
Avatar of Sathish David  Kumar N
Sathish David Kumar NFlag for India

asked on

How to use OR condition in regular expression

Hi,
How to use OR condition in regular expression

my req is
YYYY/MM/DD format if the user enter  YYYY/M/DD also i want to accept ?
eg:
YYYY/MM/DD---- 2010/08/19
YYYY/M/DD  ---- 2010/8/19
in regular expression i worte YYYY/MM/DD
YYYY/MM/DD ---- /^\d{4}\/\(d){2}\/\d{2}$/
but i dont know how to but or condition ??

How can i do that ??
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

You'd probably be better off using two DateFormat
Avatar of Sathish David  Kumar N

ASKER

is not pssoble do in that single expression ??
\d?\d

for the M or MM part
SOLUTION
Avatar of Michel Plungjan
Michel Plungjan
Flag of Denmark image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
The following is strict for months - you could take the same line for days
String RE = "\\d{4}/(?:[1-9]|1[12]|0[1-9])/\\d{2}";

Open in new window

ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
And why not points to me? It is a valid answer to your question :)
@mplungjan  
2010/13/01

if i give 13 for month it will accept  for your sloution so its wrong right ?

Thanks
@CEHJ
I didnt think in that way  , Thanks
True :)

You will need to validate the dates ANYWAY

Since CEHJ's code will allow 29/02/2011
and 08 and 09 are invalid octal and so on
Yet another example of unclear question and points assigned in no relation to original question.

The question was about how to enter either one or two digits. No validation mentioned, no language specified and the question posted in two zones - Java Programming Language and JavaScript. In my view, in the context of this question the zones are mutually exclusive as the asker needs to solve the problem in either of the two languages but not both.

I assumed it was Java and my answer is correct for Java.

Yes, i'm not saying for one minute that mine will prevent invalid dates - i was just focusing on the month
@Hegemon:
>>> The question was about how to enter either one or two digits. No validation mentioned
Thanks for time and answer .
I clearly mention that its YYYY/MM/DD , so iam taking about year,date ,month so
no need for validation rule bcz all are know month should 1 to 12 only .
i think your tech guy ....  thanks again
 
You do know that
30/02/
31/02/
 and outside leapyears 29/02/2011
and
31/4, 31/6. 31/9 and 31/11 are invalid too?

PS: Hegemon I think a LOT of regexps will work in JS and JAVA both

-  Hegemon I think a LOT of regexps will work in JS and JAVA both

I thought this too but dravidnsr said mine did not work at all (although I tested it in Java) and the first CEHJ's solution did not work in JS, so there are probably differences.