Skip to content
Related Articles
Open in App
Not now

Related Articles

Validate week days using Regular Expression

Improve Article
Save Article
  • Last Updated : 22 Dec, 2022
Improve Article
Save Article

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++

// C++ program to validate the
// Weekday using Regular
// Expression

#include <bits/stdc++.h>
#include <regex>
using namespace std;

// Function to validate the
// Weekday
string isValid_WeekDay(string str)
{
    // Regex to check valid weekday
    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 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 = "Sunday";
    cout << isValid_WeekDay(str1) << endl;

    // Test Case 2:
    string str2 = "Monday";
    cout << isValid_WeekDay(str2) << endl;

    // Test Case 3:
    string str3 = "Tues.";
    cout << isValid_WeekDay(str3) << endl;

    // Test Case 4:
    string str4 = "Payday";
    cout << isValid_WeekDay(str4) << endl;

    // Test Case 5:
    string str5 = "Friday";
    cout << isValid_WeekDay(str5) << endl;

    // Test Case 6:
    string str6 = "Birthday";
    cout << isValid_WeekDay(str6) << endl;

    return 0;
}

Java

// Java program to validate the
// Weekday using Regular Expression

import java.util.regex.*;

class GFG {
    // Function to validate the
    // Weekday
    public static boolean isValid_WeekDay(String str)
    {

        // Regex to check valid weekday
        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?))\\.?$";

        // 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 = "Sunday";
        System.out.println(isValid_WeekDay(str1));

        // Test Case 2:
        String str2 = "Monday";
        System.out.println(isValid_WeekDay(str2));

        // Test Case 3:
        String str3 = "Tues.";
        System.out.println(isValid_WeekDay(str3));

        // Test Case 4:
        String str4 = "Payday";
        System.out.println(isValid_WeekDay(str4));

        // Test Case 5:
        String str5 = "Friday";
        System.out.println(isValid_WeekDay(str5));

        // Test Case 6:
        String str6 = "Birthday";
        System.out.println(isValid_WeekDay(str6));
    }
}

Python3

# Python3 program to validate
# Weekday using Regular Expression

import re

# Function to validate
# Weekday


def isValid_WeekDay(str):

    # Regex to check valid Weekday
    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?))\.?$"

    # 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 = "Sunday"
    print(isValid_WeekDay(str1))

    # Test Case 2:
    str2 = "Monday"
    print(isValid_WeekDay(str2))

    # Test Case 3:
    str3 = "Tues."
    print(isValid_WeekDay(str3))

    # Test Case 4:
    str4 = "Payday"
    print(isValid_WeekDay(str4))

    # Test Case 5:
    str5 = "Friday"
    print(isValid_WeekDay(str5))

    # Test Case 6:
    str6 = "Birthday"
    print(isValid_WeekDay(str6))

C#

// Include namespace system
using System;
using System.Text.RegularExpressions;

public class GFG
{

  // Function to validate the
  // Weekday
  public static bool isValid_WeekDay(String str)
  {

    // Regex to check valid LEI Code
    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 the LEI Code
    // is empty return false
    if (str == null)
    {
      return false;
    }

    // Pattern class contains matcher()
    // method to find matching between
    // given weekday using regex.
    var m = regex.Match(str);

    // Return if the weekday
    // matched the ReGex
    return m.Success;
  }

  // Driver Code.
  public static void Main(String[] args)
  {

    // Test Case 1:
    var str1 = "Sunday";
    Console.WriteLine(GFG.isValid_WeekDay(str1));

    // Test Case 2:
    var str2 = "Monday";
    Console.WriteLine(GFG.isValid_WeekDay(str2));

    // Test Case 3:
    var str3 = "Tues.";
    Console.WriteLine(GFG.isValid_WeekDay(str3));

    // Test Case 4:
    var str4 = "Payday";
    Console.WriteLine(GFG.isValid_WeekDay(str4));

    // Test Case 5:
    var str5 = "Friday";
    Console.WriteLine(GFG.isValid_WeekDay(str5));

    // Test Case 6:
    var str6 = "Birthday";
    Console.WriteLine(GFG.isValid_WeekDay(str6));
  }
}

// This code is contributed by Potta Lokesh.

Javascript

// Javascript program to validate
// Weekday using Regular Expression

// Function to validate the
// Weekday
function isValid_WeekDay(str) {
    // Regex to check valid
    // weekday
    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
    // 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 = "Sunday";
console.log(isValid_WeekDay(str1));

// Test Case 2:
let str2 = "Monday";
console.log(isValid_WeekDay(str2));

// Test Case 3:
let str3 = "Tues.";
console.log(isValid_WeekDay(str3));

// Test Case 4:
let str4 = "Payday";
console.log(isValid_WeekDay(str4));

// Test Case 5:
let str5 = "Friday";
console.log(isValid_WeekDay(str5));

// Test Case 6:
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:


My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!