Validate week days using Regular Expression
Given some Weekdays, the task is to check if they are valid or not using regular expressions.
Rules for the valid Weekdays :
- It should contain specific only words as a string. They are mentioned below:
- Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, and Sunday.
- Mon, Tues, Wed, Thurs, Fri, Sat, Sun.
- Mon., Tues., Wed., Thurs., Fri., Sat., Sun.
- mon, tues, wed, thurs, fri, sat, sun.
Examples:
Input: “Monday”
Output: True
Input: Payday
Output: False
Approach: The problem can be solved using regular expression based on the following idea:
Create a regex pattern to validate the number as written below:
regex= “^(sun|Sun|mon|Mon|t(ues|hurs)|(T(ues|hurs))|Fri|fri)(day|\.)?$|wed(\.|nesday)?$|Wed(\.|nesday)?$|Sat(\.|urday)?$|sat(\.|urday)?$|t((ue?)|(hu?r?))\.?$|T((ue?)|(hu?r?))\.?$“
Where,
- ^ : This indicates the start of the string.
- $ :End of the string.
- | : OR
Follow the below steps to implement the idea:
- Create a regex expression for Weekdays.
- Use Pattern class to compile the regex formed.
- Use the matcher function to check whether the Weekday 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 isValid_WeekDay(string str)
{
const regex pattern(
"^(sun|Sun|mon|Mon|t(ues|hurs)|(T(ues|hurs))|Fri|"
"fri)(day|\.)?$|wed(\.|nesday)?$|Wed(\.|nesday)?$|"
"Sat(\.|urday)?$|sat(\.|urday)?$|t((ue?)|(hu?r?))"
"\.?$|T((ue?)|(hu?r?))\.?$" );
if (str.empty()) {
return "false" ;
}
if (regex_match(str, pattern)) {
return "true" ;
}
else {
return "false" ;
}
}
int main()
{
string str1 = "Sunday" ;
cout << isValid_WeekDay(str1) << endl;
string str2 = "Monday" ;
cout << isValid_WeekDay(str2) << endl;
string str3 = "Tues." ;
cout << isValid_WeekDay(str3) << endl;
string str4 = "Payday" ;
cout << isValid_WeekDay(str4) << endl;
string str5 = "Friday" ;
cout << isValid_WeekDay(str5) << endl;
string str6 = "Birthday" ;
cout << isValid_WeekDay(str6) << endl;
return 0;
}
|
Java
import java.util.regex.*;
class GFG {
public static boolean isValid_WeekDay(String str)
{
String regex
= "^(sun|Sun|mon|Mon|t(ues|hurs)|(T(ues|hurs))|Fri|fri)(day|\\.)"
+ "?$|wed(\\.|nesday)?$|Wed(\\.|nesday)?$|Sat(\\.|urday)"
+ "?$|sat(\\.|urday)?$|t((ue?)|(hu?r?))\\.?$|T((ue?)|(hu?r?))\\.?$" ;
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 = "Sunday" ;
System.out.println(isValid_WeekDay(str1));
String str2 = "Monday" ;
System.out.println(isValid_WeekDay(str2));
String str3 = "Tues." ;
System.out.println(isValid_WeekDay(str3));
String str4 = "Payday" ;
System.out.println(isValid_WeekDay(str4));
String str5 = "Friday" ;
System.out.println(isValid_WeekDay(str5));
String str6 = "Birthday" ;
System.out.println(isValid_WeekDay(str6));
}
}
|
Python3
import re
def isValid_WeekDay( str ):
regex = "^(sun|Sun|mon|Mon|t(ues|hurs)|(T(ues|hurs))|Fri|fri)(day|\.)" \
"?$|wed(\.|nesday)?$|Wed(\.|nesday)?$|Sat(\.|urday)?$|sat(\.|urday)" \
"?$|t((ue?)|(hu?r?))\.?$|T((ue?)|(hu?r?))\.?$"
p = re. compile (regex)
if ( str = = None ):
return "false"
if (re.search(p, str )):
return "true"
else :
return "false"
if __name__ = = '__main__' :
str1 = "Sunday"
print (isValid_WeekDay(str1))
str2 = "Monday"
print (isValid_WeekDay(str2))
str3 = "Tues."
print (isValid_WeekDay(str3))
str4 = "Payday"
print (isValid_WeekDay(str4))
str5 = "Friday"
print (isValid_WeekDay(str5))
str6 = "Birthday"
print (isValid_WeekDay(str6))
|
C#
using System;
using System.Text.RegularExpressions;
public class GFG
{
public static bool isValid_WeekDay(String str)
{
var regex = new Regex( "^(sun|Sun|mon|Mon|t(ues|hurs)|(T(ues|hurs))|Fri|fri)(day|\\.)"
+ "?$|wed(\\.|nesday)?$|Wed(\\.|nesday)?$|Sat(\\.|urday)"
+ "?$|sat(\\.|urday)?$|t((ue?)|(hu?r?))\\.?$|T((ue?)|(hu?r?))\\.?$" );
if (str == null )
{
return false ;
}
var m = regex.Match(str);
return m.Success;
}
public static void Main(String[] args)
{
var str1 = "Sunday" ;
Console.WriteLine(GFG.isValid_WeekDay(str1));
var str2 = "Monday" ;
Console.WriteLine(GFG.isValid_WeekDay(str2));
var str3 = "Tues." ;
Console.WriteLine(GFG.isValid_WeekDay(str3));
var str4 = "Payday" ;
Console.WriteLine(GFG.isValid_WeekDay(str4));
var str5 = "Friday" ;
Console.WriteLine(GFG.isValid_WeekDay(str5));
var str6 = "Birthday" ;
Console.WriteLine(GFG.isValid_WeekDay(str6));
}
}
|
Javascript
function isValid_WeekDay(str) {
let regex = new RegExp(/^(sun|Sun|mon|Mon|t(ues|hurs)|(T(ues|hurs))|Fri|fri)(day|\.)?$|wed(\.|nesday)?$|Wed(\.|nesday)?$|Sat(\.|urday)?$|sat(\.|urday)?$|t((ue?)|(hu?r?))\.?$|T((ue?)|(hu?r?))\.?$/);
if (str == null ) {
return "false" ;
}
if (regex.test(str) == true ) {
return "true" ;
}
else {
return "false" ;
}
}
let str1 = "Sunday" ;
console.log(isValid_WeekDay(str1));
let str2 = "Monday" ;
console.log(isValid_WeekDay(str2));
let str3 = "Tues." ;
console.log(isValid_WeekDay(str3));
let str4 = "Payday" ;
console.log(isValid_WeekDay(str4));
let str5 = "Friday" ;
console.log(isValid_WeekDay(str5));
let str6 = "Birthday" ;
console.log(isValid_WeekDay(str6));
|
Output
true
true
true
false
true
false
Time Complexity: O(N) for each testcase, where N is the length of the given string.
Auxiliary Space: O(1)
Related Articles:
Last Updated :
22 Dec, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...