REGEXP_REPLACE

Applies to: ✅ Data 360 SQL ✅ Tableau Hyper API

Replaces substrings matching a regular expression pattern with a replacement string.

Syntax 

1regexp_replace(<string>, <pattern>, <replacement> [, flags])

Arguments 

Required 

  • <string>: The source string.
  • <pattern>: The regular expression pattern to match.
  • <replacement>: The replacement string.

Optional 

  • flags: A string of option flags that modify matching behavior. Flags can be combined (for example, 'gi').

Flags 

FlagDescription
gGlobal replacement — replace all matches, not just the first
iCase-insensitive matching

Returns 

Returns a text value with matching substrings replaced.

Considerations 

  • The replacement string can contain \N (where N is 1-9) to insert captured groups from the pattern, or \0 to insert the entire match. Use \\ for a literal backslash.
  • By default, replaces only the first occurrence. Use the g flag for global replacement.
  • Case-insensitive matching can also come from the query’s collation; the i flag applies regardless of collation.
  • For detailed regex syntax, see Regular Expression Syntax.

Examples 

Basic Replacement 

Replace digits with a placeholder.

1SELECT regexp_replace('foo123bar', '\d+', 'X') AS result;

Returns 'fooXbar'.

Using Captured Groups 

Use captured groups in the replacement.

1SELECT regexp_replace('John Smith', '(\w+) (\w+)', '\2, \1') AS result;

Returns 'Smith, John'.

Remove Patterns 

Remove matching patterns by replacing with empty string.

1SELECT regexp_replace('Hello   World', '\s+', ' ') AS result;

Returns 'Hello World' with multiple spaces reduced to one.

Global Replacement 

Replace all occurrences using the g flag.

1SELECT regexp_replace('a1b2c3', '[0-9]', '#', 'g') AS result;

Returns 'a#b#c#'.

Case-Insensitive Replacement 

Use the i flag for case-insensitive matching.

1SELECT regexp_replace('Thomas', '.[mN]A.', 'M', 'i') AS result;

Returns 'ThM'.

Related Documentation