Published on

Regex for Weekend

Regex for Weekend

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 Weekends are typically named Saturday and Sunday. In this article let's understand how we can create a regex for weekend and how regex can be matched for a given weekend.

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 Weekend

The Weekend should have the following criteria and structure-

  • It should be a string
  • There are 2 days that are categorised as weekends - Saturday, Sunday
  • It can also be abbreviated as Sat, Sun

Regex for checking if Weekend is valid or not

Regular Expression for Weekend is-


Test string examples for the above regex-

Input StringMatch Output
Mondaydoes not match
holidaydoes not match

Here is a detailed explanation of the above regex-


^ asserts position at start of a line
1st Capturing Group (sat|satur|sun)
1st Alternative sat
sat matches the characters sat literally (case insensitive)
2nd Alternative satur
satur matches the characters satur literally (case insensitive)
3rd Alternative sun
sun matches the characters sun literally (case insensitive)
2nd Capturing Group (day)?
? matches the previous token between zero and one times, as many times as possible, giving back as needed (greedy)
day matches the characters day literally (case insensitive)
$ asserts position at the end of a line
Global pattern flags
i modifier: insensitive. Case insensitive match (ignores case of [a-zA-Z])
g modifier: global. All matches (don't return after first match)
m modifier: multi line. Causes ^ and $ to match the begin/end of each line (not only begin/end of string)

Hope this article was useful to check if the string is a valid weekend or not.