Published on

# Regex for mm-dd-yyyy

A date is a specific day or time period, often given as a combination of a month, date, and year. In this article let's understand how we can create a regex for date mm-dd-yyyy, mm/dd/yyyy and mm.dd.yyyy and how regex can be matched for a given date.

Regex (short for regular expression) is a powerful tool used for searching and manipulating text. It is composed of a sequence of characters that define a search pattern. Regex can be used to find patterns in large amounts of text, validate user input, and manipulate strings. It is widely used in programming languages, text editors, and command line tools.

# Structure of a mm-dd-yyyy or mm/dd/yyyy or mm.dd.yyyy

The date should have the following criteria and structure-

• Date can be d or dd - where d is a digit
• Month can be m or mm - where m is a digit
• Year can be yy or yyyy - where y is a digit
• Supported separators are - or / or .
• Supported date formats are mm-dd-yyyy, mm/dd/yyyy, mm.dd.yyyy
• Leap year is supported

# Regex for checking if mm-dd-yyyy is valid or not

Regular Expression for mm-dd-yyyy or mm/dd/yyyy or mm.dd.yyyy with Leap year support is-

/^((0[13578]|1[02])(\/|-|\.)(0[1-9]|[12][0-9]|3[01])(\/|-|\.)(18|19|20)[0-9]{2})|((0[469]|11)(\/|-|\.)(0[1-9]|[12][0-9]|30)(\/|-|\.)(18|19|20)[0-9]{2})|((02)(\/|-|\.)(0[1-9]|1[0-9]|2[0-8])(\/|-|\.)(18|19|20)[0-9]{2})|((02)(\/|-|\.)29(\/|-|\.)(((18|19|20)(04|08|[2468][048]|[13579][26]))|2000))$/gm  Test string examples for the above regex- Input StringMatch Output 33-43-12does not match 332-122-21does not match 06-12-2022matches 02/11/2022matches 12.11.2022matches Here is a detailed explanation of the above regex- /^((0[13578]|1[02])(\/|-|\.)(0[1-9]|[12][0-9]|3[01])(\/|-|\.)(18|19|20)[0-9]{2})|((0[469]|11)(\/|-|\.)(0[1-9]|[12][0-9]|30)(\/|-|\.)(18|19|20)[0-9]{2})|((02)(\/|-|\.)(0[1-9]|1[0-9]|2[0-8])(\/|-|\.)(18|19|20)[0-9]{2})|((02)(\/|-|\.)29(\/|-|\.)(((18|19|20)(04|08|[2468][048]|[13579][26]))|2000))$/gm

1st Alternative ((0[13578]|1[02])(\/|-|\.)(0[1-9]|[12][0-9]|3[01])(\/|-|\.)(18|19|20)[0-9]{2})
1st Capturing Group ((0[13578]|1[02])(\/|-|\.)(0[1-9]|[12][0-9]|3[01])(\/|-|\.)(18|19|20)[0-9]{2})
2nd Capturing Group (0[13578]|1[02])
1st Alternative 0[13578]
0 matches the character 0 with index 4810 (3016 or 608) literally (case sensitive)
Match a single character present in the list below [13578]
13578 matches a single character in the list 13578 (case sensitive)
2nd Alternative 1[02]
1 matches the character 1 with index 4910 (3116 or 618) literally (case sensitive)
Match a single character present in the list below [02]
02 matches a single character in the list 02 (case sensitive)
3rd Capturing Group (\/|-|\.)
1st Alternative \/
\/ matches the character / with index 4710 (2F16 or 578) literally (case sensitive)
2nd Alternative -
- matches the character - with index 4510 (2D16 or 558) literally (case sensitive)
3rd Alternative \.
\. matches the character . with index 4610 (2E16 or 568) literally (case sensitive)
4th Capturing Group (0[1-9]|[12][0-9]|3[01])
1st Alternative 0[1-9]
0 matches the character 0 with index 4810 (3016 or 608) literally (case sensitive)
Match a single character present in the list below [1-9]
1-9 matches a single character in the range between 1 (index 49) and 9 (index 57) (case sensitive)
2nd Alternative [12][0-9]
Match a single character present in the list below [12]
12 matches a single character in the list 12 (case sensitive)
Match a single character present in the list below [0-9]
0-9 matches a single character in the range between 0 (index 48) and 9 (index 57) (case sensitive)
3rd Alternative 3[01]
3 matches the character 3 with index 5110 (3316 or 638) literally (case sensitive)
Match a single character present in the list below [01]
01 matches a single character in the list 01 (case sensitive)
5th Capturing Group (\/|-|\.)
1st Alternative \/
2nd Alternative -
3rd Alternative \.
6th Capturing Group (18|19|20)
1st Alternative 18
18 matches the characters 18 literally (case sensitive)
2nd Alternative 19
19 matches the characters 19 literally (case sensitive)
3rd Alternative 20
20 matches the characters 20 literally (case sensitive)
Match a single character present in the list below [0-9]
{2} matches the previous token exactly 2 times
0-9 matches a single character in the range between 0 (index 48) and 9 (index 57) (case sensitive)
2nd Alternative ((0[469]|11)(\/|-|\.)(0[1-9]|[12][0-9]|30)(\/|-|\.)(18|19|20)[0-9]{2})
7th Capturing Group ((0[469]|11)(\/|-|\.)(0[1-9]|[12][0-9]|30)(\/|-|\.)(18|19|20)[0-9]{2})
3rd Alternative ((02)(\/|-|\.)(0[1-9]|1[0-9]|2[0-8])(\/|-|\.)(18|19|20)[0-9]{2})
13th Capturing Group ((02)(\/|-|\.)(0[1-9]|1[0-9]|2[0-8])(\/|-|\.)(18|19|20)[0-9]{2})
4th Alternative ((02)(\/|-|\.)29(\/|-|\.)(((18|19|20)(04|08|[2468][048]|[13579][26]))|2000))
19th Capturing Group ((02)(\/|-|\.)29(\/|-|\.)(((18|19|20)(04|08|[2468][048]|[13579][26]))|2000))
Global pattern flags
g modifier: global. All matches (don't return after first match)
m modifier: multi line. Causes ^ and \$ to match the begin/end of each line (not only begin/end of string)


Hope this article was useful to check if the string is a valid mm-dd-yyyy or not.