Regex in [[ ]] with =~ in Bash Scripting - Time & Space Complexity
When using regex inside [[ ]] with =~ in bash, it is important to understand how the time to check a match grows as the input changes.
We want to know how the script's running time changes when the input string gets longer.
Analyze the time complexity of the following code snippet.
input="$1"
pattern='^[a-z]+$'
if [[ $input =~ $pattern ]]; then
echo "Match found"
else
echo "No match"
fi
This code checks if the input string contains only lowercase letters using a regex match inside [[ ]] with =~.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The regex engine scans each character of the input string to check if it matches the pattern.
- How many times: It processes each character once, moving through the string from start to end.
As the input string gets longer, the regex engine checks more characters one by one.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 character checks |
| 100 | About 100 character checks |
| 1000 | About 1000 character checks |
Pattern observation: The time to check grows roughly in a straight line with the input length.
Time Complexity: O(n)
This means the time to check the regex match grows linearly with the length of the input string.
[X] Wrong: "Regex matching inside [[ ]] with =~ is instant no matter how long the input is."
[OK] Correct: The regex engine must look at each character to confirm a match, so longer inputs take more time.
Understanding how regex matching time grows helps you write efficient scripts and explain your code clearly in interviews.
"What if the regex pattern included nested quantifiers or backreferences? How would the time complexity change?"
Practice
[[ "hello123" =~ [a-z]+[0-9]+ ]] check in a bash script?Solution
Step 1: Understand the regex pattern
The pattern[a-z]+[0-9]+means one or more lowercase letters followed by one or more digits.Step 2: Apply the pattern to the string
The string "hello123" starts with letters "hello" and ends with digits "123", so it matches the pattern.Final Answer:
If the string contains letters followed by numbers -> Option BQuick Check:
Regex matches letters then digits = A [OK]
- Thinking quotes are needed around regex inside [[ ]]
- Confusing exact match with pattern match
- Assuming regex matches anywhere without order
var matches regex ^[0-9]+$ in bash?Solution
Step 1: Recall regex syntax in [[ ]]
Inside [[ ]], the regex pattern should NOT be quoted to work correctly.Step 2: Check each option
[[ $var =~ ^[0-9]+$ ]] uses[[ $var =~ ^[0-9]+$ ]]without quotes around regex, which is correct syntax.Final Answer:
[[ $var =~ ^[0-9]+$ ]] -> Option CQuick Check:
No quotes around regex pattern = B [OK]
- Putting quotes around regex pattern causing syntax errors
- Quoting variable but not regex or vice versa
- Using single brackets [ ] instead of [[ ]]
text="abc123" if [[ $text =~ ^[a-z]+[0-9]+$ ]]; then echo "Match" else echo "No match" fi
Solution
Step 1: Analyze the regex pattern
The pattern^[a-z]+[0-9]+$means the string must start with one or more lowercase letters and end with one or more digits, with nothing else.Step 2: Check the string against the pattern
The string "abc123" starts with letters "abc" and ends with digits "123", so it matches the pattern exactly.Final Answer:
Match -> Option AQuick Check:
String matches regex pattern = D [OK]
- Ignoring anchors ^ and $ in regex
- Assuming partial match is enough
- Quoting regex pattern causing failure
input="2023-04-01"
if [[ "$input" =~ "^[0-9]{4}-[0-9]{2}-[0-9]{2}$" ]]; then
echo "Valid date"
else
echo "Invalid date"
fiSolution
Step 1: Check quoting rules for regex in [[ ]]
Regex pattern inside [[ ]] with =~ must NOT be quoted; quotes make it a string literal, breaking regex matching.Step 2: Identify the error in the snippet
The snippet quotes the regex pattern, causing the condition to always fail and print "Invalid date".Final Answer:
Quotes around regex cause the match to fail -> Option AQuick Check:
No quotes around regex pattern = C [OK]
- Quoting regex pattern inside [[ ]]
- Forgetting to quote variable when needed
- Assuming regex syntax errors without checking quotes
filename ends with either ".jpg" or ".png" using regex in bash. Which condition correctly tests this?Solution
Step 1: Understand regex for file extensions
The pattern\.(jpg|png)$matches a dot followed by 'jpg' or 'png' at the end of the string.Step 2: Apply correct quoting rules
Regex pattern must NOT be quoted inside [[ ]], and variable can be unquoted if it has no spaces. [[ $filename =~ \.(jpg|png)$ ]] correctly uses unquoted regex and variable.Final Answer:
[[ $filename =~ \.(jpg|png)$ ]] -> Option DQuick Check:
Unquoted regex with alternation = A [OK]
- Quoting regex pattern causing failure
- Not escaping dot before extension
- Quoting variable unnecessarily
Ready to prove it?
Pass the test and this pattern moves unknown
