Applies to: ✅ Data 360 SQL ✅ Tableau Hyper API
Replaces substrings matching a regular expression pattern with a replacement string.
Syntax
regexp_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
| Flag | Description |
|---|---|
g |
Global replacement — replace all matches, not just the first |
i |
Case-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\0to insert the entire match. Use\\for a literal backslash. - By default, replaces only the first occurrence. Use the
gflag for global replacement. - Case-insensitive matching can also come from the query's collation; the
iflag applies regardless of collation. - For detailed regex syntax, see Regular Expression Syntax.
Examples
Basic Replacement
Replace digits with a placeholder.
SELECT regexp_replace('foo123bar', '\d+', 'X') AS result;
Returns 'fooXbar'.
Using Captured Groups
Use captured groups in the replacement.
SELECT regexp_replace('John Smith', '(\w+) (\w+)', '\2, \1') AS result;
Returns 'Smith, John'.
Remove Patterns
Remove matching patterns by replacing with empty string.
SELECT 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.
SELECT regexp_replace('a1b2c3', '[0-9]', '#', 'g') AS result;
Returns 'a#b#c#'.
Case-Insensitive Replacement
Use the i flag for case-insensitive matching.
SELECT regexp_replace('Thomas', '.[mN]A.', 'M', 'i') AS result;
Returns 'ThM'.