PostgreSQL – REGEXP_REPLACE Function
Last Updated :
18 Nov, 2024
The PostgreSQL REGEXP_REPLACE() function is a powerful text manipulation tool, designed to replace specific substrings within a string based on regular expression patterns. This function is highly beneficial for cleaning, reformatting, and transforming textual data by allowing complex pattern matching and replacement functionality.
In this article, we will explain the PostgreSQL REGEXP_REPLACE() function in-depth, covering its syntax, common use cases, and detailed examples for effective string manipulation in PostgreSQL.
What is the PostgreSQL REGEXP_REPLACE Function?
The REGEXP_REPLACE() function in PostgreSQL enables us to search and replace parts of string that match a specified POSIX regular expression pattern. This allows for more dynamic and flexible text transformations than traditional replace functions, making it ideal for data cleaning and advanced string manipulation.
Why Use REGEXP_REPLACE() in PostgreSQL?
Using REGEXP_REPLACE() can save time and simplify tasks when dealing with:
- Data standardization (e.g., transforming name formats)
- Data cleansing (e.g., removing unwanted characters or digits)
- Complex text replacements that depend on dynamic patterns.
Syntax
REGEXP_REPLACE(source, pattern, replacement_string, [, flags])
Key Terms
'source_string': This is the original string where the search and replace operation is performed.
'pattern': A POSIX regular expression pattern that identifies substrings to be replaced.
'replacement_string': The string that replaces substrings matching the pattern.
'flags (optional)’: Controls the behavior of the matching operation. Commonly used flags include ‘g’ for global replacement and ‘i’ for case-insensitive matching.
PostgreSQL REGEXP_REPLACE Function Examples
Let us take a look at some of the examples of REGEXP_REPLACE() Function in PostgreSQL to better understand the concept. These examples will show how pattern-based replacements can be used for various data manipulation tasks.
Example 1: Rearranging Name Format
Suppose we have a name formatted as first_name last_name, and we want to reverse the order to last_name, first_name. The following query demonstrates how to use REGEXP_REPLACE() to achieve this:
Query:
SELECT REGEXP_REPLACE('Raju Kumar', '(.*) (.*)', '\2, \1');
Output

Explanation:
This query uses the regular expression '(.*) (.*)' to capture two groups (first_name and last_name). The backreference \2 (for last_name) and \1 (for first_name) rearranges the name format.
Example 2: Removing Alphabets from String
Suppose we have data in the form of a string. This string is mixed with alphabets and digits as ABC12345xyz and we want to remove all alphabetic characters, leaving only the digits. The following query removes all alphabets e.g., A, B, C, etc from the source string:
Query:
SELECT REGEXP_REPLACE('ABC12345xyz', '[[:alpha:]]', '', 'g');
Output

Explanation:
This query uses [[:alpha:]] to match any alphabetic character (A-Z, a-z). The global flag 'g' ensures that all alphabetic characters are removed from the string.
Important points about PostgreSQL REGEXP_REPLACE Function
- In
REGEXP_REPLACE(), you can use backreferences (‘\1', ‘\2', etc.) in the ‘replacement_string' to refer to groups captured in the pattern.
- Special characters in regular expressions (e.g.,
^, $, \, .) have specific meanings. Ensure proper escaping of these characters if they are meant to be interpreted literally.
- Use of POSIX character classes (‘
[:alpha:]', ‘[:digit:]', etc.) and non-greedy matching (.*?, .+?) can provide more precise control over pattern matching behavior.
- By default, PostgreSQL regular expressions are case-sensitive.
Conclusion
The REGEXP_REPLACE() function in PostgreSQL is a highly flexible tool that supports advanced pattern-based replacements, offering greater flexibility than standard replacement functions. By using POSIX regular expressions, backreferences, and flags, REGEXP_REPLACE() enables complex data transformations with ease, making it an invaluable asset for data cleansing and formatting tasks.
FAQs
What is REGEXP_REPLACE in PostgreSQL?
The REGEXP_REPLACE function in PostgreSQL is used to search a string for a specified pattern and replace occurrences of that pattern with a new substring. It’s a powerful tool for text manipulation, especially when working with complex patterns.
What is the use of REGEXP_REPLACE?
REGEXP_REPLACE is primarily used to replace parts of a string based on regular expression patterns, allowing for flexible and dynamic text transformations, such as cleaning data or formatting strings.
Does Regexp work in PostgreSQL?
Yes, regular expressions (regexp) are fully supported in PostgreSQL through functions like REGEXP_REPLACE, REGEXP_MATCHES, and SIMILAR TO, enabling advanced pattern matching and text processing.
Similar Reads
PostgreSQL - REGEXP_REPLACE Function
The PostgreSQL REGEXP_REPLACE() function is a powerful text manipulation tool, designed to replace specific substrings within a string based on regular expression patterns. This function is highly beneficial for cleaning, reformatting, and transforming textual data by allowing complex pattern matchi
4 min read
PostgreSQL - REPLACE() Function
PostgreSQL REPLACE() function is a powerful tool for efficient string manipulation within our database. By utilizing the REPLACE() syntax in PostgreSQL, we can easily replace specific substrings within a string and enabling us to clean, format and modify data effectively. In this article, We will le
4 min read
PostgreSQL REVERSE() Function
The REVERSE() function in PostgreSQL is a simple yet powerful tool used to reverse the order of characters in a given string. It takes one input which is a string and returns the characters in reverse order. This function is helpful when you need to transform data, run tests or validate information.
4 min read
PostgreSQL - UPPER Function
In PostgreSQL, the UPPER function is utilized to convert a string into uppercase. This function is handy when you need to standardize text data by converting it to a uniform case, especially for comparison or display purposes. Let us get a better understanding of the UPPER Function in PostgreSQL fro
2 min read
PostgreSQL - REGEXP_MATCHES Function
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 synt
4 min read
PostgreSQL - RIGHT Function
The PostgreSQL RIGHT() function, allows you to extract a specified number of characters from the right side of a string. This function can be incredibly useful for various text-processing tasks. Let us get a better understanding of the RIGHT Function in PostgreSQL from this article. SyntaxRIGHT(stri
2 min read
PostgreSQL - Function Returning A Table
In PostgreSQL, the ability to create functions that return tables enhances the power and flexibility of our database operations. These PostgreSQL RETURN TABLE functions allow us to execute complex queries and return structured data efficiently. In this article, we will explain the PostgreSQL functio
5 min read
PostgreSQL - TRIM Function
The TRIM() function in PostgreSQL is an essential tool for removing unwanted characters from strings. Whether we're working with user inputs, formatting text, or performing data cleansing operations, TRIM() is an invaluable function for managing string data. This article will provide an in-depth loo
4 min read
PostgreSQL String Functions
PostgreSQL is a powerful, open-source relational database management system that offers a rich set of functions and operators for working with string data. String manipulation is an essential task in many applications, and PostgreSQL provides a variety of built-in functions to make working with text
8 min read
PostgreSQL - Substring Function
PostgreSQL is a powerful relational database management system with extensive text processing functions, including the flexible SUBSTRING function. This function enables users to extract specific portions of a string, making it essential for text manipulation, especially when dealing with large data
4 min read