• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Solved : Regular Expression for MMYY

 
Sagar Rohankar
Ranch Hand
Posts: 2906
1
Java Spring Ubuntu
  • Mark post as helpful
  • send pies
  • 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: 2906
1
Java Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry , but with some R&D I found a ggod soloution :

New RE for checking MMYY pattern




 
Campbell Ritchie
Sheriff
Posts: 48382
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well done
[ August 30, 2008: Message edited by: Campbell Ritchie ]
 
Piet Verdriet
Ranch Hand
Posts: 266
  • Mark post as helpful
  • send pies
  • 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 ]
 
Rob Spoor
Sheriff
Pie
Posts: 20494
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • 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
  • 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
Pie
Posts: 20494
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Right, the last two digits are years, not days. My bad/
 
Sagar Rohankar
Ranch Hand
Posts: 2906
1
Java Spring Ubuntu
  • Mark post as helpful
  • send pies
  • 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
  • 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: 2906
1
Java Spring Ubuntu
  • Mark post as helpful
  • send pies
  • 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 ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic