REQUEST COMPLIMENTARY SQLS*PLUS LICENCE
Oracle REGEXP_REPLACE function
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 Oracle REGEXP_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.
Oracle regular expression: replace a substring REGEXP_REPLACE
MORE NEWS
PreambleNoSql is not a replacement for SQL databases but is a valid alternative for many situations where standard SQL is not the best approach for...
PreambleMongoDB Conditional operators specify a condition to which the value of the document field shall correspond.Comparison Query Operators $eq...
5 Database management trends impacting database administrationIn the realm of database management systems, moreover half (52%) of your competitors feel...
The data type is defined as the type of data that any column or variable can store in MS SQL Server. What is the data type? When you create any table or...
PreambleMS SQL Server is a client-server architecture. MS SQL Server process starts with the client application sending a query.SQL Server accepts,...
First the basics: what is the master/slave?One database server (“master”) responds and can do anything. A lot of other database servers store copies of all...
PreambleAtom Hopper (based on Apache Abdera) for those who may not know is an open-source project sponsored by Rackspace. Today we will figure out how to...
PreambleMongoDB recently introduced its new aggregation structure. This structure provides a simpler solution for calculating aggregated values rather...
FlexibilityOne of the most advertised features of MongoDB is its flexibility. Flexibility, however, is a double-edged sword. More flexibility means more...
PreambleSQLShell is a cross-platform command-line tool for SQL, similar to psql for PostgreSQL or MySQL command-line tool for MySQL.Why use it?If you...
PreambleWriting an application on top of the framework on top of the driver on top of the database is a bit like a game on the phone: you say “insert...
PreambleOracle Coherence is a distributed cache that is functionally comparable with Memcached. In addition to the basic function of the API cache, it...
PreambleIBM pureXML, a proprietary XML database built on a relational mechanism (designed for puns) that offers both relational ( SQL / XML ) and...
What is PostgreSQL array? In PostgreSQL we can define a column as an array of valid data types. The data type can be built-in, custom or enumerated....
PreambleIf you are a Linux sysadmin or developer, there comes a time when you need to manage an Oracle database that can work in your environment.In this...
PreambleStarting with Microsoft SQL Server 2008, by default, the group of local administrators is no longer added to SQL Server administrators during the...