Challenge - 5 Problems
Regex Quantifier Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate2:00remaining
What is the output of this grep command using the * quantifier?
Consider a file named
What will be the output of the command
data.txt with the following content:apple
appple
aple
appppple
What will be the output of the command
grep -E 'ap*le' data.txt?Bash Scripting
grep -E 'ap*le' data.txtAttempts:
2 left
💡 Hint
Remember, * means zero or more of the previous character.
✗ Incorrect
The pattern 'ap*le' matches 'a' followed by zero or more 'p's, then 'le'. All lines match: 'apple' (two 'p's), 'appple' (three 'p's), 'aple' (one 'p'), 'appppple' (four 'p's).
💻 Command Output
intermediate2:00remaining
What does the + quantifier do in this grep command?
Given a file
What will be the output of
words.txt with content:cat caat ct caaat
What will be the output of
grep -E 'ca+t' words.txt?Bash Scripting
grep -E 'ca+t' words.txtAttempts:
2 left
💡 Hint
The + quantifier means one or more of the previous character.
✗ Incorrect
The pattern 'ca+t' matches 'c' followed by one or more 'a's, then 't'. So 'cat', 'caat', and 'caaat' match. 'ct' does not match because it has zero 'a's.
💻 Command Output
advanced2:00remaining
What is the output of this grep command using the ? quantifier?
File
What will
input.txt contains:color colour colr colouur
What will
grep -E 'colou?r' input.txt output?Bash Scripting
grep -E 'colou?r' input.txt
Attempts:
2 left
💡 Hint
The ? quantifier means zero or one of the previous character.
✗ Incorrect
The pattern 'colou?r' matches 'colo' followed by zero or one 'u', then 'r'. So 'color' (zero 'u') and 'colour' (one 'u') match. 'colr' (missing 'o') and 'colouur' (two 'u's) do not match.
🔧 Debug
advanced2:00remaining
Why does this grep command fail to match expected lines?
You run
But it matches 'abc', 'abbc', and 'abcc'. Why doesn't it match 'ac'?
grep -E 'ab+c*' file.txt on a file with lines:abc abbc ac abcc
But it matches 'abc', 'abbc', and 'abcc'. Why doesn't it match 'ac'?
Bash Scripting
grep -E 'ab+c*' file.txtAttempts:
2 left
💡 Hint
Check what each quantifier means and what the pattern requires.
✗ Incorrect
The pattern 'ab+c*' means 'a' followed by one or more 'b's, then zero or more 'c's. 'ac' has zero 'b's, so it does not match. 'abcc' matches because it has 'a', 'b', and 'cc'.
🧠 Conceptual
expert3:00remaining
How many lines will match this grep pattern with mixed quantifiers?
File
How many lines will match
test.txt contains:aaab ab aab b aaaab
How many lines will match
grep -E 'a+b?a*' test.txt?Bash Scripting
grep -E 'a+b?a*' test.txtAttempts:
2 left
💡 Hint
Break down the pattern: a+ then b? then a*.
✗ Incorrect
The pattern 'a+b?a*' means one or more 'a's, optionally one 'b', then zero or more 'a's. Matching lines: 'aaab', 'ab', 'aab', 'aaaab'. 'b' does not match (no initial a+). So 4 lines match (option B).