BeardedDork
BeardedDork

Reputation: 160

Regex matching original datetime format in python

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'

Upvotes: 0

Views: 39

Answers (1)

Mal Sund
Mal Sund

Reputation: 98

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 '

Upvotes: 1

Related Questions