Validating traditional time formats using Regular Expression
Last Updated :
22 Dec, 2022
Given a string Time Format, the task is to check whether the string follows the time formats “HH: MM: SS” or “HH: MM” using Regular Expression. Rules for valid time formats:
- It should contain only digits[0-9] and colon (:).
- No alphabets are allowed or any special characters.
Examples:
Input: “12: 55”
Output: True
Input: “24: 12: 12”
Output: False
Explanation: The standard time is never expressed 24:00:00 hours or anything else after that.
Approach: The problem can be solved based on the following idea:
Create a regex pattern to validate the number as written below:
regex= “^(?:[01]?[0-9]|2[0-3]):[0-5]?[0-9](?::[0-5]?[0-9])?$“
^ : Beginning of the string
?: : not compulsion to be present
[01] : indicates either 1 or 0
[0 – 9] : any one digit from 0 to 9 should be present.
\ : either first part before ” or second part after ”.
Follow the below steps to implement the idea:
- Create regex expression for Time formats.
- Use Pattern class to compile the regex formed.
- Use the matcher function to check whether the Time format is valid or not.
- If it is valid, return true. Otherwise, return false.
Below is the implementation of the above approach.
C++
#include <bits/stdc++.h>
#include <regex>
using namespace std;
string isValidTime(string str)
{
const regex pattern( "^(?:[01]?[0-9]|2[0-3]):[0-5]?[0-9]"
"(?::[0-5]?[0-9])?$" );
if (str.empty()) {
return "false" ;
}
if (regex_match(str, pattern)) {
return "true" ;
}
else {
return "false" ;
}
}
int main()
{
string str1 = "23:01:00" ;
cout << isValidTime(str1) << endl;
string str2 = "12:59:00" ;
cout << isValidTime(str2) << endl;
string str3 = "19:00:59" ;
cout << isValidTime(str3) << endl;
string str4 = "19:00:96" ;
cout << isValidTime(str4) << endl;
string str5 = "08:78:70" ;
cout << isValidTime(str5) << endl;
string str6 = "00:98:00" ;
cout << isValidTime(str6) << endl;
return 0;
}
|
Java
import java.util.regex.*;
class GFG {
public static boolean isValidTime(String str)
{
String regex
= "^(?:[01]?[0-9]|2[0-3]):[0-5]?[0-9](?::[0-5]?[0-9])?$" ;
Pattern p = Pattern.compile(regex);
if (str == null ) {
return false ;
}
Matcher m = p.matcher(str);
return m.matches();
}
public static void main(String args[])
{
String str1 = "23:10:59" ;
System.out.println(isValidTime(str1));
String str2 = "12:59:00" ;
System.out.println(isValidTime(str2));
String str3 = "19:00:59" ;
System.out.println(isValidTime(str3));
String str4 = "19:00:96" ;
System.out.println(isValidTime(str4));
String str5 = "08:78:70" ;
System.out.println(isValidTime(str5));
String str6 = "00:98:00" ;
System.out.println(isValidTime(str6));
}
}
|
Python3
import re
def isValidTime( str ):
regex = "^(?:[01]?[0-9]|2[0-3]):[0-5]?[0-9](?::[0-5]?[0-9])?$"
p = re. compile (regex)
if ( str = = None ):
return "false"
if (re.search(p, str )):
return "true"
else :
return "false"
if __name__ = = '__main__' :
str1 = "23:01:00"
print (isValidTime(str1))
str2 = "12:59:00"
print (isValidTime(str2))
str3 = "19:00:59"
print (isValidTime(str3))
str4 = "19:00:96"
print (isValidTime(str4))
str5 = "08:78:70"
print (isValidTime(str5))
str6 = "00:98:00"
print (isValidTime(str6))
|
C#
using System;
using System.Text.RegularExpressions;
public class GFG
{
public static bool isValidTime(String str)
{
var regex = new Regex( "^(?:[01]?[0-9]|2[0-3]):[0-5]?[0-9](?::[0-5]?[0-9])?$" );
if (str == null )
{
return false ;
}
var m = regex.Match(str);
return m.Success;
}
public static void Main(String[] args)
{
var str1 = "23:10:59" ;
Console.WriteLine(GFG.isValidTime(str1));
var str2 = "12:59:00" ;
Console.WriteLine(GFG.isValidTime(str2));
var str3 = "19:00:59" ;
Console.WriteLine(GFG.isValidTime(str3));
var str4 = "19:00:96" ;
Console.WriteLine(GFG.isValidTime(str4));
var str5 = "08:78:70" ;
Console.WriteLine(GFG.isValidTime(str5));
var str6 = "00:98:00" ;
Console.WriteLine(GFG.isValidTime(str6));
}
}
|
Javascript
function isValidTime(str) {
let regex = new RegExp(/^(?:[01]?[0-9]|2[0-3]):[0-5]?[0-9](?::[0-5]?[0-9])?$/);
if (str == null ) {
return "false" ;
}
if (regex.test(str) == true ) {
return "true" ;
}
else {
return "false" ;
}
}
let str1 = "23:01:00" ;
console.log(isValidTime(str1));
let str2 = "12:59:00" ;
console.log(isValidTime(str2));
let str3 = "19:00:59" ;
console.log(isValidTime(str3));
let str4 = "19:00:96" ;
console.log(isValidTime(str4));
let str5 = "08:78:70" ;
console.log(isValidTime(str5));
let str6 = "00:98:00" ;
console.log(isValidTime(str6));
|
Output
true
true
true
false
false
false
Time Complexity: O(N) for each testcase, where N is the length of the given string.
Auxiliary Space: O(1)
Related Articles:
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...