How to extract date from text using Python regular expression?

PythonServer Side ProgrammingProgramming

The following code using Python regex extracts the date from given string

Example

import datetime
from datetime import date
import re
s = "Jason's birthday is on 1991-09-21"
match = re.search(r'\d{4}-\d{2}-\d{2}', s)
date = datetime.datetime.strptime(match.group(), '%Y-%m-%d').date()
print date

Output

This gives the output

1991-09-21
raja
Published on 11-Jan-2018 08:19:54
Advertisements