DebugPointer
Published on

Regex for yyyy-mm-dd hh:mm:ss

Regex for yyyy-mm-dd hh:mm:ss

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 yyyymmddhhmmss 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 yyyymmddhhmmss

The date should have the following criteria and structure-

  • Date is dd - where d is a digit
  • Month is mm - where m is a digit
  • Year is yyyy - where y is a digit
  • Hour is hh - where h is a digit
  • Minute is mm - where m is a digit
  • Second is ss - where s is a digit
  • Supported date formats are yyyymmddhhmmss
  • Leap year is supported

Simple regex for checking if yyyy-mm-dd hh:mm:ss is valid or not (with Leap Year support)

/^[0-9]{4}-(0[1-9]|1[0-2])-(0[1-9]|[1-2][0-9]|3[0-1]) (2[0-3]|[01][0-9]):[0-5][0-9]:[0-5][0-9]$/gm

Test string examples for the above regex-

Input StringMatch Output
33-43-12does not match
2022-12-12 22:11:22matches
332-122-21 33:12:12does not match
2992-12-12 13:12:34matches

Here is a detailed explanation of the above regex-

/^[0-9]{4}-(0[1-9]|1[0-2])-(0[1-9]|[1-2][0-9]|3[0-1]) (2[0-3]|[01][0-9]):[0-5][0-9]:[0-5][0-9]$/gm

Match a single character present in the list below [0-9]
{4} matches the previous token exactly 4 times
0-9 matches a single character in the range between 0 (index 48) and 9 (index 57) (case sensitive)
- matches the character - with index 4510 (2D16 or 558) literally (case sensitive)
1st Capturing Group (0[1-9]|1[0-2])
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 1[0-2]
1 matches the character 1 with index 4910 (3116 or 618) literally (case sensitive)
Match a single character present in the list below [0-2]
0-2 matches a single character in the range between 0 (index 48) and 2 (index 50) (case sensitive)
- matches the character - with index 4510 (2D16 or 558) literally (case sensitive)
2nd Capturing Group (0[1-9]|[1-2][0-9]|3[0-1])
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 [1-2][0-9]
Match a single character present in the list below [1-2]
1-2 matches a single character in the range between 1 (index 49) and 2 (index 50) (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[0-1]
3 matches the character 3 with index 5110 (3316 or 638) literally (case sensitive)
Match a single character present in the list below [0-1]
0-1 matches a single character in the range between 0 (index 48) and 1 (index 49) (case sensitive)
  matches the character   with index 3210 (2016 or 408) literally (case sensitive)
3rd Capturing Group (2[0-3]|[01][0-9])
1st Alternative 2[0-3]
2 matches the character 2 with index 5010 (3216 or 628) literally (case sensitive)
Match a single character present in the list below [0-3]
0-3 matches a single character in the range between 0 (index 48) and 3 (index 51) (case sensitive)
2nd Alternative [01][0-9]
Match a single character present in the list below [01]
01 matches a single character in the list 01 (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)
: matches the character : with index 5810 (3A16 or 728) literally (case sensitive)
Match a single character present in the list below [0-5]
0-5 matches a single character in the range between 0 (index 48) and 5 (index 53) (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)
: matches the character : with index 5810 (3A16 or 728) literally (case sensitive)
Match a single character present in the list below [0-5]
0-5 matches a single character in the range between 0 (index 48) and 5 (index 53) (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)
Global pattern flags
m modifier: multi line. Causes ^ and $ to match the begin/end of each line (not only begin/end of string)
g modifier: global. All matches (don't return after first match)

Regex for yyyymmddhhmmss with Leap year support

Regular Expression for yyyymmddhhmmss 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
1-2-22matches
332-122-21does not match
02-12-2022matches
2-11-22matches
2/11/22matches
2.11.22matches

Here is a detailed explanation of the above regex-

/^(^(((\d\d)(([02468][048])|([13579][26]))0229)|(((\d\d)(\d\d)))((((0[1-9])|(1[0-2]))((0[1-9])|(1\d)|(2[0-8])))|((((0[13578])|(1[02]))31)|(((0[1,3-9])|(1[0-2]))(29|30)))))(([01]\d|2[0-3])([0-5]\d)([0-5]\d))$)$/gm

1st Capturing Group (^(((\d\d)(([02468][048])|([13579][26]))0229)|(((\d\d)(\d\d)))((((0[1-9])|(1[0-2]))((0[1-9])|(1\d)|(2[0-8])))|((((0[13578])|(1[02]))31)|(((0[1,3-9])|(1[0-2]))(29|30)))))(([01]\d|2[0-3])([0-5]\d)([0-5]\d))$)
^ asserts position at start of the string
2nd Capturing Group (((\d\d)(([02468][048])|([13579][26]))0229)|(((\d\d)(\d\d)))((((0[1-9])|(1[0-2]))((0[1-9])|(1\d)|(2[0-8])))|((((0[13578])|(1[02]))31)|(((0[1,3-9])|(1[0-2]))(29|30)))))
1st Alternative ((\d\d)(([02468][048])|([13579][26]))0229)
3rd Capturing Group ((\d\d)(([02468][048])|([13579][26]))0229)
4th Capturing Group (\d\d)
\d matches a digit (equivalent to [0-9])
\d matches a digit (equivalent to [0-9])
5th Capturing Group (([02468][048])|([13579][26]))
1st Alternative ([02468][048])
6th Capturing Group ([02468][048])
Match a single character present in the list below [02468]
02468 matches a single character in the list 02468 (case sensitive)
Match a single character present in the list below [048]
048 matches a single character in the list 048 (case sensitive)
2nd Alternative ([13579][26])
7th Capturing Group ([13579][26])
Match a single character present in the list below [13579]
13579 matches a single character in the list 13579 (case sensitive)
Match a single character present in the list below [26]
26 matches a single character in the list 26 (case sensitive)
0229 matches the characters 0229 literally (case sensitive)
2nd Alternative (((\d\d)(\d\d)))((((0[1-9])|(1[0-2]))((0[1-9])|(1\d)|(2[0-8])))|((((0[13578])|(1[02]))31)|(((0[1,3-9])|(1[0-2]))(29|30))))
8th Capturing Group (((\d\d)(\d\d)))
9th Capturing Group ((\d\d)(\d\d))
10th Capturing Group (\d\d)
\d matches a digit (equivalent to [0-9])
\d matches a digit (equivalent to [0-9])
11th Capturing Group (\d\d)
\d matches a digit (equivalent to [0-9])
\d matches a digit (equivalent to [0-9])
12th Capturing Group ((((0[1-9])|(1[0-2]))((0[1-9])|(1\d)|(2[0-8])))|((((0[13578])|(1[02]))31)|(((0[1,3-9])|(1[0-2]))(29|30))))
1st Alternative (((0[1-9])|(1[0-2]))((0[1-9])|(1\d)|(2[0-8])))
13th Capturing Group (((0[1-9])|(1[0-2]))((0[1-9])|(1\d)|(2[0-8])))
14th Capturing Group ((0[1-9])|(1[0-2]))
1st Alternative (0[1-9])
2nd Alternative (1[0-2])
17th Capturing Group ((0[1-9])|(1\d)|(2[0-8]))
1st Alternative (0[1-9])
2nd Alternative (1\d)
3rd Alternative (2[0-8])
2nd Alternative ((((0[13578])|(1[02]))31)|(((0[1,3-9])|(1[0-2]))(29|30)))
21st Capturing Group ((((0[13578])|(1[02]))31)|(((0[1,3-9])|(1[0-2]))(29|30)))
31st Capturing Group (([01]\d|2[0-3])([0-5]\d)([0-5]\d))
32nd Capturing Group ([01]\d|2[0-3])
33rd Capturing Group ([0-5]\d)
34th Capturing Group ([0-5]\d)
$ asserts position at the end of the string, or before the line terminator right at the end of the string (if any)

Hope this article was useful to check if the string is a valid yyyymmddhhmmss name or not.