How do we use Python regular expression to match a date string?

PythonServer Side ProgrammingProgramming

In the first case in the code below the given date string matches the d-m-y format and in the second case the date string does not match the format

Example

import re
datestring = '21-09-1991'
foo =re.match('(\d{2})[/.-](\d{2})[/.-](\d{4})$', datestring)
print foo.group()
datestring = '1991-09-21'
foo =re.match('(\d{2})[/.-](\d{2})[/.-](\d{4})$', datestring)
print foo

Output

21-09-1991
None
raja
Published on 11-Jan-2018 12:00:54
Advertisements