0
0
Bash Scriptingscripting~20 mins

Capture groups in Bash in Bash Scripting - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Bash Capture Groups Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate
2:00remaining
What is the output of this Bash script using capture groups?
Consider the following Bash script that uses a regex with capture groups. What will it print?
Bash Scripting
text="User: alice, ID: 42"
if [[ $text =~ User: ([a-z]+), ID: ([0-9]+) ]]; then
  echo "Name=${BASH_REMATCH[1]}, Number=${BASH_REMATCH[2]}"
else
  echo "No match"
fi
AName=User, Number=alice
BName=alice, Number=42
CNo match
DName=, Number=
Attempts:
2 left
💡 Hint
Remember that BASH_REMATCH stores the full match at index 0, and capture groups start at index 1.
📝 Syntax
intermediate
1:30remaining
Which Bash regex syntax correctly captures two groups?
Which option correctly uses capture groups in a Bash regex inside [[ ]]?
A[[ $var =~ /(foo)(bar)/ ]]
B[[ $var =~ \(foo\)\(bar\) ]]
C[[ $var =~ (foo)(bar) ]]; then
D[[ $var =~ (foo)(bar) ]]
Attempts:
2 left
💡 Hint
Bash regex inside [[ ]] does not use slashes or escaped parentheses.
🔧 Debug
advanced
2:30remaining
Why does this Bash script fail to capture groups?
Given this script, why does it print 'No match' even though the text looks correct? text="abc123" if [[ $text =~ abc([0-9]+) ]]; then echo "Number: ${BASH_REMATCH[1]}" else echo "No match" fi
AThe variable text is not quoted in the regex
BThe regex is missing anchors ^ and $
CThe script is run with sh, not bash
DBASH_REMATCH is not set outside functions
Attempts:
2 left
💡 Hint
Check which shell runs the script; sh does not support BASH_REMATCH.
🚀 Application
advanced
3:00remaining
Extract domain and path from URL using Bash capture groups
You want to extract the domain and path from this URL using Bash regex capture groups: url="https://example.com/path/to/page" Which option correctly extracts domain and path into variables domain and path?
Aif [[ $url =~ https://([^/]+)/(.*) ]]; then domain=${BASH_REMATCH[1]}; path=${BASH_REMATCH[2]}; fi
Bif [[ $url =~ https://(.*)/(.*) ]]; then domain=${BASH_REMATCH[1]}; path=${BASH_REMATCH[2]}; fi
Cif [[ $url =~ https://([^/]+)(/.*) ]]; then domain=${BASH_REMATCH[1]}; path=${BASH_REMATCH[2]}; fi
Dif [[ $url =~ https://([^/]+)(.*) ]]; then domain=${BASH_REMATCH[1]}; path=${BASH_REMATCH[2]}; fi
Attempts:
2 left
💡 Hint
The domain ends before the first slash after https://, and path starts after the slash.
🧠 Conceptual
expert
3:00remaining
How many capture groups are in this Bash regex?
In the Bash regex used inside [[ ]]: regex='(a(b)c)(d(e)f)' How many capture groups does this regex have, and what does BASH_REMATCH[3] contain if matched against 'abcdef'?
A4 groups; BASH_REMATCH[3] = 'def'
B3 groups; BASH_REMATCH[3] = 'e'
C5 groups; BASH_REMATCH[3] = 'd'
D5 groups; BASH_REMATCH[3] = 'b'
Attempts:
2 left
💡 Hint
Count all parentheses pairs, including nested ones.