How to validate time in 24 | C ++ program to validate time in 24 - hour format using Regular Expression ; Function to validate time in 24 - hour format ; Regex to check valid time in 24 - hour format ; If the time in 24 - hour format is empty return false ; Return true if the time in 24 - hour format matched the ReGex ; Driver Code ; Test Case 1 : ; Test Case 2 : ; Test Case 3 : ; Test Case 4 : ; Test Case 5 :
#include <iostream> NEW_LINE #include <regex> NEW_LINE using namespace std ; bool isValidTime ( string str ) { const regex pattern ( " ( [01 ] ? [0-9 ] ▁ 2[0-3 ] ) : [ 0-5 ] [ 0-9 ] " ) ; if ( str . empty ( ) ) { return false ; } if ( regex_match ( str , pattern ) ) { return true ; } else { return false ; } } int main ( ) { string str1 = "13:05" ; cout << str1 + " : ▁ " << isValidTime ( str1 ) << endl ; string str2 = "02:15" ; cout << str2 + " : ▁ " << isValidTime ( str2 ) << endl ; string str3 = "24:00" ; cout << str3 + " : ▁ " << isValidTime ( str3 ) << endl ; string str4 = "10:60" ; cout << str4 + " : ▁ " << isValidTime ( str4 ) << endl ; string str5 = "10:15 ▁ PM " ; cout << str5 + " : ▁ " << isValidTime ( str5 ) << endl ; return 0 ; }