0

I've crafted a regex to find unique datetime formats. I want to substitute out invalid datetimes (where the month is not 1 through 12).

i.e.

10-2019  (NO MATCH)
2-2020 (NO MATCH)
19-2019 (MATCH because 2-digit is not 1-12)

I do not care about the 4-digit number. Thus, I've derived this regex: \b(0|00|1[3-9]|[2-9][0-9])\b-\d{4}

However, I'm not receiving any matches:

>>> x = '00-1421 a 15-1432'
>>> re.sub('\b(0|00|1[3-9]|[2-9][0-9])\b-\d{4}','',x)
'00-1421 a 15-1432'

1 Answer 1

1
p = re.compile(r'\b(0|00|1[3-9]|[2-9]\d)-\d{4}')
x = '00-1421 a 15-1432'
p.sub('', x)

output:

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

1 Comment

Ah, I missed the \d at the end of the 2 digit block. Thanks!

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.