I'm familiar with dateutil.parser which allows one to parse a string representing a time into a datetime object. What I would like to do, however, is to 'search' for such a 'time string' within a larger string representing an interval of time. For example:
from datetime import timedelta
import dateutil.parser
import parse
start = dateutil.parser.parse("5 Nov 2016 15:00")
end = start + timedelta(hours=1)
string = "from {start} till {end}".format(start=start, end=end)
start_pattern = "from {:tg}"
result = parse.search(start_pattern, string)
I'd like to recover the start
and end
as datetime
objects based on the fact that they follow the words "from" and "till", respectively.
Here I have tried to use the parse module, but the format specifier :tg
(for global time syntax) doesn't seem to work on datetime
's default string representation, nor do the other available ones look similar to the one in string
.
What would be a simple and elegant way to parse back the start
and end
in this example?
The re
package could help you in this case; just make regular expressions for the strings you want to match, and use them to extract the date part.