This week's book giveaway is in the Android forum.
We're giving away four copies of Head First Android and have David & Dawn Griffiths on-line!
See this thread for details.
Win a copy of Head First Android this week in the Android forum!
  • 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
  • Paul Clapham
  • Ron McLeod
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Rob Spoor
  • Devaka Cooray
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Jj Roberts
  • Al Hobbs
  • Piet Souris

Solved : Regular Expression for MMYY

 
Ranch Hand
Posts: 2908
1
Spring Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,

Is it possible for a string like Month and Year combination like this "MMYY" to be validated using Regular Expression ?

I try , but did`n succeed.
This my RE.


Any Inputs ?

[ August 30, 2008: Message edited by: Sagar Rohankar ]
[ August 30, 2008: Message edited by: Sagar Rohankar ]
 
Sagar Rohankar
Ranch Hand
Posts: 2908
1
Spring Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry , but with some R&D I found a ggod soloution :

New RE for checking MMYY pattern




 
Marshal
Posts: 74354
334
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well done
[ August 30, 2008: Message edited by: Campbell Ritchie ]
 
Ranch Hand
Posts: 266
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Sagar Rohankar:
Sorry , but with some R&D I found a ggod soloution :

New RE for checking MMYY pattern







What about October?
Also note that the logical OR has a low precedence in regex, so your regex really means:



So, it would match "09" or "11" or "1208" but not "1108", for example.

Try this:


[ September 01, 2008: Message edited by: Piet Verdriet ]
 
Sheriff
Posts: 22506
122
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Also, your code allows dates such as 1189.

I'd go for SimpleDateFormat with setLenient(false).
[ August 30, 2008: Message edited by: Rob Prime ]
 
Piet Verdriet
Ranch Hand
Posts: 266
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Rob Prime:
Also, your code allows dates such as 1189.

...



Assuming that 11-'89 is an invalid date for the OP...
 
Rob Spoor
Sheriff
Posts: 22506
122
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Right, the last two digits are years, not days. My bad/
 
Sagar Rohankar
Ranch Hand
Posts: 2908
1
Spring Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Piet Verdriet:



What about October?
Also note that since the logical OR has a low presedence in regex, so your regex really means:



So, it would match "09" or "11" or "1208" but not "1108",



Gotcha , Thanks Piet , I`m partying all day assuming I got some Regular Expression skills !
But , anyways Thanks to you and Rob !!

Originally posted by Piet Verdriet:
So, it would match "09" or "11" or "1208" but not "1108",



I think, The "1108" string works !
 
Piet Verdriet
Ranch Hand
Posts: 266
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Sagar Rohankar:


I think, The "1108" string works !



I think not!





What you probably meant was this regex:



See how I rearranged the parenthesis? Of course, you will still miss years with October as the month. So this is the correct one:



which can be shortened to



as I suggested in a previous reply.

Good luck!
[ September 01, 2008: Message edited by: Piet Verdriet ]
 
Sagar Rohankar
Ranch Hand
Posts: 2908
1
Spring Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Actually I`m using Struts validation framework , which maps the input fields to validation .xml element, like this :



Here email, .., expirationDate are input fields on JSP page .[This form ultimately results in JavaScript code , which validate the input fields ]

and this code allow "number" > 12, (Which I don't want ) and Interestingly allowing "1108" , (Which supposed to be not valid as per your code) !

I changed the Regular Exp to "^(0[1-9]|1[0-2])[0-9][0-9]$" ..
[ September 01, 2008: Message edited by: Sagar Rohankar ]
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic