Skip to main content
TopMiniSite

Back to all posts

How to Replace String Using Regexp_replace In Oracle?

Published on
3 min read
How to Replace String Using Regexp_replace In Oracle? image

Best Tools for Oracle String Replacement to Buy in November 2025

1 Oracle 12c: SQL

Oracle 12c: SQL

BUY & SAVE
$53.93 $128.95
Save 58%
Oracle 12c: SQL
2 Murach's Oracle SQL and PL/SQL for Developers

Murach's Oracle SQL and PL/SQL for Developers

BUY & SAVE
$43.57 $54.50
Save 20%
Murach's Oracle SQL and PL/SQL for Developers
3 OCE Oracle Database SQL Certified Expert Exam Guide (Exam 1Z0-047) (Oracle Press)

OCE Oracle Database SQL Certified Expert Exam Guide (Exam 1Z0-047) (Oracle Press)

  • SAME-DAY DISPATCH FOR ORDERS BEFORE NOON-FAST DELIVERY!
  • MINT CONDITION GUARANTEED-QUALITY YOU CAN TRUST.
  • HASSLE-FREE RETURNS-SHOP WITH CONFIDENCE TODAY!
BUY & SAVE
$63.63 $68.00
Save 6%
OCE Oracle Database SQL Certified Expert Exam Guide (Exam 1Z0-047) (Oracle Press)
4 Easy Oracle PLSQL Programming: Get Started Fast with Working PL/SQL Code Examples (Easy Oracle Series)

Easy Oracle PLSQL Programming: Get Started Fast with Working PL/SQL Code Examples (Easy Oracle Series)

BUY & SAVE
$27.25
Easy Oracle PLSQL Programming: Get Started Fast with Working PL/SQL Code Examples (Easy Oracle Series)
5 SQL Pocket Guide: A Guide to SQL Usage

SQL Pocket Guide: A Guide to SQL Usage

BUY & SAVE
$23.73 $35.99
Save 34%
SQL Pocket Guide: A Guide to SQL Usage
6 Oracle SQL Developer 2.1

Oracle SQL Developer 2.1

BUY & SAVE
$38.05 $60.99
Save 38%
Oracle SQL Developer 2.1
7 Oracle PL/SQL Best Practices: Write the Best PL/SQL Code of Your Life

Oracle PL/SQL Best Practices: Write the Best PL/SQL Code of Your Life

  • QUALITY ASSURED: EACH BOOK IS CAREFULLY INSPECTED FOR READABILITY.
  • AFFORDABLE PRICING: SAVE MONEY WITH GENTLY USED, TOP-CONDITION TITLES.
  • ECO-FRIENDLY CHOICE: SUPPORT SUSTAINABILITY BY BUYING PRE-OWNED BOOKS.
BUY & SAVE
$13.43 $29.99
Save 55%
Oracle PL/SQL Best Practices: Write the Best PL/SQL Code of Your Life
8 Learn SQL By Examples: Examples of SQL Queries and Stored Procedures for MySQL and Oracle Databases

Learn SQL By Examples: Examples of SQL Queries and Stored Procedures for MySQL and Oracle Databases

BUY & SAVE
$4.99
Learn SQL By Examples: Examples of SQL Queries and Stored Procedures for MySQL and Oracle Databases
9 SQL Basics: Datamining Reference Guide for Oracle Databases

SQL Basics: Datamining Reference Guide for Oracle Databases

BUY & SAVE
$4.95
SQL Basics: Datamining Reference Guide for Oracle Databases
10 Oracle SQL By Example

Oracle SQL By Example

BUY & SAVE
$57.22
Oracle SQL By Example
+
ONE MORE?

To replace a string using REGEXP_REPLACE in Oracle, you need to specify the input string, the regular expression pattern to match, the replacement string, and any optional flags for the regex operation.

For example, if you want to replace all occurrences of the word "apple" with "orange" in a column named "fruit" in a table named "fruits_table", you can use the following SQL query:

UPDATE fruits_table SET fruit = REGEXP_REPLACE(fruit, 'apple', 'orange', 1, 0, 'i')

In this query:

  • The first argument 'fruit' is the input string column in which the replacement will be performed.
  • The second argument 'apple' is the regular expression pattern to match.
  • The third argument 'orange' is the replacement string.
  • The fourth argument '1' is the starting position in the string to begin replacing (1 means start from the beginning of the string).
  • The fifth argument '0' is the number of occurrences to replace. 0 means replace all occurrences.
  • The sixth argument 'i' is a flag for case-insensitive matching.

After running this query, all instances of the word "apple" in the "fruit" column will be replaced with "orange".

How to test the REGEXP_REPLACE function in Oracle before applying it to a full dataset?

One way to test the REGEXP_REPLACE function in Oracle before applying it to a full dataset is to use dummy data and run the function on a small sample set. This will allow you to verify that the function works as expected and produces the desired results.

Here is an example of how you can test the REGEXP_REPLACE function in Oracle:

-- Create a sample table with dummy data CREATE TABLE test_data ( id NUMBER, text VARCHAR2(100) );

INSERT INTO test_data VALUES (1, '123-456-7890'); INSERT INTO test_data VALUES (2, 'abc-def-ghi');

-- Run the REGEXP_REPLACE function on the sample data SELECT id, text, REGEXP_REPLACE(text, '[0-9]+', 'X') AS replaced_text FROM test_data;

In this example, the REGEXP_REPLACE function is used to replace all numerical digits in the 'text' column with the letter 'X'. By running this query on a small sample set, you can verify that the function works correctly and produces the desired output before applying it to a larger dataset.

How to escape special characters in the search pattern of REGEXP_REPLACE in Oracle?

To escape special characters in the search pattern of REGEXP_REPLACE in Oracle, you can use the backslash () character. Here is an example:

SELECT REGEXP_REPLACE('Hello@World!', '\@', '') FROM dual;

In this example, the backslash () is used to escape the special character "@" in the search pattern. The REGEXP_REPLACE function will replace the "@" character with an empty string, resulting in the output "HelloWorld!".

How to use boundary characters in REGEXP_REPLACE in Oracle?

Boundary characters in regex represent the beginning or end of a word or line. Here is an example of how to use boundary characters in REGEXP_REPLACE in Oracle:

  1. Using ^ character to represent the start of a word:

SELECT REGEXP_REPLACE('Hello World', '^\w+', 'Hi') AS replaced_text FROM dual;

This will replace the first word 'Hello' with 'Hi'.

  1. Using $ character to represent the end of a word:

SELECT REGEXP_REPLACE('Hello World', '\w+$', 'Universe') AS replaced_text FROM dual;

This will replace the last word 'World' with 'Universe'.

  1. Using \b characters to represent word boundary:

SELECT REGEXP_REPLACE('Hello World', '\b\w{5}\b', 'Hi') AS replaced_text FROM dual;

This will replace any 5-letter word in the string with 'Hi'.

These are just a few examples of how to use boundary characters in REGEXP_REPLACE in Oracle. You can combine boundary characters with other regex patterns to create more complex replacement rules.