1

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.

1 Answer 1

1

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")
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.