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