- Categories:
REGEXP_REPLACE¶
Returns the subject with the specified pattern — or all occurrences of the pattern — either removed or replaced by a replacement string.
Syntax¶
Arguments¶
Required:
subjectThe string to search for matches.
patternPattern to match.
For guidelines on specifying patterns, see String functions (regular expressions).
Optional:
replacementString that replaces the substrings matched by the pattern. If an empty string is specified, the function removes all matched patterns and returns the resulting string.
Default:
''(empty string).positionNumber of characters from the beginning of the string where the function starts searching for matches. The value must be a positive integer.
Default:
1(the search for a match starts at the first character on the left)occurrenceSpecifies which occurrence of the pattern to replace. If
0is specified, all occurrences are replaced.Default:
0(all occurrences)parametersString of one or more characters that specifies the parameters used for searching for matches. Supported values:
Parameter
Description
cCase-sensitive matching
iCase-insensitive matching
mMulti-line mode
eExtract submatches
sSingle-line mode POSIX wildcard character
.matches\nDefault:
cFor more information, see Specifying the parameters for the regular expression.
Returns¶
Returns a value of type VARCHAR.
If no matches are found, returns the original subject.
Returns NULL if any argument is NULL.
Usage notes¶
The replacement string can contain backreferences to capture groups; for example, sub-expressions of the pattern. A capture group is a regular expression that is enclosed within parentheses (
( )). The maximum number of capture groups is nine.Backreferences match expressions inside a capture group. Backreferences have the form
nwherenis a value from 0 to 9, inclusive, which refers to the matching instance of the capture group. For more information, see Examples (in this topic).Parentheses (
( )) and square brackets ([ ]) currently must be double-escaped to parse them as literal strings.The example below shows how to remove parentheses:
For additional usage notes, see the General usage notes for regular expression functions.
Collation details¶
Arguments with collation specifications currently aren’t supported.
Examples¶
The following example replaces all spaces in the string with nothing (that is, all spaces are removed):
The following example matches the string times and replaces it with the string days. Matching begins at the first
character in the string and replaces the second occurrence of the substring:
The following example uses backreferences to rearrange the string firstname middlename lastname as
lastname, firstname middlename and insert a comma between lastname and firstname:
The remaining examples use the data in the following table:
The following example inserts the character * between every character of the subject, including the beginning and the end,
using an empty group (()), which finds a match between any two characters:
The following example removes all of the vowels by replacing them with nothing, regardless of their order or case:
The following example removes all words that contain the lowercase letter o from the subject by matching
a word boundary (\b), followed by zero or more word characters (\S), the letter o, and then zero or more
word characters until the next word boundary:
The following example replaces all words that contain the lowercase letter o, swapping
the letters in front of and behind the first instance of o, and replacing the o with the character
sequence @@:
The following example is the same as the previous example, but the replacement starts at position 3 in the subject:
The following example is the same as the previous example, but only the third occurrence is replaced, starting at position
3 in the subject:
The following example is the same as the previous example, but it uses case-insensitive matching: