I’m trying to make the regular expression find the numbers in a date string (eg, 04/05/1989). This is what I have:
\A0[1-9]{2}\/0[1-9]{2}\/[1900-2012]\z
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
The square brackets create a character range, not a number range:
[A-Z]matches all ASCII characters fromAtoZ.[1900-2012]matches the1, the9, the0, the range of ASCII characters between0and2, the0, the1and the2. Effectively the same can be expressed as[0-29].Within a character range expression, the order of characters is not important.
[0815]is the same as[8510]. Hence[1900]is the same as[019].You want
(19[0-9]{2}|200[0-9]|201[0-2])to match the correct year range. Or, if you just want to match four digits and don’t want range validation you can use([0-9]{4})Same for month and day. You can do it with range validation or you can do it by matching one or two digits. I’d recommend the latter.
Try to convert the result to a date value. If that fails, the input was invalid. If it succeeds, check if the resulting date falls into your required range. Regex is the wrong tool to provide date validity checks.