Python regex match date

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



Python regex match date



What regular expression in Python do i use to match dates like this: "11/12/98"?




4 Answers
4



Instead of using regex, it is generally better to parse the string as a datetime.datetime object:


datetime.datetime


In [140]: datetime.datetime.strptime("11/12/98","%m/%d/%y")
Out[140]: datetime.datetime(1998, 11, 12, 0, 0)

In [141]: datetime.datetime.strptime("11/12/98","%d/%m/%y")
Out[141]: datetime.datetime(1998, 12, 11, 0, 0)



You could then access the day, month, and year (and hour, minutes, and seconds) as attributes of the datetime.datetime object:


datetime.datetime


In [143]: date.year
Out[143]: 1998

In [144]: date.month
Out[144]: 11

In [145]: date.day
Out[145]: 12



To test if a sequence of digits separated by forward-slashes represents a valid date, you could use a try..except block. Invalid dates will raise a ValueError:


try..except


ValueError


In [159]: try:
.....: datetime.datetime.strptime("99/99/99","%m/%d/%y")
.....: except ValueError as err:
.....: print(err)
.....:
.....:
time data '99/99/99' does not match format '%m/%d/%y'



If you need to search a longer string for a date,
you could use regex to search for digits separated by forward-slashes:


In [146]: import re
In [152]: match = re.search(r'(d+/d+/d+)','The date is 11/12/98')

In [153]: match.group(1)
Out[153]: '11/12/98'



Of course, invalid dates will also match:


In [154]: match = re.search(r'(d+/d+/d+)','The date is 99/99/99')

In [155]: match.group(1)
Out[155]: '99/99/99'



To check that match.group(1) returns a valid date string, you could then parsing it using datetime.datetime.strptime as shown above.


match.group(1)


datetime.datetime.strptime





Sorry, my mistake, I've tested it against 4 digits :)
– Bronek
Aug 26 '14 at 20:46





You should use the strptime wherever possible since its build for this purpose.
– rahul tyagi
Jul 22 at 10:37



I find the below RE working fine for Date in the following format;



It can accept year from 2000-2099



Please do not forget to add $ at the end,if not it accept 14-11-201 or 20177


date="13-11-2017"

x=re.search("^([1-9] |1[0-9]| 2[0-9]|3[0-1])(.|-)([1-9] |1[0-2])(.|-|)20[0-9][0-9]$",date)

x.group()



output = '13-11-2017'



Using this regular expression you can validate different kinds of Date/Time samples, just a little change is needed.



^dddd/(0?[1-9]|1[0-2])/(0?[1-9]|[12][0-9]|3[01]) (00|[0-9]|1[0-9]|2[0-3]):([0-9]|[0-5][0-9]):([0-9]|[0-5][0-9])$ -->validate this: 2018/7/12 13:00:00


^dddd/(0?[1-9]|1[0-2])/(0?[1-9]|[12][0-9]|3[01]) (00|[0-9]|1[0-9]|2[0-3]):([0-9]|[0-5][0-9]):([0-9]|[0-5][0-9])$



for your format you cad change it to:



^(0?[1-9]|[12][0-9]|3[01])/(0?[1-9]|1[0-2])/dd$ --> validates this: 11/12/98


^(0?[1-9]|[12][0-9]|3[01])/(0?[1-9]|1[0-2])/dd$



Well, from my understanding, simply for matching this format in a given string, I prefer this regular expression:



pattern='[0-9|/]+'



to match the format in a more strict way, the following works:



pattern='(?:[0-9]2/)2[0-9]2'



Personally, I cannot agree with unutbu's answer since sometimes we use regular expression for "finding" and "extract", not only "validating".






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Firebase Auth - with Email and Password - Check user already registered

Dynamically update html content plain JS

How to determine optimal route across keyboard