REGULAR EXPRESSIONS Workbook
REGULAR EXPRESSIONS Workbook
INTRODUCTION
Regular expressions (regex) are invaluable tools in programming for pattern matching and text
manipulation tasks. They provide a concise and flexible means of searching, extracting, and
modifying text based on specific patterns. With regular expressions, developers can efficiently
validate input, parse data, and perform complex string operations with minimal code.
The basic format of a regular expression is:
/PATTERN/ FLAG
REGEXP FLAGS
Flags show how our expression would be interpreted in our code. Some important flags to keep
in mind are:
Example 1:
The regular expression /h?at/g matches any string that contains the substring "at" preceded
optionally by the letter 'h'. Here's a breakdown of the pattern:
• h?: Matches 'h' zero or one time, making it optional.
• at: Matches the substring "at" literally.
So, this regular expression will match strings like "at", "hat", but not "bat" or "chat".
Example
The regular expression `(function) (al)?/g` matches any string that contains the word "function"
followed by either a space and the word "al" or just a space.
So, this regular expression will match strings like "function", "function al", "functional", but not
"function alal" or "functionalal", and it will consider multiple occurrences within the input text
(due to 'g' flag).
Example 2
The regular expression (aba?c(us)?)*/gm matches any string that contains zero or more
occurrences of the following pattern:
• aba: Matches the string "aba" literally.
• ?: Makes the preceding character 'a' optional, allowing it to appear zero or one time.
• c: Matches the character 'c' literally.
• (us)?: Matches the string "us" optionally, allowing it to appear zero or one time.
So, this regular expression will match strings like "abc", "abacus", "abac", "abacu", "abacacus",
"abacabc", "abacus", "abacuscus", etc
Example 3
The regular expression `.e\sneed(s)?\s[gb]lue/gm` matches any string that contains a word
ending with 'e' followed by a space, the word "need" optionally followed by 's', another space,
and then either 'blue' or 'glue'.
So, this regular expression will match strings like "I need blue", "She needs glue", "We need
blue", "He needs glue", etc., ignoring case (due to 'm' flag) and considering multiple lines (due
to 'g' flag).
Example 4
The regular expression \d{1,2}-(?:\d{1,2}|[a-z]{3})-\d{4} matches date patterns in the format of
"day-month-year". So, this regular expression will match date patterns like "01-01-2023", "25-
12-1998", "15-Jan-2005", etc., where the day and month can be represented either as digits or
as three-letter month abbreviations.
Example 5
The regular expression /[ab].ll/gm matches any string that contains either 'a' or 'b', followed by
any character, then followed by 'll'. So, the regular expression will match strings like 'ball', 'bell',
'tall', 'fell', 'doll', 'wall', etc., wherever 'a' or 'b' is followed by any character and then followed by
'll'.
Example 6
The regular expression /[ab].?ll/gm matches any string that contains either 'a' or 'b', followed
by an optional character (denoted by ?), then followed by 'll'.So, the regular expression will
match strings like 'all', 'bll', 'axll', 'bxll', but not 'axxll' or 'bx'.
QUESTIONS: underline the part of string which will be selected by the regular expression (use
the site regexr.com to verify)
1. REGEXP: /.at/g
STRING: Where are you going to eat at today?
Can i have what you have eaten.
2. REGEXP: [rd]ed
STRING: the match ended and the red team won.
the bed was not made today
Question2: specify regular expression to match the following strings
1. Matching phone numbers in the format xxx-xxx-xxxx.
2. Matching all nu.edu emails for students (eg [email protected]).
3. Match all vowels in a string regardless of them being in upper/lowercase.
4. Match all websites(for eg www.something.com)
5. Match the rhyming words in the following poem:
Humpty Dumpty sat on a wall, Humpty Dumpty had a great fall.