Refactor regex function and convert datetime