PostgreSQL REGEXP_MATCHES() Function
Summary: in this tutorial, you will learn how to use the PostgreSQL REGEXP_MATCHES()
function to extract substrings from a string based on a regular expression.
Introduction to the PostgreSQL REGEXP_MATCHES() function
The REGEXP_MATCHES()
function allows you to extract substrings from a string based on a regular expression pattern.
Here’s the basic syntax for the PostgreSQL REGEXP_MATCHES()
function:
The REGEXP_MATCHES()
function accepts three arguments:
1) source
The source
is a string that you want to extract substrings that match a regular expression.
2) pattern
The pattern
is a POSIX regular expression for matching.
3) flags
The flags
argument is one or more characters that control the behavior of the function. For example, i
allows you to match case-insensitively.
The REGEXP_MATCHES()
function returns a set of text, even if the result array only contains a single element.
PostgreSQL REGEXP_MATCHES() function examples
Let’s explore some examples of using the REGEXP_MATCHES()
function.
1) Basic REGEXP_MATCHES() function examples
The following example uses the REGEXP_MATCHES()
function to extract hashtags such as PostgreSQL
and REGEXP_MATCHES
from a string:
Output:
In this example, the following regular expression matches any word that starts with the hash character (#
) and is followed by any alphanumeric characters or underscore (_
).
The g
flag argument is for the global search.
The result set has two rows, each is an array (text[]
), which indicates that there are two matches.
If you want to transform the elements of the array into separate rows, you can use the UNNEST()
function:
Output:
2) Using the PostgreSQL REGEXP_MATCHES() function with table data example
We’ll use the following film
table from the sample database:
The following statement uses the REGEXP_MATCHES()
function to retrieve films with descriptions containing the word Cat
or Dog
:
Output:
Summary
- Use the PostgreSQL
REGEXP_MATCHES()
function to extract text from a string based on a regular expression.