How to validate time in 24-hour format using Regular Expression
Given a string str, the task is to check whether the string is valid time in 24-hour format or not by using Regular Expression.
The valid time in the 24-hour format must satisfy the following conditions.
- It should start from 0-23 or 00-23.
- It should be followed by a ‘:'(colon).
- It should be followed by two digits from 00 to 59.
- It should not end with ‘am’, ‘pm’ or ‘AM’, ‘PM’.
Examples:
Input: str = “13:05”
Output: true
Explanation: The given string satisfies all the above mentioned conditions.
Input: str = “02:15”
Output: true
Explanation: The given string satisfies all the above mentioned conditions.
Input: str = “24:00”
Output: false
Explanation: The given string doesn’t be in the range of 0-23 or 00-23(i.e., hour is out of range), therefore it is not a valid time in 24-hour format.
Input: str = “10:60”
Output: false
Explanation: The given string doesn’t be in the range of 00 to 59(i.e., minute is out of range), therefore it is not a valid time in 24-hour format.
Input: str = “10:15 PM”
Output: false
Explanation: The given string end with ‘AM’ or ‘PM’, therefore it is not a valid time in 24-hour format.
Approach: This problem can be solved by using Regular Expression.
- Get the string.
- Create a regular expression to check time in 24-hour format as mentioned below:
regex = "([01]?[0-9]|2[0-3]):[0-5][0-9]";
- Where:
- ( represents the start of the group.
- [01]?[0-9] represents the time starts with 0-9, 1-9, 00-09, 10-19.
- | represents or.
- 2[0-3] represents the time starts with 20-23.
- ) represents the end of the group.
- : represents the time should be followed by a colon(:).
- [0-5][0-9] represents the time followed by 00 to 59
- Match the given string with the regex, in Java this can be done by using Pattern.matcher().
- Return true if the string matches with the given regex, else return false.
Below is the implementation of the above approach:
C++
// C++ program to validate// time in 24-hour format// using Regular Expression#include <iostream>#include <regex>using namespace std;// Function to validate time in 24-hour formatbool isValidTime(string str){ // Regex to check valid time in 24-hour format const regex pattern("([01]?[0-9]|2[0-3]):[0-5][0-9]"); // If the time in 24-hour format // is empty return false if (str.empty()) { return false; } // Return true if the time in 24-hour format // matched the ReGex if(regex_match(str, pattern)) { return true; } else { return false; }}// Driver Codeint main(){ // Test Case 1: string str1 = "13:05"; cout << str1 + ": "<< isValidTime(str1) << endl; // Test Case 2: string str2 = "02:15"; cout << str2 + ": "<< isValidTime(str2) << endl; // Test Case 3: string str3 = "24:00"; cout << str3 + ": "<< isValidTime(str3) << endl; // Test Case 4: string str4 = "10:60"; cout << str4 + ": "<< isValidTime(str4) << endl; // Test Case 5: string str5 = "10:15 PM"; cout << str5 + ": "<< isValidTime(str5) << endl; return 0;}// This code is contributed by yuvraj_chandra |
Java
// Java program to validate the time in// 24-hour format using Regular Expression.import java.util.regex.*;class GFG { // Function to validate the time in 24-hour format public static boolean isValidTime(String time) { // Regex to check valid time in 24-hour format. String regex = "([01]?[0-9]|2[0-3]):[0-5][0-9]"; // Compile the ReGex Pattern p = Pattern.compile(regex); // If the time is empty // return false if (time == null) { return false; } // Pattern class contains matcher() method // to find matching between given time // and regular expression. Matcher m = p.matcher(time); // Return if the time // matched the ReGex return m.matches(); } // Driver Code. public static void main(String args[]) { // Test Case 1: String str1 = "13:05"; System.out.println(str1 + ": " + isValidTime(str1)); // Test Case 2: String str2 = "02:15"; System.out.println(str2 + ": " + isValidTime(str2)); // Test Case 3: String str3 = "24:00"; System.out.println(str3 + ": " + isValidTime(str3)); // Test Case 4: String str4 = "10:60"; System.out.println(str4 + ": " + isValidTime(str4)); // Test Case 5: String str5 = "10:15 PM"; System.out.println(str5 + ": " + isValidTime(str5)); }} |
Python3
# Python3 program to validate the time in# 24-hour format using Regular Expression.import re# Function to validate the time# in 24-hour formatdef isValidTime(time): # Regex to check valid time in 24-hour format. regex = "^([01]?[0-9]|2[0-3]):[0-5][0-9]$"; # Compile the ReGex p = re.compile(regex); # If the time is empty # return false if (time == "") : return False; # Pattern class contains matcher() method # to find matching between given time # and regular expression. m = re.search(p, time); # Return True if the time # matched the ReGex otherwise False if m is None : return False; else : return True # Driver Code.if __name__ == "__main__" : # Test Case 1: str1 = "13:05"; print(str1, ": ", isValidTime(str1)); # Test Case 2: str2 = "02:15"; print(str2, ": ", isValidTime(str2)); # Test Case 3: str3 = "24:00"; print(str3, ": ", isValidTime(str3)); # Test Case 4: str4 = "10:60"; print(str4, ": ", isValidTime(str4)); # Test Case 5: str5 = "10:15 PM"; print(str5, ": ", isValidTime(str5));# This code is contributed by AnkitRai01 |
13:05: true 02:15: true 24:00: false 10:60: false 10:15 PM: false
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.
In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.


