A day of the week is a period of time within a seven-day cycle, used to divide a calendar week into seven equal parts. The days of the week are typically named Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, and Saturday. In this article let’s understand how we can create a regex for day of a week and how regex can be matched for a given day of a week.
Regex (short for regular expression) is a powerful tool used for searching and manipulating text. It is composed of a sequence of characters that define a search pattern. Regex can be used to find patterns in large amounts of text, validate user input, and manipulate strings. It is widely used in programming languages, text editors, and command line tools.
Structure of a days of week
The day of week should have the following criteria and structure-
- It should be a string
- There are 7 days in a week – Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday
- It can also be abbreviated as Mon, Tue, Wed, Thu, Fri, Sat, Sun
- It is also abbreviated as Mon, Tues, Wed, Thurs, Fri, Sat, Sun
Regex for checking if Day of week is valid or not
Regular Expression for Day of week is-
/^(Sun|Mon|(T(ues|hurs))|Fri)(day|\.)?$|Wed(\.|nesday)?$|Sat(\.|urday)?$|T((ue?)|(hu?r?))\.?$/igm
Test string examples for the above regex-
Input String | Match Output |
---|---|
sund | does not match |
Sunday | matches |
holiday | does not match |
Sat | matches |
thursday | matches |
Tue. | matches |
Tues | matches |
Here is a detailed explanation of the above regex-
/^(Sun|Mon|(T(ues|hurs))|Fri)(day|\.)?$|Wed(\.|nesday)?$|Sat(\.|urday)?$|T((ue?)|(hu?r?))\.?$/igm
1st Alternative (Sun|Mon|(T(ues|hurs))|Fri)(day|\.)?$
1st Capturing Group (Sun|Mon|(T(ues|hurs))|Fri)
1st Alternative Sun
Sun matches the characters Sun literally (case insensitive)
2nd Alternative Mon
Mon matches the characters Mon literally (case insensitive)
3rd Alternative (T(ues|hurs))
2nd Capturing Group (T(ues|hurs))
T matches the character T with index 8410 (5416 or 1248) literally (case insensitive)
3rd Capturing Group (ues|hurs)
1st Alternative ues
ues matches the characters ues literally (case insensitive)
2nd Alternative hurs
hurs matches the characters hurs literally (case insensitive)
4th Alternative Fri
Fri matches the characters Fri literally (case insensitive)
4th Capturing Group (day|\.)?
? matches the previous token between zero and one times, as many times as possible, giving back as needed (greedy)
1st Alternative day
day matches the characters day literally (case insensitive)
2nd Alternative \.
\. matches the character . with index 4610 (2E16 or 568) literally (case insensitive)
$ asserts position at the end of a line
2nd Alternative Wed(\.|nesday)?$
Wed matches the characters Wed literally (case insensitive)
5th Capturing Group (\.|nesday)?
? matches the previous token between zero and one times, as many times as possible, giving back as needed (greedy)
1st Alternative \.
\. matches the character . with index 4610 (2E16 or 568) literally (case insensitive)
2nd Alternative nesday
nesday matches the characters nesday literally (case insensitive)
$ asserts position at the end of a line
3rd Alternative Sat(\.|urday)?$
Sat matches the characters Sat literally (case insensitive)
6th Capturing Group (\.|urday)?
? matches the previous token between zero and one times, as many times as possible, giving back as needed (greedy)
1st Alternative \.
\. matches the character . with index 4610 (2E16 or 568) literally (case insensitive)
2nd Alternative urday
urday matches the characters urday literally (case insensitive)
$ asserts position at the end of a line
4th Alternative T((ue?)|(hu?r?))\.?
T matches the character T with index 8410 (5416 or 1248) literally (case insensitive)
7th Capturing Group ((ue?)|(hu?r?))
1st Alternative (ue?)
8th Capturing Group (ue?)
2nd Alternative (hu?r?)
\. matches the character . with index 4610 (2E16 or 568) literally (case insensitive)
? matches the previous token between zero and one times, as many times as possible, giving back as needed (greedy)
Global pattern flags
m modifier: multi line. Causes ^ and $ to match the begin/end of each line (not only begin/end of string)
g modifier: global. All matches (don't return after first match)
i modifier: insensitive. Case insensitive match (ignores case of [a-zA-Z])
Hope this article was useful to check if the string is a valid days of the week or not. In conclusion, understanding the structure of days of the week and creating regular expressions (regex) to validate them is crucial for various programming and text manipulation tasks. Regex, a potent tool for pattern matching, aids in efficient text processing. This article delved into the criteria for valid days of the week, presented a comprehensive regex pattern, and provided test examples. By grasping these concepts, readers can confidently work with days of the week data and regex patterns in their programming endeavors.