Oracle REGEXP_REPLACE Function Guide
The REGEXP_REPLACE function in Oracle is used to replace parts of a string that match a specified pattern. Its syntax is as follows:
Replace the specified pattern in the source string with the replacement.
Among them:
- String to be replaced.
- pattern: regular expression pattern used for matching.
- replacement: substitute the matching part of the string.
Example of Usage:
SELECT REGEXP_REPLACE('Hello World', 'o', 'u') FROM dual;
-- 输出:Hellu Wurld
SELECT REGEXP_REPLACE('ABC123XYZ', '[A-Z]', '') FROM dual;
-- 输出:123
SELECT REGEXP_REPLACE('Hello123World', '\d', '') FROM dual;
-- 输出:HelloWorld
SELECT REGEXP_REPLACE('ABC-123-XYZ', '(\w+)-(\d+)-(\w+)', '\3-\2-\1') FROM dual;
-- 输出:XYZ-123-ABC
In the examples above, the first statement replaces the letter ‘o’ in the string with ‘u’. The second statement replaces uppercase letters in the string with an empty string. The third statement replaces numbers in the string with an empty string. Lastly, the final statement uses grouping captures and rearranges the matching parts in a specified order.