Capture groups in Bash in Bash Scripting - Time & Space Complexity
We want to understand how the time taken by a Bash script using capture groups changes as the input size grows.
Specifically, how does the script's work increase when processing longer text or more matches?
Analyze the time complexity of the following Bash snippet using capture groups.
text="apple banana cherry date"
for word in $text; do
if [[ $word =~ (a)(.*) ]]; then
echo "First letter: ${BASH_REMATCH[1]}, Rest: ${BASH_REMATCH[2]}"
fi
done
This code loops over words in a string and uses capture groups to split each word starting with 'a' into parts.
Look for repeated actions in the code.
- Primary operation: Looping over each word in the text.
- How many times: Once for each word in the input string.
As the number of words grows, the script checks each word once.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 checks and possible captures |
| 100 | 100 checks and possible captures |
| 1000 | 1000 checks and possible captures |
Pattern observation: The work grows directly with the number of words.
Time Complexity: O(n)
This means the time grows linearly with the number of words processed.
[X] Wrong: "Capture groups make the script slower exponentially because they do extra work inside the loop."
[OK] Correct: Each capture group operation happens once per word, so it adds a small constant cost per iteration, not exponential growth.
Understanding how loops and pattern matching scale helps you explain script efficiency clearly and confidently.
"What if the script used nested loops to process each character inside each word? How would the time complexity change?"
Practice
BASH_REMATCH array hold after a successful regex match in Bash?Solution
Step 1: Understand BASH_REMATCH usage
When using[[ string =~ regex ]], Bash stores the full matched text inBASH_REMATCH[0].Step 2: Identify captured groups storage
Captured groups from parentheses in the regex are stored starting atBASH_REMATCH[1]onwards.Final Answer:
The full matched string at index 0 and captured groups from index 1 onwards -> Option AQuick Check:
BASH_REMATCH = full match + groups [OK]
- Thinking BASH_REMATCH holds only groups
- Confusing BASH_REMATCH with exit status
- Expecting regex pattern in BASH_REMATCH
Solution
Step 1: Recall Bash regex match syntax
Bash uses the conditional expression[[ string =~ regex ]]to test regex matches.Step 2: Identify correct syntax among options
Only [[ $string =~ $regex ]] uses the correct Bash syntax with double brackets and the =~ operator.Final Answer:
[[ $string =~ $regex ]] -> Option CQuick Check:
Use [[ ]] with =~ for regex match [OK]
- Using single brackets [ ] which don't support =~
- Using parentheses () like in other languages
- Using commands like test or match incorrectly
string="user:1234"
if [[ $string =~ ([a-z]+):([0-9]+) ]]; then
echo "Name=${BASH_REMATCH[1]}, ID=${BASH_REMATCH[2]}"
fi
Solution
Step 1: Analyze regex and string
The regex([a-z]+):([0-9]+)matches letters before colon and digits after colon in the string "user:1234".Step 2: Check captured groups in BASH_REMATCH
BASH_REMATCH[1]captures "user" andBASH_REMATCH[2]captures "1234".Final Answer:
Name=user, ID=1234 -> Option AQuick Check:
Captured groups print correct values [OK]
- Expecting full string in BASH_REMATCH[1]
- Forgetting to use double brackets [[ ]]
- Misreading regex pattern
string="abc123"
if [[$string =~ ([a-z]+)([0-9]+)]]; then
echo "${BASH_REMATCH[1]} and ${BASH_REMATCH[2]}"
fi
Solution
Step 1: Check syntax of conditional expression
The conditional expression[[ ... ]]requires spaces after [[ and before ]]. The snippet misses spaces.Step 2: Confirm regex and BASH_REMATCH usage
The regex is valid and BASH_REMATCH is set correctly when using =~ inside [[ ]].Final Answer:
Missing spaces inside [[ and ]] -> Option BQuick Check:
Spaces needed inside [[ and ]] [OK]
- Omitting spaces inside [[ ]]
- Confusing single [ ] with [[ ]]
- Assuming regex pattern is wrong
url="https://example.com/path/to/page"
if [[ $url =~ https://([^/]+)(/.*) ]]; then
echo "Domain: ${BASH_REMATCH[1]}"
echo "Path: ${BASH_REMATCH[2]}"
fi
Solution
Step 1: Analyze regex pattern
The regexhttps://([^/]+)(/.*)matches 'https://' literally, then captures domain (any chars except '/') and path (slash followed by anything).Step 2: Check BASH_REMATCH usage
BASH_REMATCH[1]holds domain,BASH_REMATCH[2]holds path, correctly extracted from the URL.Final Answer:
Correctly captures domain as example.com and path as /path/to/page -> Option DQuick Check:
Regex groups extract domain and path correctly [OK]
- Not escaping dots in regex (not needed inside character class)
- Assuming BASH_REMATCH can't hold multiple groups
- Missing literal https:// in regex
Ready to prove it?
Pass the test and this pattern moves unknown
