Skip to content

REGEXP_REPLACE

REGEXP_REPLACE(expr, pat, repl[, position[, occurrence[, match_type]]])

Section titled “REGEXP_REPLACE(expr, pat, repl[, position[, occurrence[, match_type]]])”
  • expr: This is the string or column of a table that is to be searched for a pattern.
  • pat: The pattern to be searched within the expr parameter. It is the regular expression that identifies what will be replaced.
  • repl: A string to replace the matched patterns in the expr.
  • position: An optional parameter specifying the position in the expr where the function starts the search for the matching pattern. The default position is 1, indicating the beginning of the string.
  • occurrence: Also optional, it indicates which occurrence of the matched pattern will be replaced. If omitted, all occurrences will be replaced.
  • match_type: Another optional parameter which determines how the function matches the pattern. It can be ‘c’ (case sensitive), ‘i’ (case insensitive), ‘m’ (multi-line mode), ‘n’ (UNIX line endings), ‘u’ (Unicode), or ‘x’ (ignore whitespace and comments in the pattern). The default match type is ‘c’.
SELECT REGEXP_REPLACE('12345678', '(\\d{2})', '\\1-') AS FormattedNumber;
+-----------------+
| FormattedNumber |
+-----------------+
| 12-34-56-78 |
+-----------------+

The REGEXP_REPLACE function in this example finds every group of two digits in the string ‘12345678’, denoted by ’(\d2)’, and replaces them with the same number followed by a dash, denoted by ‘\1-’. The result is the original number separated into pairs by dashes.