Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to check if the variable 'text' contains the word 'hello' using regex.
Bash Scripting
if [[ $text [1] "hello" ]]; then echo "Match found" fi
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using '==' instead of '=~' for regex matching.
Using single brackets [ ] which do not support '=~'.
✗ Incorrect
The operator '=~' is used inside [[ ]] to test if a string matches a regex pattern.
2fill in blank
mediumComplete the code to check if the variable 'input' starts with a digit using regex.
Bash Scripting
if [[ $input [1] ^[0-9] ]]; then echo "Starts with a digit" fi
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using '==' instead of '=~' for regex.
Not escaping special characters when needed.
✗ Incorrect
The '=~' operator is used to match regex patterns inside [[ ]]. '^' means start of string.
3fill in blank
hardFix the error in the code to correctly check if 'var' contains only letters.
Bash Scripting
if [[ $var [1] ^[a-zA-Z]+$ ]]; then echo "Only letters" fi
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using '==' instead of '=~' causes the regex to be treated as a string.
Forgetting to quote the variable can cause errors if it is empty.
✗ Incorrect
Use '=~' to match regex. '^[a-zA-Z]+$' matches strings with only letters.
4fill in blank
hardFill both blanks to check if 'filename' ends with '.txt' using regex.
Bash Scripting
if [[ $filename [1] \.[2]$ ]]; then echo "Text file" fi
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using '==' instead of '=~' for regex.
Not escaping the dot before 'txt'.
✗ Incorrect
Use '=~' for regex matching. '\.txt$' matches strings ending with '.txt'.
5fill in blank
hardFill all three blanks to create a regex that matches a string with exactly 3 digits.
Bash Scripting
if [[ $code [1] ^[2][3]$ ]]; then echo "Three digits" fi
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using '==' instead of '=~'.
Forgetting to use curly braces for exact count.
✗ Incorrect
Use '=~' for regex matching. '^[0-9]{3}$' matches exactly three digits.