Given some Traditional Date Time Formats, the task is to check if they are valid or not using regular expressions. Rules for the valid format are:
- It should contain only digits (0 - 9) and a few words like (SEPTEMBER, Sep, SEP).
- It can only contain a few special characters like ":", and "-".
- It can only contain one white space.
Examples:
Input: "2023-01-01 01:01:01"
Output: TrueInput: "12/07/1998 12:00"
Output: False
Approach: The problem can be solved based on the following idea:
Create a regex pattern to validate the number as written below:
regex = "^([0-9]{4})-((01|02|03|04|05|06|07|08|09|10|11|12|(?:J(anuary|u(ne|ly))|February|Ma(rch|y)|A(pril|ugust)|(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)|(JANUARY|FEBRUARY|MARCH|APRIL|MAY|JUNE|JULY|AUGUST|SEPTEMBER|OCTOBER|NOVEMBER|DECEMBER)|(September|October|November|December)|(jan|feb|mar|apr|may|jun|jul|aug|sep|oct|nov|dec)|(JAN|FEB|MAR|APR|MAY|JUN|JUL|AUG|SEP|OCT|NOV|DEC)))|(january|february|march|april|may|june|july|august|september|october|november|december))-([0-3][0-9])\s([0-1][0-9]|[2][0-3]):([0-5][0-9]):([0-5][0-9])$"Where,
- ^ : Starting of the string.
- [0 - 9] {4} : 4 digits should be there
- \ : One of them should be present.
- $ : End of the string.
Follow the below steps to implement the idea:
- Create a regex expression for the Traditional DateTime Format.
- Use Pattern class to compile the regex formed.
- Use the matcher function to check whether the Format is valid or not.
- If it is valid, return true. Otherwise, return false.
Below is the implementation of the above approach:
// C++ code to validate the
// DateTime(YYYY-MM-DD HH:MM:SS) using Regular
// Expression
#include <iostream>
#include <regex>
using namespace std;
// Function to validate the
// DateTime(YYYY-MM-DD HH:MM:SS)
string isValid_DateTime(string str)
{
// Regex to check valid
// DateTime(YYYY-MM-DD HH:MM:SS).
const regex pattern(
"^([0-9]{4})-((01|02|03|04|05|06|07|08|09|10|11|12|"
"(?:J(anuary|u(ne|ly))|February|Ma(rch|y)|A(pril|"
"ugust)|(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|"
"Nov|Dec)|(JANUARY|FEBRUARY|MARCH|APRIL|MAY|JUNE|"
"JULY|AUGUST|SEPTEMBER|OCTOBER|NOVEMBER|DECEMBER)|("
"September|October|November|December)|(jan|feb|mar|"
"apr|may|jun|jul|aug|sep|oct|nov|dec)|(JAN|FEB|MAR|"
"APR|MAY|JUN|JUL|AUG|SEP|OCT|NOV|DEC)))|(january|"
"february|march|april|may|june|july|august|"
"september|october|november|december))-([0-3][0-9])"
"\\s([0-1][0-9]|[2][0-3]):([0-5][0-9]):([0-5][0-9])"
"$");
// If the str
// is empty return false
if (str.empty()) {
return "false";
}
// Return true if the str
// matched the ReGex
if (regex_match(str, pattern)) {
return "true";
}
else {
return "false";
}
}
// Driver Code
int main()
{
// Test Case 1:
string str1 = "2023-01-01 01:01:01";
cout << isValid_DateTime(str1) << endl;
// Test Case 2:
string str2 = "1998-07-12 18:01:01";
cout << isValid_DateTime(str2) << endl;
// Test Case 3:
string str3 = "1998-07-12";
cout << isValid_DateTime(str3) << endl;
// Test Case 4:
string str4 = "12/07/1998 12:00";
cout << isValid_DateTime(str4) << endl;
// Test Case 5:
string str5 = "1998-13-12 23:01:01";
cout << isValid_DateTime(str5) << endl;
return 0;
}
// Java program to validate the
// DateTime(YYYY-MM-DD HH:MM:SS) using Regex
import java.util.regex.*;
class GFG {
// Function to validate the
// DateTime(YYYY-MM-DD HH:MM:SS)
public static boolean isValid_DateTime(String str)
{
// Regex to check valid
// DateTime(YYYY-MM-DD HH:MM:SS)
String regex
= "^([0-9]{4})-((01|02|03|04|05|06|07|08|09|10|11|12|"
+ "(?:J(anuary|u(ne|ly))|February|Ma(rch|y)|"
+ "A(pril|ugust)|(Jan|Feb|Mar|Apr|May|Jun|Jul|"
+ "Aug|Sep|Oct|Nov|Dec)|(JANUARY|FEBRUARY|MARCH|"
+ "APRIL|MAY|JUNE|JULY|AUGUST|SEPTEMBER|OCTOBER|"
+ "NOVEMBER|DECEMBER)|(September|October|November|"
+ "December)|(jan|feb|mar|apr|may|jun|jul|aug|sep|"
+ "oct|nov|dec)|(JAN|FEB|MAR|APR|MAY|JUN|JUL|AUG|SEP"
+ "|OCT|NOV|DEC)))|(january|february|march|april|may|"
+ "june|july|august|september|october|november|december))"
+ "-([0-3][0-9])\\s([0-1][0-9]|[2][0-3]):([0-5][0-9])"
+ ":([0-5][0-9])$";
// Compile the ReGex
Pattern p = Pattern.compile(regex);
// If the str
// is empty return false
if (str == null) {
return false;
}
// Pattern class contains matcher()
// method to find matching between
// given str using regex.
Matcher m = p.matcher(str);
// Return if the str
// matched the ReGex
return m.matches();
}
// Driver Code.
public static void main(String args[])
{
// Test Case 1:
String str1 = "2023-01-01 01:01:01";
System.out.println(isValid_DateTime(str1));
// Test Case 2:
String str2 = "1998-07-12 18:01:01";
System.out.println(isValid_DateTime(str2));
// Test Case 3:
String str3 = "1998-07-12";
System.out.println(isValid_DateTime(str3));
// Test Case 4:
String str4 = "12/07/1998 12:00:00";
System.out.println(isValid_DateTime(str4));
// Test Case 5:
String str5 = "1998-13-12 23:01:01";
System.out.println(isValid_DateTime(str5));
}
}
# Python3 program to validate
# DateTime(YYYY-MM-DD HH:MM:SS) using Regex
import re
# Function to validate
# DateTime(YYYY-MM-DD HH:MM:SS)
def isValid_DateTime(str):
# Regex to check valid DateTime
# (YYYY-MM-DD HH:MM:SS)
regex = "^([0-9]{4})-((01|02|03|04|05|06|07|08|09|10|11|12|" \
"(?:J(anuary|u(ne|ly))|February|Ma(rch|y)|A(pril|ugust)" \
"|(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)|" \
"(JANUARY|FEBRUARY|MARCH|APRIL|MAY|JUNE|JULY|AUGUST|" \
"SEPTEMBER|OCTOBER|NOVEMBER|DECEMBER)|(September|October|" \
"November|December)|(jan|feb|mar|apr|may|jun|jul|aug|sep|" \
"oct|nov|dec)|(JAN|FEB|MAR|APR|MAY|JUN|JUL|AUG|SEP|OCT|" \
"NOV|DEC)))|(january|february|march|april|may|june|july|" \
"august|september|october|november|december))-([0-3][0-9])" \
"\\s([0-1][0-9]|[2][0-3]):([0-5][0-9]):([0-5][0-9])$"
# Compile the ReGex
p = re.compile(regex)
# If the string is empty
# return false
if (str == None):
return "false"
# Return if the string
# matched the ReGex
if(re.search(p, str)):
return "true"
else:
return "false"
# Driver code
if __name__ == '__main__':
# Test Case 1:
str1 = "2023-01-01 01:01:01"
print(isValid_DateTime(str1))
# Test Case 2:
str2 = "1998-07-12 18:01:01"
print(isValid_DateTime(str2))
# Test Case 3:
str3 = "1998-07-12"
print(isValid_DateTime(str3))
# Test Case 4:
str4 = "12 / 07 / 1998 12:00"
print(isValid_DateTime(str4))
# Test Case 5:
str5 = "1998-13-12 23:01:01"
print(isValid_DateTime(str5))
// C# program to validate the
// DateTime(YYYY-MM-DD HH:MM:SS)
//using Regular Expressions
using System;
using System.Text.RegularExpressions;
class GFG
{
// Main Method
static void Main(string[] args)
{
// Input strings to Match
// DateTime(YYYY-MM-DD HH:MM:SS)
string[] str={"2023-01-01 01:01:01","1998-07-12 18:01:01" ,
"1998-07-12","12/07/1998 12:00",
"1998-13-12 23:01:01"};
foreach(string s in str) {
Console.WriteLine( isValid_DateTime(s) ? "true" : "false");
}
Console.ReadKey(); }
// method containing the regex
public static bool isValid_DateTime(string str)
{
string strRegex = @"^([0-9]{4})-((01|02|03|04|05|06|07|08|09|10|11|12|(?:J(anuary|u(ne|ly))|February|Ma(rch|y)|A(pril|ugust)|(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)|(JANUARY|FEBRUARY|MARCH|APRIL|MAY|JUNE|JULY|AUGUST|SEPTEMBER|OCTOBER|NOVEMBER|DECEMBER)|(September|October|November|December)|(jan|feb|mar|apr|may|jun|jul|aug|sep|oct|nov|dec)|(JAN|FEB|MAR|APR|MAY|JUN|JUL|AUG|SEP|OCT|NOV|DEC)))|(january|february|march|april|may|june|july|august|september|october|november|december))-([0-3][0-9])\s([0-1][0-9]|[2][0-3]):([0-5][0-9]):([0-5][0-9])$";
Regex re = new Regex(strRegex);
if (re.IsMatch(str))
return (true);
else
return (false);
}
}
// Javascript program to validate
// DateTime(YYYY-MM-DD HH:MM:SS) using Regular Expression
// Function to validate the
// DateTime(YYYY-MM-DD HH:MM:SS)
function isValid_DateTime(str) {
// Regex to check valid
// DateTime(YYYY-MM-DD HH:MM:SS)
let regex = new RegExp(/^([0-9]{4})-((01|02|03|04|05|06|07|08|09|10|11|12|(?:J(anuary|u(ne|ly))|February|Ma(rch|y)|A(pril|ugust)|(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)|(JANUARY|FEBRUARY|MARCH|APRIL|MAY|JUNE|JULY|AUGUST|SEPTEMBER|OCTOBER|NOVEMBER|DECEMBER)|(September|October|November|December)|(jan|feb|mar|apr|may|jun|jul|aug|sep|oct|nov|dec)|(JAN|FEB|MAR|APR|MAY|JUN|JUL|AUG|SEP|OCT|NOV|DEC)))|(january|february|march|april|may|june|july|august|september|october|november|december))-([0-3][0-9])\s([0-1][0-9]|[2][0-3]):([0-5][0-9]):([0-5][0-9])$/)
// if str
// is empty return false
if (str == null) {
return "false";
}
// Return true if the str
// matched the ReGex
if (regex.test(str) == true) {
return "true";
}
else {
return "false";
}
}
// Driver Code
// Test Case 1:
let str1 = "2023-01-01 01:01:01";
console.log(isValid_DateTime(str1));
// Test Case 2:
let str2 = "1998-07-12 18:01:01";
console.log(isValid_DateTime(str2));
// Test Case 3:
let str3 = "1998-07-12";
console.log(isValid_DateTime(str3));
// Test Case 4:
let str4 = "12/07/1998 12:00";
console.log(isValid_DateTime(str4));
// Test Case 5:
let str5 = "1998-13-12 23:01:01";
console.log(isValid_DateTime(str5));
Output
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: