0
0
PostgreSQLquery~5 mins

Regular expression functions (regexp_match, regexp_replace) in PostgreSQL - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What does the PostgreSQL function regexp_match do?
It searches a string for a pattern using a regular expression and returns the first substring that matches as a text array.
Click to reveal answer
beginner
How does regexp_replace work in PostgreSQL?
It searches a string for a pattern using a regular expression and replaces the first match (by default) with a specified replacement string. Use the 'g' flag to replace all occurrences.
Click to reveal answer
intermediate
What is the return type of regexp_match in PostgreSQL?
It returns a text array containing the matched substring(s). If no match is found, it returns NULL.
Click to reveal answer
intermediate
How can you replace only the first match of a pattern using regexp_replace?
regexp_replace replaces only the first match by default (no 'g' flag). You can also specify the occurrence parameter as 1.
Click to reveal answer
beginner
Give an example of using regexp_match to extract the first word from a string.
Example: SELECT regexp_match('Hello world', '^\w+'); returns {Hello}.
Click to reveal answer
What does regexp_match('abc123', '\\d+') return?
A'123'
B{'abc'}
CNULL
D{'123'}
Which function replaces text matching a pattern in PostgreSQL?
Aregexp_replace
Breplace
Csubstring
Dregexp_match
What will regexp_replace('abc123abc', '\\d', 'X') return?
AabcX123abc
BabcXXXabc
CabcX23abc
DabcXabc
If regexp_match finds no match, what does it return?
AEmpty array
BNULL
CEmpty string
DError
How to replace only the first occurrence of a pattern using regexp_replace?
AUse 1 as occurrence parameter
BUse 'i' flag
CUse 'g' flag
DIt always replaces all occurrences
Explain how regexp_match works in PostgreSQL and what it returns.
Think about how you find a pattern in a sentence and what you get back.
You got /4 concepts.
    Describe how to use regexp_replace to change parts of a string matching a pattern.
    Imagine correcting typos in a text by pattern.
    You got /4 concepts.