date matching using python regex
Question: 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)
    []
    


Answer: `[1-31]` matches `1-3` and `1` which 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-2012` would 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 a `dd-mm-yyyy` date:
`\b\d{1,2}[-/:]\d{1,2}[-/:]\d{4}\b`

However, 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 EU `dd.mm.yyyy`
syntax 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()`](http://docs.python.org/library/datetime.html#strftime-strptime-
behavior) 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?).

