I am dealing with strings that need to be converted into dates in Python. In a normal situation, my string would have %d/%m/%Y %H:%M:%S. For instance:
18/02/2013 09:21:14
However in some occasion I could obtain something like %d/%m/%Y %H:%M:%S:%ms, such as:06/01/2014 09:52:14:78
I would like to get rid of that ms bit but I need to figure out how. I have been able to create a regular expression which can test if the date matches:
mydate = re.compile("^((((31\/(0?[13578]|1[02]))|((29|30)\/(0?[1,3-9]|1[0-2])))\/(1[6-9]|[2-9]\d)?\d{2})|(29\/0?2\/(((1[6-9]|[2-9]\d)?(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))))|(0?[1-9]|1\d|2[0-8])\/((0?[1-9])|(1[0-2]))\/((1[6-9]|[2-9]\d)?\d{2})) (20|21|22|23|[0-1]?\d):[0-5]?\d:[0-5]?\d$")
s = "06/01/2014 09:52:14:78"
bool(mydate.match(s))
>>> False
However I do not know how to obtain only the interesting part, i.e 06/01/2014 09:52:14
Any ideas?

trytostrptimethe string? If it fails,rsplitto remove the last part andtryagain.