Search code examples
pythonstringdatenamed-entity-recognition

How do I get the text after a date using python?


I have a string which might have a date of some form inside it.

I want to extract the portion appearing just after that date.

For example:

s = "The term starts on Jan 1, 2000 and terminates on"

should return

output = "and terminates on"

My suggestion would be

s[s.find(', 2') + 6]

Solution

  • Try using dateutil with fuzzy_with_tokens=True

    Ex:

    from dateutil.parser import parse
    s = "The term starts on Jan 1, 2000 and terminates on"
    print(parse(s, fuzzy_with_tokens=True))
    

    Output:

    (datetime.datetime(2000, 1, 1, 0, 0), ('The term starts on ', ' ', ' ', 'and terminates on'))