Preamble
The Oracle/PLSQL REGEXP_REPLACE function is an extension of the function REPLACE. This function, introduced in Oracle 10g, allows you to replace a sequence of characters in a string with a different set of characters using regular expression pattern mapping.
Syntax of the Oracle/PLSQL function REGEXP_REPLACE
REGEXP_REPLACE( string_id, pattern_id [, replacement_string_id [, start_position_id [, nth_appearance_id [, match_parameter_id ] ] ] ] ]
Parameters and function arguments
- string_id – A search line. It can be CHAR, VARCHAR2, NCHAR, NVARCHAR2, CLOB or NCLOB.
- pattern_id – Template. Regular expression for comparison. It can be a combination of the following values:
Meaning | Description |
^ | Corresponds to the beginning of the line. When using match_parameter with m, corresponds to the beginning of the string anywhere within the expression. |
$ | Corresponds to the end of the line. When using match_parameter with m, it corresponds to the end of the string anywhere within the expression. |
* | Corresponds to zero or more occurrences. |
? | Corresponds to one or more occurrences. |
? | Corresponds to zero or one entry. |
. | Corresponds to any character except NULL. |
| | Used as “OR” to specify more than one alternative. |
[ ] | It is used to specify a list of matches where you try to match any of the characters in the list. |
[^ ] | It is used to specify a nonmatching list where you try to match any character except for those on the list. |
( ) | Used for group expressions as subexpressions. |
{m} | Corresponds m times. |
{m,} | Matching at least m times. |
{m,n} | Matching at least m times, but not more than n times. |
\n | n is a number between 1 and 9. It corresponds to the n-th subexpression located in ( ) before \n. |
[..] | Corresponds to a single element mappings that can be more than one character. |
[::] | Meets the symbol class. |
[==] | Corresponds to the class of equivalence. |
\d | Corresponds to the digital symbol. |
\D | Corresponds to a non-digital symbol. |
\w | Corresponds to the text symbol. |
\W | Corresponds to a non-text symbol. |
\s | Corresponds to the space character. |
\S | Doesn’t match the space character. |
\A | Corresponds to the beginning of a line or corresponds to the end of a line before a new line character. |
\Z | Corresponds to the end of the line. |
*? | Corresponds to the previous pattern of zero or more occurrences. |
+? | One or more entries correspond to the previous template. |
?? | Corresponds to the previous zero or one entry pattern. |
{n}? | Corresponds to the previous template n times. |
{n,}? | Corresponds to the previous template at least n times. |
{n,m}? | Corresponds to the previous template at least n times, but not more than m times. |
- replacement_string_id – It’s optional. The corresponding templates in the string will be replaced with replace_string. If the replacement_string parameter is omitted, the function simply deletes all matching templates and returns the resulting string.
- start_position_id – Optional. This is the position in the string from which the search will start. If this parameter is omitted, by default it is 1, which is the first position in the string.
- nth_appearance_id – Optional. This is the n-th view of the pattern in the string. If this option is omitted, it defaults to 1, which is the first entry of the template in the string. If you specify 0 for this parameter, all template entries in the string will be replaced.
- match_parameter_id – It’s optional. This allows you to change the compliance behavior for the REGEXP_REPLACE condition. This can be a combination of the following values:
Meaning | Description |
‘c’ | Performs register-sensitive alignment. |
‘i’ | Performs case insensitive alignment. |
‘n’ | Allows a character period (.) to match the character of a new string. By default, the metasymic period. |
‘m’ | The expression assumes that there are several lines where ^ is the beginning of a line and $ is the end of a line, regardless of the position of these characters in the expression. By default, the expression is assumed to be on the same line. |
‘x’ | The symbols of spaces are ignored. By default, the space characters are the same as any other character. |
The function REGEXP_REPLACE returns a string value.
If there are conflicting values for match_parameter, function REGEXP_REPLACE will use the last value.
The REGEXP_REPLACE function can be used in the following Oracle/PLSQL versions
Oracle 12c, Oracle 11g, Oracle 10g
Example of a match to the first word
Consider an example of using the REGEXP_REPLACE function to replace the first word in a string.
For example:
SELECT REGEXP_REPLACE ('Bing is a great search engine.', '^(\S*)', 'Google')
FROM dual;
--Result: 'Google is a great search engine.'
This example will return ‘Google is a great search engine.’ Because the beginning of the match at the beginning of the line, as indicated by ^, and then finds the first word according to (\S*). Then the function will replace this first word with ‘Google’.
Example of a number match
Let’s consider an example of how we will use the OracleREGEXP_REPLACE function to compare a pattern of digital characters.
For example:
SELECT REGEXP_REPLACE ('1, 4, and 10 numbers for example.', '\d', '@')
FROM dual;
--Result: @, @, and @@@ numbers for example.
This example will replace all numbers in the string, as specified in the \d template, with @.
We could change our template to search only for two-digit numbers
For example:
SELECT REGEXP_REPLACE ('1, 4, and 10 numbers for example', '(\d)(\d)', '@')
FROM dual;
--Result: 1, 4, and @ numbers for example
This example will replace a number that has two digits, as specified in the template (\d) (\d). In this case it will skip the numeric values 2 and 5 and replace 10 with @.
Now let’s see how we will use the REGEXP_REPLACE function with the table column to replace the two-digit numbers.
For example:
SELECT REGEXP_REPLACE (address, '(\d)(\d)', 'Str')
FROM contacts;
In this example, we will replace all two-digit values of the address field in the contacts table with ‘Str’.
This is an example of comparing several alternatives.
The next example we will look at involves using the | template. | the template is used as an ‘OR’ to specify multiple alternatives.
For instance:
SELECT REGEXP_REPLACE ('AeroSmith', 'a|e|i|o|u', 'R')
FROM dual;
-Result: ARrRSmRth
This example will return ‘ARrRSmRth’ because it looks for the first vowel (a, e, i, o or u) in the string. Since we didn’t specify a match_parameter value, the REGEXP_REPLACE function will perform a case sensitive search, which means that ‘A’ in ‘AeroSmith’ will not match.
We could modify our query to perform a case-insensitive search as follows:
SELECT REGEXP_REPLACE ('AeroSmith', 'a|e|i|o|u', 'R', 1, 0, 'i')
FROM dual;
--Result: RRrRSmRth
Now, since we specified match_parameter = ‘i’, the query will replace ‘A’ in the string. This time, ‘A’ in ‘AeroSmith’ will be matched with a template. Note also that we have specified the 5th parameter as 0 to replace all occurrences.
Now consider how you will use this function with the column.
About Enteros
Enteros offers a patented database performance management SaaS platform. It proactively identifies root causes of complex business-impacting database scalability and performance issues across a growing number of clouds, RDBMS, NoSQL, and machine learning database platforms.
The views expressed on this blog are those of the author and do not necessarily reflect the opinions of Enteros Inc. This blog may contain links to the content of third-party sites. By providing such links, Enteros Inc. does not adopt, guarantee, approve, or endorse the information, views, or products available on such sites.
Are you interested in writing for Enteros’ Blog? Please send us a pitch!
RELATED POSTS
Enteros and Generative AI in the Insurance Sector: Performance Monitoring and Enterprise Efficiency
- 21 February 2025
- Database Performance Management
In the fast-evolving world of finance, where banking and insurance sectors rely on massive data streams for real-time decisions, efficient anomaly man…
Enhancing Real Estate IT Efficiency with Enteros: Leveraging Observability, Cloud FinOps, and Database Performance Optimization
In the fast-evolving world of finance, where banking and insurance sectors rely on massive data streams for real-time decisions, efficient anomaly man…
Enteros and CloudTech: Database Performance and RevOps in the BFSI Sector
- 20 February 2025
- Database Performance Management
In the fast-evolving world of finance, where banking and insurance sectors rely on massive data streams for real-time decisions, efficient anomaly man…
Optimizing Cost Attribution and Estimation in Healthcare: Enhancing Database Performance Monitoring with Enteros
In the fast-evolving world of finance, where banking and insurance sectors rely on massive data streams for real-time decisions, efficient anomaly man…