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. A time zone is a geographic region that has adopted the same standard time for legal, commercial, and social purposes. In this article let’s understand how we can create a regex for timezone and how regex can be matched for a given timezone.
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 timezone
The time should have the following criteria and structure-
- It should start with either
+
or-
- It will be followed by two digits for hours
- It will be followed by
:
- It will be followed by two digits for minutes
- Other special characters are not allowed
Regex for checking if timezone is valid or not
Regular Expression for timezone is-
/^(?:Z|[+-](?:2[0-3]|[01][0-9]):[0-5][0-9])$/gm
Test string examples for the above regex-
Input String | Match Output |
---|---|
02:20 | does not match |
+33:11 | does not match |
+05:30 | matches |
-10:00 | matches |
+00:00 | matches |
Here is a detailed explanation of the above regex-
/^(?:Z|[+-](?:2[0-3]|[01][0-9]):[0-5][0-9])$/gm
^ asserts position at start of the string
Non-capturing group (?:Z|[+-](?:2[0-3]|[01][0-9]):[0-5][0-9])
1st Alternative Z
Z matches the character Z with index 9010 (5A16 or 1328) literally (case sensitive)
2nd Alternative [+-](?:2[0-3]|[01][0-9]):[0-5][0-9]
Match a single character present in the list below [+-]
+- matches a single character in the list +- (case sensitive)
Non-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)
$ 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 timezone or not. In conclusion, time and time zones play a crucial role in our lives, facilitating coordination across the globe. Regular expressions (regex) provide a powerful tool to validate and manipulate time zone information efficiently. By understanding the structure of time zones and crafting appropriate regex patterns, we can accurately identify valid time zone representations. Incorporating regex into programming, text processing, and data validation tasks empowers us to work effectively with time-related data.