PostgreSQL - REGEXP_MATCHES Function
Last Updated :
19 Nov, 2024
The PostgreSQL REGEXP_MATCHES() function is a powerful tool for matching POSIX regular expressions against a string. It returns substrings that satisfy the pattern, making it indispensable for string manipulation, pattern matching, and data extraction tasks. In this article, we will explain the syntax, examples, and important considerations to help us master the REGEXP_MATCHES() function.
What is PostgreSQL REGEXP_MATCHES?
The REGEXP_MATCHES() function in PostgreSQL evaluates a given regular expression pattern against a string and returns matching substrings. It is particularly useful for tasks that require advanced text processing such as extracting, analyzing, or validating data based on specific patterns.
Unlike basic string-matching functions, REGEXP_MATCHES supports complex patterns and can return multiple matches, making it highly flexible for data extraction and manipulation.
Syntax
REGEXP_MATCHES(source_string, pattern [, flags])
Key Terms
- source_string: The string from which the regular expression will match and return the substrings.
- pattern: A POSIX regular expression used to match against the source string.
- flags (optional): Flags that modify the matching behavior. For example, 'g' (global) allows for multiple matches within the source string.
Return Value
The REGEXP_MATCHES() function returns a set of text arrays containing substrings that match the given pattern.
Examples of PostgreSQL REGEXP_MATCHES Function
Let us take a look at some of the examples of REGEXP_MATCHES Function in PostgreSQL to better understand the concept. These examples demonstrate how to use regular expressions effectively for various text-processing tasks.
Example 1: Extracting Hashtags from a Social Media Post
This example demonstrates how to extract all hashtags from a text string, Using regular expressions to identify patterns starting with #
. Suppose, we have a social networking’s post as follows
'Learning #Geeksforgeeks #geekPower'
The following statement allows us to extract the hashtags such as 'Geeksforgeeks' and 'geekPower':
SELECT
REGEXP_MATCHES('Learning #Geeksforgeeks #geekPower', '#([A-Za-z0-9_]+)', 'g');
Output

Explanation:
In this example, the regular expression '#([A-Za-z0-9_]+)' is used to match any hashtag that starts with a '#' followed by alphanumeric characters or underscores. The 'g' flag ensures that all matching patterns are returned.
Example 2: Matching Specific Patterns
We can use the REGEXP_MATCHES() function to match various patterns. For instance, to match the string 'ABC' with a pattern that identifies groups. This query demonstrates how grouping constructs in the pattern capture specific parts of the string, such as the first character and the remaining sequence.
Query:
SELECT REGEXP_MATCHES('ABC', '^(A)(..)$', 'g');
Output

Explanation:
In this case, the pattern ^(A)(..)$
captures the first character 'A' and the next two characters 'BC' as separate groups. The result groups are returned as an array.
Important Points About PostgreSQL REGEXP_MATCHES Function
- If there are multiple matches in the 'source_string', REGEXP_MATCHES will return a set of all matches.
- When the source string is NULL, REGEXP_MATCHES returns NULL and does not perform the regular expression matching.
- Be cautious with patterns that can match zero-length strings, such as '.*'. These can lead to infinite loops or unexpected results if not handled properly.
- The g flag (global) is often misunderstood. It does not change the function's behavior to find all matches in the way it does in other programming languages. Instead, REGEXP_MATCHES inherently returns all matches as a set.
Conclusion
The REGEXP_MATCHES() function in PostgreSQL is a flexible tool for extracting and analyzing data based on regular expressions. Its ability to handle multiple matches and return grouped results makes it ideal for tasks like pattern recognition, text mining, and data cleaning
Similar Reads
SQL Tutorial Structured Query Language (SQL) is the standard language used to interact with relational databases. Whether you want to create, delete, update or read data, SQL provides the structure and commands to perform these operations. SQL is widely supported across various database systems like MySQL, Oracl
8 min read
SQL Commands | DDL, DQL, DML, DCL and TCL Commands SQL commands are crucial for managing databases effectively. These commands are divided into categories such as Data Definition Language (DDL), Data Manipulation Language (DML), Data Control Language (DCL), Data Query Language (DQL), and Transaction Control Language (TCL). In this article, we will e
7 min read
Normal Forms in DBMS In the world of database management, Normal Forms are important for ensuring that data is structured logically, reducing redundancy, and maintaining data integrity. When working with databases, especially relational databases, it is critical to follow normalization techniques that help to eliminate
7 min read
Window Functions in SQL SQL window functions are essential for advanced data analysis and database management. It is a type of function that allows us to perform calculations across a specific set of rows related to the current row. These calculations happen within a defined window of data and they are particularly useful
6 min read
Top 60 DBMS Interview Questions with Answers for 2025 A Database Management System (DBMS) is the backbone of modern data storage and management. Understanding DBMS concepts is critical for anyone looking to work with databases. Whether you're preparing for your first job in database management or advancing in your career, being well-prepared for a DBMS
15+ min read
SQL Exercises : SQL Practice with Solution for Beginners and Experienced SQL (Structured Query Language) is a powerful and flexible tool for managing and manipulating relational databases. Regardless of our experience level, practising SQL exercises is essential for improving our skills. Regular practice not only enhances our understanding of SQL concepts but also builds
15+ min read
SQL Cheat Sheet ( Basic to Advanced) Creating and managing databases in SQL involves various commands and concepts that handle the structuring, querying, and manipulation of data. In this guide, we will see a comprehensive cheat sheet for essential SQL operations, offering a practical reference for tasks ranging from database creation
15 min read
SQL Views Views in SQL are a type of virtual table that simplifies how users interact with data across one or more tables. Unlike traditional tables, a view in SQL does not store data on disk; instead, it dynamically retrieves data based on a pre-defined query each time itâs accessed. SQL views are particular
7 min read
MySQL Tutorial This MySQL Tutorial is made for both beginners and experienced professionals. Whether you're starting with MYSQL basics or diving into advanced concepts, this free tutorial is the ideal guide to help you learn and understand MYSQL, no matter your skill level. From setting up your database to perform
11 min read
Indexing in Databases - Set 1 Indexing is a crucial technique used in databases to optimize data retrieval operations. It improves query performance by minimizing disk I/O operations, thus reducing the time it takes to locate and access data. Essentially, indexing allows the database management system (DBMS) to locate data more
8 min read