Quantifiers (*, +, ?) in Bash Scripting - Time & Space Complexity
When using quantifiers like *, +, and ? in bash scripting, it's important to understand how they affect the number of operations performed.
We want to see how the script's work grows as input size changes when these quantifiers are used.
Analyze the time complexity of the following bash script using quantifiers.
#!/bin/bash
input="$1"
if [[ $input =~ ^[a-z]*$ ]]; then
echo "Only lowercase letters or empty"
fi
if [[ $input =~ ^[a-z]+$ ]]; then
echo "At least one lowercase letter"
fi
if [[ $input =~ ^[a-z]?$ ]]; then
echo "Zero or one lowercase letter"
fi
This script checks if the input matches patterns with *, +, and ? quantifiers.
Look at what repeats when the script runs.
- Primary operation: The regex engine checks each character in the input string against the pattern.
- How many times: It processes each character once to match the pattern.
The time to check the input grows as the input gets longer because each character is checked.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 character checks |
| 100 | About 100 character checks |
| 1000 | About 1000 character checks |
Pattern observation: The work grows linearly with input size.
Time Complexity: O(n)
This means the time to run the regex grows directly with the length of the input string.
[X] Wrong: "Quantifiers like * or + make the script run instantly no matter the input size."
[OK] Correct: Even with quantifiers, the regex engine must check each character, so longer inputs take more time.
Understanding how quantifiers affect performance helps you write efficient scripts and explain your reasoning clearly in interviews.
"What if the regex pattern used nested quantifiers or backreferences? How would that affect time complexity?"
Practice
* mean when used in a regular expression?Solution
Step 1: Understand the meaning of
The*in regex*quantifier matches zero or more occurrences of the character or group before it.Step 2: Compare with other quantifiers
Unlike+which requires at least one match,*allows zero matches too.Final Answer:
Match zero or more occurrences of the preceding element -> Option AQuick Check:
*means zero or more matches [OK]
- Confusing * with + which requires at least one match
- Thinking * matches exactly one occurrence
- Mixing * with ? which means zero or one
+ quantifier in grep to match one or more digits?Solution
Step 1: Recognize that
The+needs extended regex+quantifier is not supported by default in basic grep; it requires-Eor-Poption.Step 2: Identify correct grep syntax
Usinggrep -E '[0-9]+'enables extended regex and matches one or more digits.Final Answer:
grep -E '[0-9]+' file.txt -> Option DQuick Check:
Use -E or -P for + quantifier [OK]
- Using + without -E or -P causes literal + match
- Confusing * with + quantifier
- Using -P without knowing if Perl regex is supported
file.txt contains the lines:apple apples ap ape
Command:
grep -E 'ap?le' file.txt
Solution
Step 1: Understand the regex
Theap?lep?means zero or one 'p' character. So it matches 'ale' or 'aple'.Step 2: Check which lines match
Lines: 'apple' (has two p's, so no), 'apples' (extra chars, no), 'ap' (too short), 'ape' (does not match 'ap?le' because 'l' is missing). Actually 'apple' has two 'p's, so 'ap?le' matches 'ale' or 'aple' but 'apple' has 'pp', so no match. 'ape' does not match 'ap?le' because 'l' is missing. So only 'apple' matches partially but not fully.Step 3: Re-examine lines carefully
Actually, 'apple' is 'a p p l e'. The regex 'ap?le' means 'a' followed by zero or one 'p', then 'l', then 'e'. So 'apple' has two 'p's, so it does not match. 'ape' does not match 'ap?le' because 'l' is missing. So no output.Final Answer:
apple -> Option AQuick Check:
Regex matches zero or one p, so 'apple' no match [OK]
- Assuming 'apple' matches when it has two p's
- Ignoring that ? allows zero or one occurrence only
- Confusing 'ap?le' with 'ap*le'
grep '[0-9]+' file.txt
But it returns no matches even though the file has digits. What is the likely problem?
Solution
Step 1: Understand grep default regex
By default, grep uses basic regex where + is treated as a literal plus sign, not a quantifier.Step 2: Identify correct usage for + quantifier
To use + as 'one or more', grep needs-E(extended regex) or-P(Perl regex) option.Final Answer:
The + quantifier needs -E or -P option to work -> Option BQuick Check:
Use -E or -P for + quantifier [OK]
- Using + without enabling extended regex
- Thinking * and + are interchangeable always
- Assuming [0-9] does not match digits
Solution
Step 1: Understand the pattern needed
We want to match 'cat' or 'cats'. The 's' is optional, so uses?quantifier.Step 2: Use word boundaries and extended regex
Use\bfor word boundaries and-Eto enable?quantifier.Step 3: Check options
grep -E '\bcats?\b' file.txt usesgrep -E '\bcats?\b'which matches 'cat' or 'cats' correctly. Others either lack -E or misplace the quantifier.Final Answer:
grep -E '\bcats?\b' file.txt -> Option CQuick Check:
Use -E and s? to match zero or one s [OK]
- Forgetting -E option for ? quantifier
- Placing ? after wrong character
- Not using word boundaries causing partial matches
Ready to prove it?
Pass the test and this pattern moves unknown
