# `REGEXP_REPLACE`

**Applies to:** ✅ Data 360 SQL ✅ Tableau Hyper API

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

## Syntax

```sql
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 `\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](/docs/data/data-cloud-query-guide/references/dc-sql-reference/string-matching.md#regular-expression-syntax).

## Examples

### Basic Replacement

Replace digits with a placeholder.

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

Returns `'fooXbar'`.

### Using Captured Groups

Use captured groups in the replacement.

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

Returns `'Smith, John'`.

### Remove Patterns

Remove matching patterns by replacing with empty string.

```sql
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.

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

Returns `'a#b#c#'`.

### Case-Insensitive Replacement

Use the `i` flag for case-insensitive matching.

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

Returns `'ThM'`.

## Related Documentation

- [REPLACE](/docs/data/data-cloud-query-guide/references/dc-sql-reference/replace.md)
- [REGEXP_SUBSTR](/docs/data/data-cloud-query-guide/references/dc-sql-reference/regexp-substr.md)
- [Regular Expression Syntax](/docs/data/data-cloud-query-guide/references/dc-sql-reference/string-matching.md#regular-expression-syntax)
