Oracle REGEXP_REPLACE Guide: Syntax & Examples
In Oracle, you can utilize the REGEXP_REPLACE function for regex replacement.
Syntax:
REGEXP_REPLACE(source_string, pattern, replacement [, start_position [, occurrence [, match_parameter ]]])
Explanation of Parameters:
- source string: the string to be replaced.
- Pattern: Regular expression pattern used to match content to be replaced.
- replaced string: the string that has been substituted.
- Optional: specify the starting position in the source string for the replacement, with the default value being 1.
- occurrence: specifies the number of times to replace the matching item, with the default being to replace all matching items.
- Optional match parameter can define some matching options for regular expressions, such as case sensitivity.
Original: 我最好的朋友是我从小就认识的。
Paraphrased: My best friend is someone I have known since I was a child.
- Replace all the letters in the string with “”: SELECT REGEXP_REPLACE(‘Hello World’, ‘[a-zA-Z]’, ”) FROM dual; Result: **** *****
- Replace the numbers in the string with an empty string:
SELECT REGEXP_REPLACE(‘abc123def456’, ‘[0-9]’) FROM dual;
Result: abcdef - Replace the first occurrence of a specified string in a given string:
SELECT REGEXP_REPLACE(‘Hello World’, ‘o’, ”) FROM dual;
Result: Hell World - Replace the second occurrence of a specified string in the string:
SELECT REGEXP_REPLACE(‘Hello World’, ‘o’, ”, 1, 2) FROM dual;
Output: Hello Wrld