Computer >> Computer tutorials >  >> Programming >> Python

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


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