Possible Duplicate:
Regular Expression to describe Credit Card expiry (valid thru) date
I’m trying to find a regex pattern to validate a 4 digit credit card expiration date. The format is YYMM
I tried [12-99]{2}[0-12]{2}
but this allows 00 as a month
So i guess I really need a way to match the 3rd and 4th digits with a number between 0 and 12 while accounting for a leading zero
Any ideas?
[12-99]means1or2-9or9, which is equivalent to[1-9]. Same for the second one[0-12]means0-1or2, which is equivalent to[0-2].You could go for something like this:
As you can see, checking numeric ranges is a bit of a hassle with regular expressions. If you are using this in a programming language (as opposed to a tool or an XSD restriction), you should probably just check for
(\d\d)(\d\d)and then validate the numeric ranges of the two captures using integer inequality operators of your programming language. This will also allow you to make the lower boundary of the valid years dependent on the current year (so that, come 2013,12is no longer allowed for the first two digits, without you needing to change the code).