Regular expression matching (~ operator) in PostgreSQL - Time & Space Complexity
We want to understand how the time it takes to check if text matches a pattern grows as the text gets longer.
How does the matching process scale when the input text size increases?
Analyze the time complexity of the following code snippet.
SELECT *
FROM documents
WHERE content ~ '^[A-Z0-9]*$';
This query finds all rows where the content column matches a pattern: only uppercase letters and digits.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The regular expression engine checks each character in the content string to see if it fits the pattern.
- How many times: For each row, it scans through the content string characters once, from start to end or until a mismatch is found.
As the length of the content string grows, the time to check the pattern grows roughly in proportion to the string length.
| 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 as the string gets longer.
Time Complexity: O(n)
This means the time to match grows in a straight line with the length of the text being checked.
[X] Wrong: "Regular expression matching always takes the same time no matter how long the text is."
[OK] Correct: The engine usually checks each character, so longer text means more work and more time.
Understanding how pattern matching time grows helps you write efficient queries and explain performance in real projects.
"What if the regular expression included a wildcard that can match many characters? How would that affect the time complexity?"
Practice
~ do when used in a query like column_name ~ 'pattern'?Solution
Step 1: Understand the
The~operator~operator in PostgreSQL is used to check if a string matches a regular expression pattern.Step 2: Apply to the query context
When used ascolumn_name ~ 'pattern', it returns true if the column's value matches the pattern, otherwise false.Final Answer:
Checks if the column value matches the regular expression pattern -> Option CQuick Check:
~means match pattern [OK]
- Thinking ~ replaces or modifies data
- Confusing ~ with counting occurrences
- Assuming ~ returns string length
email contains the domain example.com using the ~ operator?Solution
Step 1: Understand the pattern for matching domain
To match emails ending with '@example.com', the regex should be@example\.com$where\.escapes the literal dot and$means end of string.Step 2: Check the syntax of the
The correct syntax is~operator usageemail ~ '@example\.com$'to find emails ending with that domain.Final Answer:
SELECT * FROM users WHERE email ~ '@example\.com$'; -> Option BQuick Check:
Regex match with~and end anchor [OK]
- Using LIKE instead of ~ for regex matching
- Missing anchors like $ for exact domain match
- Using = operator with regex pattern
products with a column code containing values: 'A123', 'B234', 'A345', 'C456', what will the query SELECT code FROM products WHERE code ~ '^A'; return?Solution
Step 1: Understand the regex pattern
The caret^A^means the string must start with 'A'.Step 2: Apply pattern to each code
Codes starting with 'A' are 'A123' and 'A345'.Final Answer:
A123, A345 -> Option AQuick Check:
^ means start with 'A' [OK]
- Confusing ^ with end anchor $
- Expecting all rows to match
- Ignoring regex anchors
SELECT * FROM users WHERE username ~ '*'; but it returns an error. What is the most likely cause?Solution
Step 1: Check regex pattern syntax
The pattern*is invalid because the*quantifier requires a preceding atom (e.g., a character or group).Step 2: Identify common error with special characters
Regex special symbols like*,+,?need escape characters (e.g.,\*in SQL string) when used literally.Final Answer:
The regex pattern is missing escape characters for special symbols -> Option AQuick Check:
Escape special regex chars in SQL strings [OK]
- Using single quotes incorrectly for regex
- Assuming ~ needs double quotes
- Ignoring NULL values causing errors
orders where the order_id starts with a letter followed by exactly three digits. Which query using the ~ operator is correct?Solution
Step 1: Understand the pattern requirements
The order_id must start with a letter[A-Za-z]followed by exactly three digits[0-9]{3}, and nothing else.Step 2: Check regex anchors and pattern
Using^and$anchors ensures the entire string matches.'^[A-Za-z][0-9]{3}$'matches a letter then three digits exactly.Final Answer:
SELECT * FROM orders WHERE order_id ~ '^[A-Za-z][0-9]{3}$'; -> Option DQuick Check:
^ and $ anchor full string match [OK]
- Swapping letter and digit order
- Missing anchors causing partial matches
- Using \w which includes digits and letters
Ready to prove it?
Pass the test and this pattern moves unknown
