0
0
PostgreSQLquery~5 mins

Regular expression matching (~ operator) in PostgreSQL - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What does the ~ operator do in PostgreSQL?
The ~ operator checks if a string matches a regular expression pattern. It returns true if the pattern matches, false otherwise.
Click to reveal answer
beginner
How do you write a query to find rows where the column name contains only digits using ~?
Use WHERE name ~ '^\d+$'. This means the entire name must be digits from start (^) to end ($).
Click to reveal answer
intermediate
What is the difference between ~ and ~* operators in PostgreSQL?
~ is case-sensitive regex match, while ~* is case-insensitive regex match.
Click to reveal answer
intermediate
Write a PostgreSQL query using ~ to find emails ending with @example.com.
SELECT * FROM table WHERE email ~ '@example\.com$'; <br> The \. escapes the dot, and $ means end of string.
Click to reveal answer
intermediate
Can the ~ operator be used to check for multiple patterns at once?
No, ~ matches one regex pattern at a time. To check multiple patterns, combine conditions with OR or use regex alternation like pattern1|pattern2.
Click to reveal answer
What does the PostgreSQL operator ~ do?
AConcatenates two strings
BPerforms a case-insensitive string comparison
CChecks if a string contains only letters
DMatches a string against a regular expression pattern
Which operator would you use for case-insensitive regex matching in PostgreSQL?
A~*
B~
C!~
DILIKE
What does the regex pattern ^\d+$ mean when used with ~?
AString contains at least one digit
BString contains only digits from start to end
CString ends with a digit
DString starts with a digit
How do you escape a dot (.) in a regex pattern for PostgreSQL?
A\.
B.
C\d
D\*
Can the ~ operator match multiple patterns separated by commas?
AYes, it matches all patterns automatically
BYes, but only if patterns are in quotes
CNo, it matches only one regex pattern at a time
DNo, it only matches numeric patterns
Explain how the ~ operator works in PostgreSQL and give an example query.
Think about how you check if a string fits a pattern using regex.
You got /4 concepts.
    Describe the difference between ~ and ~* operators and when to use each.
    Consider if letter case matters in your pattern matching.
    You got /3 concepts.