I have seen some posts that deal with recognizing if a possible string could be a date or not,but none of them seem to deal with if a sentence could have potential dates in it or not.
I have used the dateutil parser function which seems to be effective in recognizing dates in strings only if the date is the only component of the string.
from dateutil.parser import parse
def is_date(string, fuzzy=False):
"""
Return whether the string can be interpreted as a date.
:param string: str, string to check for date
:param fuzzy: bool, ignore unknown tokens in string if True
"""
try:
parse(string, fuzzy=fuzzy)
return True
except ValueError:
return False
>>> is_date("1990-12-1")
True
>>> is_date("foo 1990-12-1 bar")
False