Solved

Regular expression for Year Format

Posted on 2013-05-16
9
371 Views
Last Modified: 2013-06-12
Hi,
I am using VS2010 and C#.Net
I am trying to validate the years entered in a TextBox.
It can be two formats.Ex:2013-2012(fourdigitshyphenfourdigits) or 2009(fourdigits)

I am trying this expression and it is failing for 2011 @"20[01]\d *- *20[01]\d";

The first digit should be Two and second digit is zero and third digit and fourth digit can be any thing 0-9.
I tested for 2013-2012 and it worked fine. But it is not working for 2011.

Thanks
0
Comment
Question by:KavyaVS
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 4
9 Comments
 
LVL 21

Assisted Solution

by:Mazdajai
Mazdajai earned 400 total points
ID: 39172006
Try
20\d\d(-20\d\d)?

Open in new window

0
 

Author Comment

by:KavyaVS
ID: 39172157
I tried this and it worked fine.
Please check is there any mistake in it.
20\d\d *(- *20\d\d)?

Thanks
0
 
LVL 21

Expert Comment

by:Mazdajai
ID: 39172206
Not sure why you need the asterisk based on your requirement?

It can be two formats.Ex:2013-2012(fourdigitshyphenfourdigits) or 2009(fourdigits)
The first digit should be Two and second digit is zero and third digit and fourth digit can be any thing 0-9.
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 75

Assisted Solution

by:käµfm³d 👽
käµfm³d   👽 earned 100 total points
ID: 39196570
I agree. The pattern "20\d\d *(- *20\d\d)?" also accepts the following (probably not what you want):

200000000
20000-2008
20000----------2008
200
200-2008
200--------------2008
0
 

Author Comment

by:KavyaVS
ID: 39209380
Empty spaces before and after the Hyphen causing the problems.I added space asterisk before and after the hyphen which means 0 or more spaces.
How to validate empty spaces without adding the *.

Thanks
0
 
LVL 21

Expert Comment

by:Mazdajai
ID: 39209484
I think we are still unsure what you mean, what is the expected output? You can add \s? to make empty space optional.
20\d\d(\s?-\s?20\d\d)? 

Open in new window

It would be helpful if you can provide expected output.
0
 

Author Comment

by:KavyaVS
ID: 39223230
I tested the  3 patterns.Three not working.
"20\d\d *(- *20\d\d)?"
"20\d\d(\s?-\s?20\d\d)?"
"20\d\d(-20\d\d)?"
Yes. The pattern "20\d\d *(- *20\d\d)?" accepting the following:
200000000
20000-2008
20000----------2008
200-2008
200--------------2008

And this pattern 20\d\d(\s?-\s?20\d\d)?  also accepts the following:
200000000
20000-2008
20000----------2008
200-2008
200--------------2008

And even this pattern(with out considering empty space before or after hyphen) 20\d\d(-20\d\d)? also accepting the following:
200000000
20000-2008
20000----------2008
200-2008
200--------------2008


I want this:
The entered text in the Text Box can be two formats.Ex:2013-2012(fourdigitshyphenfourdigits) or 2009(fourdigits)


The first digit should be Two and second digit is zero and third digit and fourth digit can be any thing 0-9.
In case if there are optional single empty space before and after hyphen I want to accept it.

Thanks.
0
 
LVL 21

Accepted Solution

by:
Mazdajai earned 400 total points
ID: 39223448
^20\d\d\s?(-\s?20[0-9][0-9])?$

Open in new window

0
 

Author Closing Comment

by:KavyaVS
ID: 39242726
Thanks
0

Featured Post

Technology Partners: 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!

Question has a verified solution.

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

User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

630 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