Time is a concept that is used to describe the duration of events or the progression of existence. It is often measured in units such as seconds, minutes, hours, days, and years. Timestamp is defined by yyyy-mm-dd hh:mm:ss In this article let’s understand how we can create a regex for time yyyy-mm-dd hh:mm:ss and how regex can be matched for a given time.
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 timestamp yyyy-mm-dd hh:mm:ss
The time should have the following criteria and structure-
- It should start with a 4 digit year
- Then followed by a hyphen
-
- Then 2 digit month
- Then followed by a hyphen
-
- Then 2 digit date
- Space to separate date and time
- Then 2 digit hour
- Then followed by a colon
:
- Then 2 digit minute
- Then followed by a colon
:
- Then 2 digit second
Regex for checking if timestamp yyyy-mm-dd hh:mm:ss is valid or not
Regular Expression for time yyyy-mm-dd hh:mm:ss is-
/^[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 String | Match Output |
---|---|
20221-12-12 31:31:12 | does not match |
32:11:43 | does not match |
2022-12-12 11:31:12 | matches |
1999-01-01 01:01:02 | matches |
3043-08-22 12:41:32 | matches |
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
^ asserts position at start of a line
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)
$ 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 time hh:mm:ss or not. In this article, we delved into the concept of time and how it’s represented using timestamps in the format yyyy-mm-dd hh:mm:ss. We explored the power of regular expressions (regex) and learned how to create a regex pattern to validate and match timestamps. Regular expressions prove invaluable in text manipulation and validation tasks, and understanding their construction enhances your programming and text processing capabilities.