0
import re

s = "hello,2021/11/54, world"

ans = re.search('[0-9]{4}/[0-9]{2}/[0-9]{2}' ,s)


if ans is None:
    print("no match")
else:
    print(ans.group())

i want to match date from the given string but i does not want to match for months value greater than 12 and day value greater than 31 how to do this so far i have tried this and it works perfectly fine. date is in format YYYY/MM/DD

2 Answers 2

2

I suggest using regex only to extract the various date candidates in your text. Other than this, rely on the datetime library to check the validity of the date.

import datetime
import re

def validate(date_text):
    try:
        datetime.datetime.strptime(date_text, '%Y/%m/%d')
        print(date_text + " => VALID")
    except ValueError:
        print(date_text + " => INVALID date format, should be YYYY/MM/DD")

s = "hello,2021/11/54, world"
dates = re.findall(r'\b\d{4}/\d{2}/\d{2}\b', s)
for date in dates:
    validate(date)

The reason to avoid pure regex is that there are a number of edge cases which can be difficult to accurately capture. For example, February may have 28 or 29 days, depending on whether or not it be a leap year.

Sign up to request clarification or add additional context in comments.

Comments

0

To search for the date, use the following regex command: (?:19|20)[0-9]{2}[-\\/ ]?(0?[1-9]|1[0-2])[-/ ]?(0?[1-9]|[12][0-9]|3[01])

The following code should work:

import re

s = "hello,2021/11/54, world"

ans = re.search('(?:19|20)[0-9]{2}[-\\/ ]?(0?[1-9]|1[0-2])[-/ ]?(0?[1-9]|[12][0-9]|3[01])' ,s)


if ans is None:
    print("no match")
else:
    print(ans.group())

Note: The date range is only from 1900 - 2999

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.