Solved

How to use OR condition in regular expression

Posted on 2010-08-25
15
685 Views
Last Modified: 2013-11-11
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 ??
0
Comment
  • 4
  • 4
  • 3
  • +1
15 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 33520747
You'd probably be better off using two DateFormat
0
 
LVL 20

Author Comment

by:Sathish David Kumar N
ID: 33520910
is not pssoble do in that single expression ??
0
 
LVL 10

Expert Comment

by:Hegemon
ID: 33521010
\d?\d

for the M or MM part
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 75

Assisted Solution

by:Michel Plungjan
Michel Plungjan earned 100 total points
ID: 33521018
/^\d{4}\/\d{1,2}\/\d{2}$/
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 33521206
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

0
 
LVL 86

Accepted Solution

by:
CEHJ earned 400 total points
ID: 33529392
The accepted answer will of course allow

2010/13/01

If you want js, try


String RE = /\d{4}\/([1-9]|1[12]|0[1-9])\/\d{2}/;

Open in new window

0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 33534066
And why not points to me? It is a valid answer to your question :)
0
 
LVL 20

Author Closing Comment

by:Sathish David Kumar N
ID: 33534540
@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
0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 33535562
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
0
 
LVL 10

Expert Comment

by:Hegemon
ID: 33536477
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.

0
 
LVL 86

Expert Comment

by:CEHJ
ID: 33537058
Yes, i'm not saying for one minute that mine will prevent invalid dates - i was just focusing on the month
0
 
LVL 20

Author Comment

by:Sathish David Kumar N
ID: 33537065
@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
 
0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 33539352
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

0
 
LVL 10

Expert Comment

by:Hegemon
ID: 33540144
-  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.
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
PHP: Filling Out/Creating a PDF 29 94
Bootstrap question 4 26
Add - in a loop to first and last element. 6 33
AngularJS: ng-repeat 25 19
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
This video teaches viewers about errors in exception handling.

685 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question