1

I am wondering how could I check if a date is within a string in Python. Imagine I have the following string:

str = 'yesterday it was a sunny day here in Boston; I went to the park and I played with 3 dogs; 2007-03-01'

I am trying to find a way to check if my string has a date on it but I couldn't find a way to do it, I thought of checking the numbers but that wouldn't work as I have many possibilities and numbers could be outside the date as in the string above.

What I have thought so far is to do something like:

str.split(';')

which would give me:

['yesterday it was a sunny day here in Boston', ' I went to the park and I played with 3 dogs', ' 2007-03-01']

But still I couldn't figure out a way to check if some of the elements in this new list is a date, as all of them seem to be strings. Is there any function in Python that allows to do so? Or could you give me any clue to achieve it?

I have also checked this Check if string has date, any format, but I am not trying to check if my string could be a date, but finding the date itself.

Marisa
  • 1,135
  • 3
  • 20
  • 33
  • check the `datetime` module! – Cut7er Aug 31 '18 at 09:49
  • 2
    @Marisa you should re-read https://stackoverflow.com/questions/25341945 more carefully, as it contains all you need to solve your own issue. – bruno desthuilliers Aug 31 '18 at 10:03
  • @brunodesthuilliers No the link doesn't contain what the OP has asked. Read more **carefully**. OP wants to detect if a substring of a larger string is a date or note. What the other question does is, it does not check for a substring but check whether the whole string is a date or not. – abhishah901 Dec 02 '20 at 04:52

0 Answers0