0
0
Bash Scriptingscripting~10 mins

Why regex enables pattern matching in Bash Scripting - Test Your Understanding

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to check if the variable 'text' contains the word 'hello' using regex.

Bash Scripting
if [[ $text =~ [1] ]]; then echo "Match found"; fi
Drag options to blanks, or click blank then click option'
A"^hello$"
B"hello?"
C"[0-9]+"
D"hello"
Attempts:
3 left
💡 Hint
Common Mistakes
Using anchors like ^ or $ which restrict the match to start or end of the string.
Using patterns that match numbers instead of the word 'hello'.
2fill in blank
medium

Complete the code to match any string that starts with 'file' followed by digits.

Bash Scripting
if [[ $filename =~ [1] ]]; then echo "Valid file name"; fi
Drag options to blanks, or click blank then click option'
A"^file[0-9]+$"
B"file.*"
C"file[0-9]+"
D"file[0-9]*"
Attempts:
3 left
💡 Hint
Common Mistakes
Using '*' which means zero or more digits, allowing 'file' alone.
Using anchors that restrict the match to the whole string.
3fill in blank
hard

Fix the error in the regex to match an email address in the variable 'email'.

Bash Scripting
if [[ $email =~ [1] ]]; then echo "Valid email"; fi
Drag options to blanks, or click blank then click option'
A"[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,}"
B"[a-z]+@[a-z]+\\.[a-z]+"
C"[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}"
D"^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,}$"
Attempts:
3 left
💡 Hint
Common Mistakes
Using a single backslash which is not enough inside double quotes.
Not escaping the dot, causing it to match any character.
4fill in blank
hard

Fill both blanks to create a regex that matches strings starting with 'log' and ending with '.txt'.

Bash Scripting
if [[ $filename =~ [1] && $filename =~ [2] ]]; then echo "Log file detected"; fi
Drag options to blanks, or click blank then click option'
A"^log"
B"log"
C"\.txt$"
D"txt$"
Attempts:
3 left
💡 Hint
Common Mistakes
Not using anchors '^' and '$' to specify start and end.
Not escaping the dot in '.txt'.
5fill in blank
hard

Fill all three blanks to create a regex that matches a date in 'YYYY-MM-DD' format.

Bash Scripting
if [[ $date =~ ^[1]-[2]-[3]$ ]]; then echo "Valid date format"; fi
Drag options to blanks, or click blank then click option'
A[0-9]{4}
B(0[1-9]|1[0-2])
C(0[1-9]|[12][0-9]|3[01])
D[0-9]{2}
Attempts:
3 left
💡 Hint
Common Mistakes
Using incorrect digit counts for year.
Not restricting month and day to valid ranges.