📖 Scenario: You are managing a customer database for an online store. Some customer phone numbers are stored in different formats. You want to clean and extract parts of these phone numbers using regular expressions.
🎯 Goal: Build SQL queries using PostgreSQL's regexp_match and regexp_replace functions to extract area codes and standardize phone number formats.
📋 What You'll Learn
Create a table called customers with a phone column containing specific phone numbers.
Add a variable or setting to define the pattern for area codes.
Write a query using regexp_match to extract the area code from each phone number.
Write a query using regexp_replace to format all phone numbers into the standard format (XXX-XXX-XXXX).
💡 Why This Matters
🌍 Real World
Cleaning and standardizing phone numbers is common in customer databases to ensure consistent data for communication.
💼 Career
Database administrators and data analysts often use regular expressions in SQL to extract and transform text data efficiently.
Progress0 / 4 steps
1
Create the customers table with phone numbers
Create a table called customers with a column phone of type text. Insert these exact phone numbers as rows: '(123) 456-7890', '123.456.7890', '123-456-7890', '1234567890'.
PostgreSQL
Hint
Use CREATE TABLE to make the table and INSERT INTO to add the phone numbers exactly as shown.
2
Define the area code pattern
Create a variable called area_code_pattern and set it to the regular expression string '\\d{3}' which matches exactly three digits.
PostgreSQL
Hint
Use the \set command in psql to create a variable with the pattern string.
3
Extract area codes using regexp_match
Write a SQL query selecting phone and the first match of the area code using regexp_match(phone, '\\d{3}') as area_code from the customers table.
PostgreSQL
Hint
Use regexp_match to get the first group matching three digits and select it as area_code.
4
Standardize phone numbers using regexp_replace
Write a SQL query selecting phone and a new column standard_phone where you use regexp_replace to convert all phone numbers into the format XXX-XXX-XXXX. Use the pattern '\\D' to remove all non-digit characters first, then insert dashes with regexp_replace.
PostgreSQL
Hint
First remove all non-digit characters with regexp_replace(phone, '\\D', '', 'g'). Then apply another regexp_replace to insert dashes between groups of digits.
Practice
(1/5)
1. What does the PostgreSQL function regexp_match('abc123', '\\d+') return?
easy
A. An error because of incorrect syntax
B. The string 'abc123' unchanged
C. An empty array because no digits are found
D. An array with the first sequence of digits found: ['123']
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 D
Quick Check:
regexp_match returns first match array [OK]
Hint: regexp_match returns first matching group as array [OK]
Common Mistakes:
Thinking regexp_match returns the whole string
Expecting a string instead of an array
Confusing regexp_match with regexp_replace
2. Which of the following is the correct syntax to replace all digits in 'abc123def' with '#' using regexp_replace in PostgreSQL?
easy
A. regexp_replace('abc123def', '\\d+', '#', 'g')
B. regexp_replace('abc123def', '[0-9]', '#', 'g')
C. regexp_replace('abc123def', '[0-9]+', '#')
D. regexp_replace('abc123def', '\\d', '#')
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 A
Quick Check:
Use 'g' flag to replace all matches [OK]
Hint: Use 'g' flag in regexp_replace to replace all matches [OK]