Sorry I am a beginner in python and regex
I have a date string:
date_string = '2018-05-01 00:00:00'
I would like to check that date_string is in the right format with a regex which is the case in our example
I believe the regex for this format is:
(\d{4})-(\d{2})-(\d{2}) (\d{2}):(\d{2}):(\d{2})
I would just like to check if a date corresponds to this regex:
Example : for date_string = '2018-05 00:00:00'
=> variable has the wrong format
Thank you.
CodePudding user response:
Your regex is fine. To do this in practice, you might use re.search
:
date_string = '2018-05-01 00:00:00'
if re.search(r'^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}$', date_string):
print("VALID") # prints VALID
else:
print("INVALID")