Challenge - 5 Problems
Regex Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ query_result
intermediate2:00remaining
What is the output of regexp_match with a capturing group?
Consider the following SQL query:
What is the output of this 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]+');
Attempts:
2 left
💡 Hint
regexp_match returns the whole match followed by captured groups as a text array.
✗ Incorrect
The pattern '[a-z]+(\d+)[a-z]+' matches letters, then captures digits, then letters. regexp_match returns the whole match followed by captured groups as an array. So it returns {abc123xyz,123}.
❓ query_result
intermediate2:00remaining
What does regexp_replace do with global flag?
Given the query:
What is the output?
SELECT regexp_replace('a1b2c3', '\d', 'X', 'g');What is the output?
PostgreSQL
SELECT regexp_replace('a1b2c3', '\d', 'X', 'g');
Attempts:
2 left
💡 Hint
The 'g' flag means replace all matches, not just the first.
✗ Incorrect
The pattern '\d' matches digits. With 'g' flag, all digits are replaced by 'X'. So 'a1b2c3' becomes 'aXbXcX'.
📝 Syntax
advanced2: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'?
Attempts:
2 left
💡 Hint
Without 'g' flag, only the first match is replaced.
✗ Incorrect
Option A replaces only the first digit because no 'g' flag is given. Option A replaces all digits. Option A replaces all digit groups. Option A replaces only the first digit group.
🧠 Conceptual
advanced2: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+');
Attempts:
2 left
💡 Hint
Unclosed bracket in regex causes syntax error.
✗ Incorrect
The pattern '[a-z+' is invalid because the bracket is not closed. PostgreSQL raises a syntax error for invalid regex patterns.
❓ optimization
expert3: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?
Attempts:
2 left
💡 Hint
translate is faster than regexp_replace for single character replacements.
✗ Incorrect
translate replaces characters directly without regex overhead, making it faster for removing vowels than regexp_replace or nested replace calls.