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 dd-mm-yyyy, dd/mm/yyyy and dd.mm.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 dd-mm-yyyy or dd/mm/yyyy or dd.mm.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 dd-mm-yyyy, dd/mm/yyyy, dd.mm.yyyy
- Leap year is supported
Regex for checking if dd-mm-yyyy is valid or not
Regular Expression for dd-mm-yyyy or dd/mm/yyyy or dd.mm.yyyy with Leap year support is-
/^(?:(?:31(\/|-|\.)(?:0?[13578]|1[02]))\1|(?:(?:29|30)(\/|-|\.)(?:0?[13-9]|1[0-2])\2))(?:(?:1[6-9]|[2-9]\d)?\d{2})$|^(?:29(\/|-|\.)0?2\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])|(?:1[0-2]))\4(?:(?:1[6-9]|[2-9]\d)?\d{2})$/gm
Test string examples for the above regex-
Input String | Match Output |
---|---|
33-43-12 | does not match |
1-2-22 | matches |
332-122-21 | does not match |
02-12-2022 | matches |
2-11-22 | matches |
2/11/22 | matches |
2.11.22 | matches |
Here is a detailed explanation of the above regex-
/^(?:(?:31(\/|-|\.)(?:0?[13578]|1[02]))\1|(?:(?:29|30)(\/|-|\.)(?:0?[13-9]|1[0-2])\2))(?:(?:1[6-9]|[2-9]\d)?\d{2})$|^(?:29(\/|-|\.)0?2\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])|(?:1[0-2]))\4(?:(?:1[6-9]|[2-9]\d)?\d{2})$/gm
1st Alternative ^(?:(?:31(\/|-|\.)(?:0?[13578]|1[02]))\1|(?:(?:29|30)(\/|-|\.)(?:0?[13-9]|1[0-2])\2))(?:(?:1[6-9]|[2-9]\d)?\d{2})$
^ asserts position at start of a line
Non-capturing group (?:(?:31(\/|-|\.)(?:0?[13578]|1[02]))\1|(?:(?:29|30)(\/|-|\.)(?:0?[13-9]|1[0-2])\2))
1st Alternative (?:31(\/|-|\.)(?:0?[13578]|1[02]))\1
Non-capturing group (?:31(\/|-|\.)(?:0?[13578]|1[02]))
31 matches the characters 31 literally (case sensitive)
1st 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)
Non-capturing group (?:0?[13578]|1[02])
1st Alternative 0?[13578]
0 matches the character 0 with index 4810 (3016 or 608) literally (case sensitive)
? matches the previous token between zero and one times, as many times as possible, giving back as needed (greedy)
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)
\1 matches the same text as most recently matched by the 1st capturing group
2nd Alternative (?:(?:29|30)(\/|-|\.)(?:0?[13-9]|1[0-2])\2)
Non-capturing group (?:(?:29|30)(\/|-|\.)(?:0?[13-9]|1[0-2])\2)
Non-capturing group (?:29|30)
1st Alternative 29
29 matches the characters 29 literally (case sensitive)
2nd Alternative 30
30 matches the characters 30 literally (case sensitive)
2nd Capturing Group (\/|-|\.)
1st Alternative \/
\/ matches the character / with index 4710 (2F16 or 578) literally (case sensitive)
2nd Alternative -
3rd Alternative \.
Non-capturing group (?:0?[13-9]|1[0-2])
1st Alternative 0?[13-9]
2nd Alternative 1[0-2]
\2 matches the same text as most recently matched by the 2nd capturing group
Non-capturing group (?:(?:1[6-9]|[2-9]\d)?\d{2})
Non-capturing group (?:1[6-9]|[2-9]\d)?
\d matches a digit (equivalent to [0-9])
$ asserts position at the end of a line
2nd Alternative ^(?:29(\/|-|\.)0?2\3(?:(?:(?:1[6-9]|[2-9]\d)?(?:0[48]|[2468][048]|[13579][26])|(?:(?:16|[2468][048]|[3579][26])00))))$
^ asserts position at start of a line
Non-capturing group (?:29(\/|-|\.)0?2\3(?:(?:(?:1[6-9]|[2-9]\d)?(?:0[48]|[2468][048]|[13579][26])|(?:(?:16|[2468][048]|[3579][26])00))))
$ asserts position at the end of a line
3rd Alternative ^(?:0?[1-9]|1\d|2[0-8])(\/|-|\.)(?:(?:0?[1-9])|(?:1[0-2]))\4(?:(?:1[6-9]|[2-9]\d)?\d{2})$
^ asserts position at start of a line
Non-capturing group (?:0?[1-9]|1\d|2[0-8])
4th Capturing Group (\/|-|\.)
Non-capturing group (?:(?:0?[1-9])|(?:1[0-2]))
\4 matches the same text as most recently matched by the 4th capturing group
Non-capturing group (?:(?:1[6-9]|[2-9]\d)?\d{2})
$ asserts position at the end of a line
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 dd-mm-yyyy or not. In this article, we delved into the world of regular expressions (regex) and explored how to create a powerful regex pattern for validating date formats like dd-mm-yyyy, dd/mm/yyyy, and dd.mm.yyyy. Understanding the structure of these date formats, along with leap year support, allowed us to construct a comprehensive regex pattern. We also provided detailed explanations and test cases to ensure a clear understanding of the pattern’s functionality. Regular expressions prove to be a valuable tool for text manipulation and validation, making them an essential skill for developers, programmers, and anyone working with text data.