Regular expression functions help find or change text patterns inside data. They make searching and editing text easier and faster.
Regular expression functions (regexp_match, regexp_replace) in PostgreSQL
regexp_match(string, pattern [, flags]) regexp_replace(string, pattern, replacement [, flags])
regexp_match returns the first part of the string that matches the pattern as an array.
regexp_replace replaces parts of the string that match the pattern with the replacement text.
SELECT regexp_match('hello123', '\\d+');
SELECT regexp_replace('hello world', ' ', '_');
SELECT regexp_match('abc123def', '([a-z]+)(\\d+)([a-z]+)');
SELECT regexp_replace('abc123def', '\\d+', '456');
This example finds a phone number pattern in the text and then replaces it with a hidden version.
SELECT regexp_match('My phone is 555-1234', '\\d{3}-\\d{4}') AS phone_match; SELECT regexp_replace('My phone is 555-1234', '\\d{3}-\\d{4}', 'XXX-XXXX') AS phone_hidden;
Remember to double backslashes (\\) in patterns because PostgreSQL uses backslash as an escape character.
Flags like 'i' can be added to make matching case-insensitive.
regexp_match returns NULL if no match is found.
Use regexp_match to find text patterns and get matching parts.
Use regexp_replace to change parts of text that match a pattern.
Regular expressions let you work with complex text rules easily.
Practice
regexp_match('abc123', '\\d+') return?Solution
Step 1: Understand regexp_match function
The function searches the input string for the first substring matching the pattern.Step 2: Apply pattern '\\d+' to 'abc123'
The pattern '\\d+' matches one or more digits. In 'abc123', '123' matches.Final Answer:
An array with the first sequence of digits found: ['123'] -> Option DQuick Check:
regexp_match returns first match array [OK]
- Thinking regexp_match returns the whole string
- Expecting a string instead of an array
- Confusing regexp_match with regexp_replace
regexp_replace in PostgreSQL?Solution
Step 1: Understand regexp_replace parameters
regexp_replace(text, pattern, replacement, flags) replaces matching parts. To replace all matches, use flag 'g'.Step 2: Choose pattern and flags to replace all digits
Pattern '\\d+' matches one or more digits. Flag 'g' ensures all digit sequences replaced, not just first.Final Answer:
regexp_replace('abc123def', '\\d+', '#', 'g') -> Option AQuick Check:
Use 'g' flag to replace all matches [OK]
- Omitting 'g' flag replaces only first match
- Using '\\d' without 'g' replaces only one digit
- Confusing pattern syntax with wrong brackets
SELECT regexp_match('user_2023_data', 'user_(\\d{4})_data');Solution
Step 1: Analyze the pattern 'user_(\\d{4})_data'
The pattern looks for 'user_' followed by exactly 4 digits (captured), then '_data'.Step 2: Match pattern against 'user_2023_data'
The string matches perfectly, capturing '2023' as the group.Final Answer:
['2023'] -> Option CQuick Check:
Captured group returns ['2023'] [OK]
- Expecting full string instead of captured group
- Not escaping backslashes properly
- Confusing regexp_match with regexp_replace output
SELECT regexp_replace('abc123def', '\\d+', '#');It is intended to replace all digit sequences with '#'.
Solution
Step 1: Understand regexp_replace behavior without flags
Without 'g' flag, regexp_replace replaces only the first match.Step 2: Check the pattern and replacement
Pattern '\\d+' is valid and replacement '#' is correct. Only missing 'g' flag causes partial replacement.Final Answer:
Missing 'g' flag, so only first digit sequence replaced -> Option BQuick Check:
Use 'g' flag to replace all matches [OK]
- Assuming all matches replaced without 'g'
- Thinking pattern syntax is wrong
- Using single quotes incorrectly for replacement
regexp_match. Which query correctly extracts 'example'?Solution
Step 1: Understand regexp_match returns array of groups
regexp_match returns an array with captured groups. To get the domain, access first element [1].Step 2: Check pattern '@([\\w]+)\\.com'
This pattern captures word characters after '@' and before '.com', which is the domain.Final Answer:
SELECT (regexp_match('user@example.com', '@([\\w]+)\\.com'))[1]; -> Option AQuick Check:
Access first group from regexp_match array [OK]
- Not accessing array element from regexp_match
- Using regexp_replace without global flag incorrectly
- Confusing pattern syntax for domain extraction
Ready to prove it?
Pass the test and this pattern moves unknown
