| Author |
Solved : Regular Expression for MMYY
|
Sagar Rohankar
Ranch Hand
Joined: Feb 19, 2008
Posts: 2896
|
|
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 ]
|
[LEARNING bLOG] | [Freelance Web Designer] | [and "Rohan" is part of my surname]
|
 |
Sagar Rohankar
Ranch Hand
Joined: Feb 19, 2008
Posts: 2896
|
|
Sorry , but with some R&D I found a ggod soloution : New RE for checking MMYY pattern
|
 |
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32648
|
|
Well done  [ August 30, 2008: Message edited by: Campbell Ritchie ]
|
 |
Piet Verdriet
Ranch Hand
Joined: Feb 25, 2006
Posts: 266
|
|
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
Joined: Oct 27, 2005
Posts: 19216
|
|
Also, your code allows dates such as 1189. I'd go for SimpleDateFormat with setLenient(false). [ August 30, 2008: Message edited by: Rob Prime ]
|
SCJP 1.4 - SCJP 6 - SCWCD 5
How To Ask Questions How To Answer Questions
|
 |
Piet Verdriet
Ranch Hand
Joined: Feb 25, 2006
Posts: 266
|
|
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
Joined: Oct 27, 2005
Posts: 19216
|
|
|
Right, the last two digits are years, not days. My bad/
|
 |
Sagar Rohankar
Ranch Hand
Joined: Feb 19, 2008
Posts: 2896
|
|
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
Joined: Feb 25, 2006
Posts: 266
|
|
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
Joined: Feb 19, 2008
Posts: 2896
|
|
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 ]
|
 |
 |
|
|
subject: Solved : Regular Expression for MMYY
|
|
|