Challenge - 5 Problems
Regex Grep Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate2:00remaining
Output of grep -E with alternation
Given a file
What is the output of this command?
colors.txt with the following content:red blue green yellow purple orange
What is the output of this command?
grep -E 'red|blue|pink' colors.txt
Attempts:
2 left
💡 Hint
The -E option allows using extended regex. The pattern matches lines containing 'red' or 'blue' or 'pink'.
✗ Incorrect
The command searches for lines containing either 'red', 'blue', or 'pink'. Since 'pink' is not in the file, only 'red' and 'blue' lines are printed.
💻 Command Output
intermediate2:00remaining
Using grep -E with character classes and quantifiers
Consider a file
What is the output of:
words.txt containing:cat cot cut cit cet citizen cute
What is the output of:
grep -E '^c[aeiou]t$' words.txt
Attempts:
2 left
💡 Hint
The pattern matches lines starting with 'c', followed by a vowel, then 't', and nothing else.
✗ Incorrect
The regex ^c[aeiou]t$ matches exactly three-letter words starting with 'c', followed by a vowel (a,e,i,o,u), and ending with 't'. Matches: 'cat' (a), 'cot' (o), 'cut' (u), 'cit' (i), 'cet' (e). 'citizen' and 'cute' have additional characters.
💻 Command Output
advanced2:00remaining
Output of grep -E with grouping and quantifiers
Given a file
What is the output of:
numbers.txt with:123 1123 11123 111123 1111123
What is the output of:
grep -E '^1{3}23$' numbers.txtAttempts:
2 left
💡 Hint
The pattern matches lines starting with exactly three '1's followed by '23' and nothing else.
✗ Incorrect
The regex ^1{3}23$ matches lines that start with exactly three '1's then '23'. Only '11123' matches exactly. '111123' has four '1's, so it does not match.
💻 Command Output
advanced2:00remaining
grep -E with optional groups and anchors
File
What is the output of:
animals.txt contains:cat cats dog dogs caterpillar catalog
What is the output of:
grep -E '^cat(s)?$' animals.txt
Attempts:
2 left
💡 Hint
The pattern matches lines starting with 'cat' and optionally ending with 's', and nothing else.
✗ Incorrect
The regex ^cat(s)?$ matches 'cat' or 'cats' exactly. 'caterpillar' and 'catalog' have extra characters, so they do not match.
💻 Command Output
expert3:00remaining
Complex grep -E regex with nested groups and alternation
File
What is the output of:
log.txt contains:error123 warning456 info789 error456 warning123 info123
What is the output of:
grep -E '^(error|warning)(123|456)$' log.txt
Attempts:
2 left
💡 Hint
The pattern matches lines starting with 'error' or 'warning' followed by '123' or '456' exactly.
✗ Incorrect
The regex ^(error|warning)(123|456)$ matches lines that start with either 'error' or 'warning' and end with either '123' or '456'. So 'error123', 'error456', 'warning123', and 'warning456' match. 'info789' and 'info123' do not match.