Skip to content

Instantly share code, notes, and snippets.

@princeppy
Last active November 29, 2024 14:38
Show Gist options
  • Save princeppy/2c1e511d22b9a377cce41b1e1b6cf4ac to your computer and use it in GitHub Desktop.
Save princeppy/2c1e511d22b9a377cce41b1e1b6cf4ac to your computer and use it in GitHub Desktop.
dd-MMM-YYYY, dd/MMM/YYYY, dd.MMM.YYYY

Debuggex

^(?:(?:31(\/|-|\.)(?:0?[13578]|1[02]|(?:Jan|Mar|May|Jul|Aug|Oct|Dec)))\1|(?:(?:29|30)(\/|-|\.)(?:0?[1,3-9]|1[0-2]|(?:Jan|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec))\2))(?:(?:1[6-9]|[2-9]\d)?\d{2})$|^(?:29(\/|-|\.)(?:0?2|(?:Feb))\3(?:(?:(?:1[6-9]|[2-9]\d)?(?:0[48]|[2468][048]|[13579][26])|(?:(?:16|[2468][048]|[3579][26])00))))$|^(?:0?[1-9]|1\d|2[0-8])(\/|-|\.)(?:(?:0?[1-9]|(?:Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep))|(?:1[0-2]|(?:Oct|Nov|Dec)))\4(?:(?:1[6-9]|[2-9]\d)?\d{2})$

Regular expression visualization

Debuggex Date Formatter

import re
regex = re.compile("^(?:(?:31(/|\\-|\\.)(?:0{0,1}[13578]|1[02]|(?:Jan|Mar|May|Jul|Aug|Oct|Dec)))\\1|(?:(?:29|30)(/|\\-|\\.)(?:0{0,1}[1,3-9]|1[0-2]|(?:Jan|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec))\\2))(?:(?:1[6-9]|[2-9]\\d){0,1}\\d{2,2})$|^(?:29(/|\\-|\\.)(?:0{0,1}2|(?:Feb))\\3(?:(?:(?:1[6-9]|[2-9]\\d){0,1}(?:0[48]|[2468][048]|[13579][26])|(?:(?:16|[2468][048]|[3579][26])00))))$|^(?:0{0,1}[1-9]|1\\d|2[0-8])(/|\\-|\\.)(?:(?:0{0,1}[1-9]|(?:Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep))|(?:1[0-2]|(?:Oct|Nov|Dec)))\\4(?:(?:1[6-9]|[2-9]\\d){0,1}\\d{2,2})$")
testString = "" # fill this in
matchArray = regex.findall(testString)
# the matchArray variable contains the list of matches

https://regexr.com/39tr1

https://stackoverflow.com/questions/15491894/regex-to-validate-date-formats-dd-mm-yyyy-dd-mm-yyyy-dd-mm-yyyy-dd-mmm-yyyy

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment