0
0
PostgreSQLquery~20 mins

Regular expression functions (regexp_match, regexp_replace) in PostgreSQL - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Regex Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2:00remaining
What is the output of regexp_match with a capturing group?
Consider the following SQL query:
SELECT regexp_match('abc123xyz', '[a-z]+(\d+)[a-z]+');

What is the output of this query?
PostgreSQL
SELECT regexp_match('abc123xyz', '[a-z]+(\d+)[a-z]+');
ANULL
B{abc123xyz}
C{abc123xyz,123}
D{abc, 123, xyz}
Attempts:
2 left
💡 Hint
regexp_match returns the whole match followed by captured groups as a text array.
query_result
intermediate
2:00remaining
What does regexp_replace do with global flag?
Given the query:
SELECT regexp_replace('a1b2c3', '\d', 'X', 'g');

What is the output?
PostgreSQL
SELECT regexp_replace('a1b2c3', '\d', 'X', 'g');
AaXb2c3
BaXbXcX
Ca1b2c3
DaXbXc3
Attempts:
2 left
💡 Hint
The 'g' flag means replace all matches, not just the first.
📝 Syntax
advanced
2:00remaining
Which regexp_replace syntax is correct to replace only first digit?
Which of the following queries correctly replaces only the first digit in 'abc123' with 'Z'?
ASELECT regexp_replace('abc123', '\d', 'Z');
BSELECT regexp_replace('abc123', '\d', 'Z', 'g');
CSELECT regexp_replace('abc123', '\d+', 'Z', 'g');
DSELECT regexp_replace('abc123', '\d+', 'Z');
Attempts:
2 left
💡 Hint
Without 'g' flag, only the first match is replaced.
🧠 Conceptual
advanced
2:00remaining
What error occurs with invalid regexp pattern?
What error will this query produce?
SELECT regexp_match('test', '[a-z+');
PostgreSQL
SELECT regexp_match('test', '[a-z+');
Asyntax error in regular expression
Breturns NULL
Cempty array {}
Druntime error: invalid input syntax
Attempts:
2 left
💡 Hint
Unclosed bracket in regex causes syntax error.
optimization
expert
3:00remaining
Which regexp_replace is most efficient to remove all vowels?
You want to remove all vowels (a, e, i, o, u) from a large text column. Which query is most efficient?
ASELECT regexp_replace(text_column, '[aeiou]', '', 'g') FROM big_table;
BSELECT regexp_replace(text_column, '[aeiou]+', '', 'g') FROM big_table;
CSELECT replace(replace(replace(text_column, 'a', ''), 'e', ''), 'i', '') FROM big_table;
DSELECT translate(text_column, 'aeiou', '') FROM big_table;
Attempts:
2 left
💡 Hint
translate is faster than regexp_replace for single character replacements.