

# Regular expression functions

Use these functions to test, extract, replace, and split text with regular expressions, when a pattern needs more than the wildcards of `LIKE`.

Patterns use RE2 syntax, which does not support lookahead or lookbehind assertions. The [`~` (bitwise NOT and regular expression match) operator](duckdb-functions-numeric-bitwise.md#athena-duckdb-functions-numeric-bitwise-not-operator) also tests whether a regular expression matches part of a string.

## `regexp_escape` function
<a name="athena-duckdb-functions-string-regexp-escape"></a>

Returns `string` with a backslash added before every ASCII character that is not a letter, a digit, or an underscore. Characters outside ASCII, such as the euro sign (`chr(8364)`), are not changed. Use the result as a pattern that matches `string` literally, for example when you build a pattern from text that contains characters such as `.`, `+`, or `?`.

### Syntax
<a name="athena-duckdb-functions-string-regexp-escape-syntax"></a>

```sql
regexp_escape({{string}})
```

### Arguments
<a name="athena-duckdb-functions-string-regexp-escape-arguments"></a>

The `regexp_escape` function takes the following argument:

**{{string}}**  
The string to escape, as a `VARCHAR` value.

### Return type
<a name="athena-duckdb-functions-string-regexp-escape-return-type"></a>

`VARCHAR`

### Examples
<a name="athena-duckdb-functions-string-regexp-escape-examples"></a>

The following SQL query escapes a price label that contains a dollar sign, a period, a space, and parentheses. The result has a backslash before each of these characters.

```sql
SELECT regexp_escape('$5.00 (approx)') AS result
```

The query returns the following result.


| result | 
| --- | 
| `\$5\.00\ \(approx\)` | 

The following SQL query compares an escaped pattern with an unescaped one. Escaped, `1+1` matches only the literal text `1+1`, so it does not match `11`. Unescaped, `+` means one or more, so the pattern matches.

```sql
SELECT
    regexp_matches(
        '11', regexp_escape('1+1')
    ) AS escaped,
    regexp_matches('11', '1+1') AS unescaped
```

The query returns the following result.


| escaped | unescaped | 
| --- | --- | 
| `false` | `true` | 

## `regexp_extract` function
<a name="athena-duckdb-functions-string-regexp-extract"></a>

Returns the first part of `string` that matches the regular expression `regex`. By default, returns the whole match. With `group`, returns that capturing group instead.

With `name_list`, returns every capturing group at once, as a `STRUCT` with one field for each name in `name_list`, in order.

If `regex` does not match, returns an empty string (or a `STRUCT` whose fields are empty strings), not `NULL`.

### Syntax
<a name="athena-duckdb-functions-string-regexp-extract-syntax"></a>

```sql
regexp_extract({{string}}, {{regex}} [, {{group}} ] [, {{options}} ])
regexp_extract({{string}}, {{regex}}, {{name_list}} [, {{options}} ])
```

### Arguments
<a name="athena-duckdb-functions-string-regexp-extract-arguments"></a>

The `regexp_extract` function takes the following arguments:

**{{string}}**  
The string to search, as a `VARCHAR` value.

**{{regex}}**  
The regular expression, as a `VARCHAR` value.

**{{group}}**  
The capturing group to return. It must be a constant `INTEGER` value. The default is `0`, which returns the whole match. This argument is optional.

**{{name\_list}}**  
The field names, one for each capturing group in `regex`. It must be a constant list of `VARCHAR` values (`VARCHAR[]`).

**{{options}}**  
The option letters, such as `'i'` for case-insensitive matching. It must be a constant `VARCHAR` value. This argument is optional.

### Return type
<a name="athena-duckdb-functions-string-regexp-extract-return-type"></a>

`VARCHAR`. With `name_list`, a `STRUCT` with one `VARCHAR` field for each name, for example `STRUCT(y VARCHAR, m VARCHAR, d VARCHAR)`.

### Examples
<a name="athena-duckdb-functions-string-regexp-extract-examples"></a>

The following SQL query returns the first capturing group, with case-insensitive matching.

```sql
SELECT
    regexp_extract(
        'ABC', '([a-z])(b)', 1, 'i'
    ) AS result
```

The query returns the following result.


| result | 
| --- | 
| `A` | 

The following SQL query has no match, so it returns an empty string, not `NULL`.

```sql
SELECT regexp_extract('abc', 'x') = '' AS is_empty
```

The query returns the following result.


| is\_empty | 
| --- | 
| `true` | 

The following SQL query returns the parts of a date as a `STRUCT` with named fields.

```sql
SELECT
    regexp_extract(
        '2023-04-15', '(\d+)-(\d+)-(\d+)',
        ['y', 'm', 'd']
    ) AS result
```

The query returns the following result.


| result | 
| --- | 
| `{'y': 2023, 'm': 04, 'd': 15}` | 

### Differences from Athena engine version 3
<a name="athena-duckdb-functions-string-regexp-extract-differences-from-engine-version-3"></a>

In Athena engine version 3, `regexp_extract` returns `NULL` when there is no match. In DuckDB, it returns an empty string. To get the Athena engine version 3 result, use `NULLIF(regexp_extract({{string}}, {{regex}}), '')`.

## `regexp_extract_all` function
<a name="athena-duckdb-functions-string-regexp-extract-all"></a>

Returns a list with one element for each match of the regular expression `regex` in `string`. Matches do not overlap, so a character is never part of two matches. By default, each element is the whole match. With `group`, each element is that capturing group.

With `name_list`, each element is a `STRUCT` that holds the capturing groups of one match, with one field for each name in `name_list`.

If there is no match, returns an empty list.

### Syntax
<a name="athena-duckdb-functions-string-regexp-extract-all-syntax"></a>

```sql
regexp_extract_all({{string}}, {{regex}} [, {{group}}
    [, {{options}} ] ])
regexp_extract_all({{string}}, {{regex}}, {{name_list}}
    [, {{options}} ])
```

### Arguments
<a name="athena-duckdb-functions-string-regexp-extract-all-arguments"></a>

The `regexp_extract_all` function takes the following arguments:

**{{string}}**  
The string to search, as a `VARCHAR` value.

**{{regex}}**  
The regular expression, as a `VARCHAR` value.

**{{group}}**  
The capturing group to return from each match, as an `INTEGER` value. The default is `0`, which returns the whole match. This argument is optional.

**{{name\_list}}**  
The field names, one for each capturing group in `regex`. It must be a constant list of `VARCHAR` values (`VARCHAR[]`).

**{{options}}**  
The option letters, such as `'i'` for case-insensitive matching. It must be a constant `VARCHAR` value. This argument is optional.

### Return type
<a name="athena-duckdb-functions-string-regexp-extract-all-return-type"></a>

`VARCHAR[]`. With `name_list`, a list of `STRUCT` values, for example `STRUCT(person VARCHAR, age VARCHAR)[]`.

### Examples
<a name="athena-duckdb-functions-string-regexp-extract-all-examples"></a>

The following SQL query returns the second capturing group, the age, from each match.

```sql
SELECT
    regexp_extract_all(
        'Peter: 33, Paul:14', '(\w+):\s*(\d+)', 2
    ) AS result
```

The query returns the following result.


| result | 
| --- | 
| `[33, 14]` | 

The following SQL query omits `group`, so each element of the list is a whole match.

```sql
SELECT
    regexp_extract_all(
        'Peter: 33, Paul:14', '(\w+):\s*(\d+)'
    ) AS result
```

The query returns the following result.


| result | 
| --- | 
| `['Peter: 33', 'Paul:14']` | 

The following SQL query returns the capturing groups of each match as a `STRUCT` with named fields.

```sql
SELECT
    regexp_extract_all(
        'Peter: 33, Paul:14', '(\w+):\s*(\d+)',
        ['person', 'age']
    ) AS result
```

The query returns the following result.


| result | 
| --- | 
| `[{'person': Peter, 'age': 33}, {'person': Paul, 'age': 14}]` | 

The following SQL query has no match, so it returns an empty list, and the list has a length of `0`.

```sql
SELECT
    len(
        regexp_extract_all('abc', '[0-9]')
    ) AS match_count
```

The query returns the following result.


| match\_count | 
| --- | 
| `0` | 

### Considerations
<a name="athena-duckdb-functions-string-regexp-extract-all-considerations"></a>

To pass `options` without `name_list`, you must also pass `group`. Use `0` to return whole matches.

## `regexp_full_match` function
<a name="athena-duckdb-functions-string-regexp-full-match"></a>

Returns `true` if the regular expression `regex` matches all of `string`, from the first character to the last, and `false` otherwise. To test for a match anywhere in the string, use the [`regexp_matches` function](#athena-duckdb-functions-string-regexp-matches).

### Syntax
<a name="athena-duckdb-functions-string-regexp-full-match-syntax"></a>

```sql
regexp_full_match({{string}}, {{regex}} [, {{options}} ])
```

### Arguments
<a name="athena-duckdb-functions-string-regexp-full-match-arguments"></a>

The `regexp_full_match` function takes the following arguments:

**{{string}}**  
The string to test, as a `VARCHAR` value.

**{{regex}}**  
The regular expression, as a `VARCHAR` value.

**{{options}}**  
The option letters, such as `'i'` for case-insensitive matching. It must be a constant `VARCHAR` value. This argument is optional.

### Return type
<a name="athena-duckdb-functions-string-regexp-full-match-return-type"></a>

`BOOLEAN`

### Examples
<a name="athena-duckdb-functions-string-regexp-full-match-examples"></a>

The following SQL query returns `false`, because `(an)*` matches only part of the string.

```sql
SELECT regexp_full_match('anabanana', '(an)*') AS result
```

The query returns the following result.


| result | 
| --- | 
| `false` | 

The following SQL query compares `regexp_full_match` with `regexp_matches` for a pattern that matches only the middle of the string.

```sql
SELECT
    regexp_full_match('abc', 'b') AS whole_string,
    regexp_matches('abc', 'b') AS anywhere
```

The query returns the following result.


| whole\_string | anywhere | 
| --- | --- | 
| `false` | `true` | 

The following SQL query uses the `'i'` option for case-insensitive matching.

```sql
SELECT regexp_full_match('ABC', 'abc', 'i') AS result
```

The query returns the following result.


| result | 
| --- | 
| `true` | 

## `regexp_matches` function
<a name="athena-duckdb-functions-string-regexp-matches"></a>

Returns `true` if any part of `string` matches the regular expression `regex`, and `false` otherwise. To require the match to start at the beginning or finish at the end of the string, anchor the pattern with `^` or `$`, or use the [`regexp_full_match` function](#athena-duckdb-functions-string-regexp-full-match).

A pattern that can match an empty string, such as `(an)*`, matches every string.

### Syntax
<a name="athena-duckdb-functions-string-regexp-matches-syntax"></a>

```sql
regexp_matches({{string}}, {{regex}} [, {{options}} ])
```

### Arguments
<a name="athena-duckdb-functions-string-regexp-matches-arguments"></a>

The `regexp_matches` function takes the following arguments:

**{{string}}**  
The string to search, as a `VARCHAR` value.

**{{regex}}**  
The regular expression, as a `VARCHAR` value.

**{{options}}**  
The option letters, such as `'i'` for case-insensitive matching. It must be a constant `VARCHAR` value. This argument is optional.

### Return type
<a name="athena-duckdb-functions-string-regexp-matches-return-type"></a>

`BOOLEAN`

### Examples
<a name="athena-duckdb-functions-string-regexp-matches-examples"></a>

The following SQL query returns `true`, because `(an)*` can match an empty string, so it matches every string.

```sql
SELECT regexp_matches('anabanana', '(an)*') AS result
```

The query returns the following result.


| result | 
| --- | 
| `true` | 

The following SQL query compares an unanchored pattern with a pattern anchored to the start of the string.

```sql
SELECT
    regexp_matches('abc', 'b') AS contains_b,
    regexp_matches('abc', '^b') AS starts_with_b
```

The query returns the following result.


| contains\_b | starts\_with\_b | 
| --- | --- | 
| `true` | `false` | 

The following SQL query compares the default case-sensitive match with the `'i'` option.

```sql
SELECT
    regexp_matches('ABC', 'b') AS case_sensitive,
    regexp_matches('ABC', 'b', 'i') AS case_insensitive
```

The query returns the following result.


| case\_sensitive | case\_insensitive | 
| --- | --- | 
| `false` | `true` | 

### Considerations
<a name="athena-duckdb-functions-string-regexp-matches-considerations"></a>

Regular expressions use RE2 syntax, which does not support lookahead or lookbehind assertions such as `(?=...)`. A pattern that uses them fails. Rewrite the pattern without the assertion.

### Differences from Athena engine version 3
<a name="athena-duckdb-functions-string-regexp-matches-differences-from-engine-version-3"></a>

Athena engine version 3 tests for a match with `regexp_like`. DuckDB does not have `regexp_like`. Use `regexp_matches`, which also returns `true` for a match anywhere in the string.

In Athena engine version 3, a regular expression can use lookahead and lookbehind assertions, such as `(?=...)`. In DuckDB, regular expressions use RE2 syntax, which does not support them, so a query whose pattern uses them fails. Rewrite the pattern without the assertion.

## `regexp_replace` function
<a name="athena-duckdb-functions-string-regexp-replace"></a>

Returns `string` with the first match of the regular expression `regex` replaced by `replacement`. To replace every match, pass the `'g'` option. If there is no match, returns `string` unchanged.

In `replacement`, `\1` through `\9` insert the text of the matching capturing group, and `\0` inserts the whole match.

### Syntax
<a name="athena-duckdb-functions-string-regexp-replace-syntax"></a>

```sql
regexp_replace({{string}}, {{regex}}, {{replacement}} [, {{options}} ])
```

### Arguments
<a name="athena-duckdb-functions-string-regexp-replace-arguments"></a>

The `regexp_replace` function takes the following arguments:

**{{string}}**  
The string to search, as a `VARCHAR` value.

**{{regex}}**  
The regular expression, as a `VARCHAR` value.

**{{replacement}}**  
The text that replaces each match, as a `VARCHAR` value. It can contain group references such as `\1`.

**{{options}}**  
The option letters, such as `'g'` to replace every match and `'i'` for case-insensitive matching. It must be a constant `VARCHAR` value. You can combine letters, as in `'gi'`. This argument is optional.

### Return type
<a name="athena-duckdb-functions-string-regexp-replace-return-type"></a>

`VARCHAR`

### Examples
<a name="athena-duckdb-functions-string-regexp-replace-examples"></a>

The following SQL query replaces only the first match, because it does not pass the `'g'` option.

```sql
SELECT regexp_replace('hello', '[lo]', '-') AS result
```

The query returns the following result.


| result | 
| --- | 
| `he-lo` | 

The following SQL query passes the `'g'` option to replace every match.

```sql
SELECT
    regexp_replace('hello', '[lo]', '-', 'g') AS result
```

The query returns the following result.


| result | 
| --- | 
| `he---` | 

The following SQL query uses group references to reorder the parts of a date.

```sql
SELECT
    regexp_replace(
        '2021-03-04', '(\d+)-(\d+)-(\d+)', '\3/\2/\1'
    ) AS result
```

The query returns the following result.


| result | 
| --- | 
| `04/03/2021` | 

### Differences from Athena engine version 3
<a name="athena-duckdb-functions-string-regexp-replace-differences-from-engine-version-3"></a>

In Athena engine version 3, `regexp_replace` replaces every match. In DuckDB, it replaces only the first match unless you pass the `'g'` option. To get the Athena engine version 3 result, use `regexp_replace({{string}}, {{regex}}, {{replacement}}, 'g')`.

In Athena engine version 3, you write a group reference in `replacement` as `$1`. In DuckDB, write `\1` instead, because DuckDB inserts `$1` as literal text.

In Athena engine version 3, you can omit `replacement` to remove every match. DuckDB requires `replacement`. To remove every match, pass `''` and the `'g'` option.

## `regexp_split_to_table` function
<a name="athena-duckdb-functions-string-regexp-split-to-table"></a>

Splits `string` at each match of the regular expression `regex` and returns each part as a separate row. It is equivalent to `unnest(string_split_regex({{string}}, {{regex}}))`.

To get the parts as a list in one row, use the [`string_split_regex` function](#athena-duckdb-functions-string-string-split-regex).

### Syntax
<a name="athena-duckdb-functions-string-regexp-split-to-table-syntax"></a>

```sql
regexp_split_to_table({{string}}, {{regex}})
```

### Arguments
<a name="athena-duckdb-functions-string-regexp-split-to-table-arguments"></a>

The `regexp_split_to_table` function takes the following arguments:

**{{string}}**  
The string to split, as a `VARCHAR` value.

**{{regex}}**  
The regular expression that matches the separators, as a `VARCHAR` value.

### Return type
<a name="athena-duckdb-functions-string-regexp-split-to-table-return-type"></a>

`VARCHAR`, one row for each part.

### Examples
<a name="athena-duckdb-functions-string-regexp-split-to-table-examples"></a>

The following SQL query splits a string at each digit.

```sql
SELECT regexp_split_to_table('a1b2c', '[0-9]') AS part
ORDER BY part
```

The query returns the following results.


| part | 
| --- | 
| `a` | 
| `b` | 
| `c` | 

The following SQL query returns one row for each tag in a comma-separated column. Each row keeps the `id` of the row that it came from.

```sql
SELECT id, regexp_split_to_table(tags, ',') AS tag
FROM (VALUES
    (1, 'red,blue'),
    (2, 'green')
) AS t(id, tags)
ORDER BY id, tag
```

The query returns the following results.


| id | tag | 
| --- | --- | 
| `1` | `blue` | 
| `1` | `red` | 
| `2` | `green` | 

### Considerations
<a name="athena-duckdb-functions-string-regexp-split-to-table-considerations"></a>

Call `regexp_split_to_table` in the select list. It is not a table function, so you cannot call it in the `FROM` clause.

If `string` is `NULL`, `regexp_split_to_table` returns no rows, so the row that it came from does not appear in the result.

## `string_split_regex` function
<a name="athena-duckdb-functions-string-string-split-regex"></a>

Splits `string` at each match of the regular expression `regex` and returns the parts as a list. If a match is at the start or end of `string`, the list starts or ends with an empty element.

To return one row for each part, use the [`regexp_split_to_table` function](#athena-duckdb-functions-string-regexp-split-to-table).

`regexp_split_to_array` and `str_split_regex` are aliases of `string_split_regex`.

### Syntax
<a name="athena-duckdb-functions-string-string-split-regex-syntax"></a>

```sql
string_split_regex({{string}}, {{regex}} [, {{options}} ])
```

### Arguments
<a name="athena-duckdb-functions-string-string-split-regex-arguments"></a>

The `string_split_regex` function takes the following arguments:

**{{string}}**  
The string to split, as a `VARCHAR` value.

**{{regex}}**  
The regular expression that matches the separators, as a `VARCHAR` value.

**{{options}}**  
The option letters, such as `'i'` for case-insensitive matching. It must be a constant `VARCHAR` value. This argument is optional.

### Return type
<a name="athena-duckdb-functions-string-string-split-regex-return-type"></a>

`VARCHAR[]`

### Examples
<a name="athena-duckdb-functions-string-string-split-regex-examples"></a>

The following SQL query splits a string at each space that has an optional semicolon before it.

```sql
SELECT
    string_split_regex(
        'hello world; 42', ';? '
    ) AS result
```

The query returns the following result.


| result | 
| --- | 
| `[hello, world, 42]` | 

The following SQL query uses the alias `regexp_split_to_array` to split a string at each run of digits.

```sql
SELECT
    regexp_split_to_array('a1b22c', '[0-9]+') AS result
```

The query returns the following result.


| result | 
| --- | 
| `[a, b, c]` | 

The following SQL query counts the parts that each function returns for the separator `.` in the string `a.b`. As a regular expression, `.` matches any character, so `string_split_regex` splits at every character and returns four empty parts. The `string_split` function treats `.` as literal text, so it splits only at the period and returns two parts.

```sql
SELECT
    len(
        string_split_regex('a.b', '.')
    ) AS regex_part_count,
    len(string_split('a.b', '.')) AS literal_part_count
```

The query returns the following result.


| regex\_part\_count | literal\_part\_count | 
| --- | --- | 
| `4` | `2` | 

### Differences from Athena engine version 3
<a name="athena-duckdb-functions-string-string-split-regex-differences-from-engine-version-3"></a>

Athena engine version 3 splits a string at a regular expression with `regexp_split`. DuckDB does not have `regexp_split`. Use `string_split_regex` or its alias `regexp_split_to_array`.