What am i doing wrong in the below regular expression matching
>>> import re
>>> d="30-12-2001"
>>> re.findall(r"\b[1-31][/-:][1-12][/-:][1981-2011]\b",d)
[]
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.
[1-31]matches1-3and1which is basically 1, 2 or 3. You cannot match a number rage unless it’s a subset of 0-9. Same applies to[1981-2011]which matches exactly one character that is 0, 1, 2, 8 or 9.The best solution is simply matching any number and then checking the numbers later using python itself. A date such as
31-02-2012would not make any sense – and making your regex check that would be hard. Making it also handle leap years properly would make it even harder or impossible. Here’s a regex matching anything that looks like add-mm-yyyydate:\b\d{1,2}[-/:]\d{1,2}[-/:]\d{4}\bHowever, I would highly suggest not allowing any of
-,:and/as:is usually used for times,/usually for the US way of writing a date (mm/dd/yyyy) and-for the ISO way (yyyy-mm-dd). The EUdd.mm.yyyysyntax is not handled at all.If the string does not contain anything but the date, you don’t need a regex at all – use
strptime()instead.All in all, tell the user what date format you expect and parse that one, rejecting anything else. Otherwise you’ll get ambiguous cases such as
04/05/2012(is it april 5th or may 4th?).