Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to capture the first word from the string using Bash regex.
Bash Scripting
text="Hello World" if [[ $text =~ ^([a-zA-Z]+) ]]; then echo "First word: ${BASH_REMATCH[[1]]}" fi
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using BASH_REMATCH[0] instead of BASH_REMATCH[1] to get the captured group.
✗ Incorrect
In Bash, BASH_REMATCH[1] holds the first captured group from the regex match.
2fill in blank
mediumComplete the code to check if the variable 'email' contains a domain using capture groups.
Bash Scripting
email="user@example.com" if [[ $email =~ @([a-zA-Z0-9.-]+)\.com$ ]]; then domain=${BASH_REMATCH[[1]]} echo "Domain: $domain" fi
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using BASH_REMATCH[0] which returns the whole email instead of the domain.
✗ Incorrect
The domain part is captured in the first group, so BASH_REMATCH[1] holds it.
3fill in blank
hardFix the error in the code to correctly capture the year from the date string.
Bash Scripting
date="2024-06-15" if [[ $date =~ ([0-9]{4})-([0-9]{2})-([0-9]{2}) ]]; then year=${BASH_REMATCH[[1]]} echo "Year: $year" fi
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using BASH_REMATCH[0] which returns the full date string.
✗ Incorrect
The year is captured in the first group, so BASH_REMATCH[1] holds it.
4fill in blank
hardFill both blanks to capture the username and domain from an email string.
Bash Scripting
email="alice@example.com" if [[ $email =~ ^([a-zA-Z0-9._%+-]+)@([a-zA-Z0-9.-]+)\.com$ ]]; then user=${BASH_REMATCH[[1]]} domain=${BASH_REMATCH[[2]]} echo "User: $user, Domain: $domain" fi
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using index 0 which returns the full email instead of parts.
✗ Incorrect
The first capture group is the username (index 1), the second is the domain (index 2).
5fill in blank
hardFill all three blanks to capture year, month, and day from a date string.
Bash Scripting
date="2024-06-15" if [[ $date =~ ^([0-9]{4})-([0-9]{2})-([0-9]{2})$ ]]; then year=${BASH_REMATCH[[1]]} month=${BASH_REMATCH[[2]]} day=${BASH_REMATCH[[3]]} echo "Year: $year, Month: $month, Day: $day" fi
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using index 0 for any of the date parts.
✗ Incorrect
The capture groups for year, month, and day are at indices 1, 2, and 3 respectively.