---
title: regexp_extract function
description: Learn the syntax of the regexp\_extract function of the SQL language in Databricks SQL and Databricks Runtime.
canonical_url: https://docs.databricks.com/aws/en/sql/language-manual/functions/regexp_extract
---

# `regexp_extract` function

**Applies to:** ![check marked yes](https://docs.databricks.com/aws/en/images/icons/check.png) Databricks SQL ![check marked yes](https://docs.databricks.com/aws/en/images/icons/check.png) Databricks Runtime

Extracts the first string in `str` that matches the `regexp` expression and corresponds to the `regex` group index.

## Syntax

```
regexp_extract(str, regexp [, idx] )
```

## Arguments

* `str`: A `STRING` expression to be matched.
* `regexp`: A `STRING` expression with a matching pattern.
* `idx`: An optional integral number expression greater or equal 0 with default 1.

## Returns

A `STRING`.

If `str`, `regexp`, or `idx` is `NULL`, the result is `NULL`.
If `regexp` does not match any part of `str`, the result is an empty string.
If the overall pattern matches but the group specified by `idx` does not participate in the match, the result is an empty string.

The `regexp` string must be a regular expression. See [Regular expressions](https://docs.databricks.com/aws/en/sql/language-manual/sql-ref-regexp-pattern.md) for the supported syntax.

When using literals, use [`raw-literal` (`r` prefix)](https://docs.databricks.com/aws/en/sql/language-manual/data-types/string-type.md) to avoid escape character pre-processing.

`regexp` may contain multiple groups. Capture groups are defined by enclosing part of the pattern in parentheses `(...)`.
`idx` indicates which regex group to extract.
An `idx` of 0 means matching the entire regular expression.
The default value of `idx` is 1, meaning the first capture group is returned when `idx` is omitted.

If `idx` is greater than the number of capture groups defined in `regexp`, Databricks raises an error.

Only the first match of `regexp` in `str` is considered. To extract all matches, use [regexp\_extract\_all](https://docs.databricks.com/aws/en/sql/language-manual/functions/regexp_extract_all.md).

## Common error conditions

* [INVALID\_PARAMETER\_VALUE.PATTERN](https://docs.databricks.com/aws/en/error-messages/invalid-parameter-value-error-class.md#pattern)
* [INVALID\_PARAMETER\_VALUE.REGEX\_GROUP\_INDEX](https://docs.databricks.com/aws/en/error-messages/invalid-parameter-value-error-class.md#regex_group_index)

## Examples

### Extract specific capture groups

```sql
> SELECT regexp_extract('100-200', '(\\d+)-(\\d+)', 1);
 100

> SELECT regexp_extract('100-200', '(\\d+)-(\\d+)', 2);
 200
```

### Use idx 0 to return the entire match

```sql
> SELECT regexp_extract('Order: 100-200', '(\\d+)-(\\d+)', 0);
 100-200
```

### Default idx is 1

When `idx` is omitted, the first capture group is returned.

```sql
> SELECT regexp_extract('abc123def', '([a-z]+)(\\d+)');
 abc
```

### No match returns an empty string

```sql
> SELECT regexp_extract('hello world', '(\\d+)', 1);

```

### NULL input returns NULL

```sql
> SELECT regexp_extract(NULL, '(\\d+)', 1);
 NULL
```

### Use raw string literals to avoid double escaping

In a regular SQL string, backslashes must be doubled (`\\d` to mean `\d`). Use the `r` prefix for raw string literals to write patterns more naturally.

```sql
> SELECT regexp_extract('abc123', r'(\d+)', 1);
 123
```

### Extract a domain from a URL

```sql
> SELECT regexp_extract('https://www.databricks.com/product', r'https?://([^/]+)', 1);
 www.databricks.com
```

### Extract a component from a date string

```sql
> SELECT regexp_extract('Event on 2024-03-15', r'(\d{4})-(\d{2})-(\d{2})', 0);
 2024-03-15

> SELECT regexp_extract('Event on 2024-03-15', r'(\d{4})-(\d{2})-(\d{2})', 2);
 03
```

### Optional capture group that does not participate in the match

When a group is optional and does not match, an empty string is returned for that group.

```sql
> SELECT regexp_extract('ac', r'(a)(b)?(c)', 2);

```

### Invalid regex pattern

```sql
> SELECT regexp_extract('abc', '[invalid', 0);
  Error: INVALID_PARAMETER_VALUE.PATTERN
```

## Related functions

* [Regular expressions](https://docs.databricks.com/aws/en/sql/language-manual/sql-ref-regexp-pattern.md)
* [`like` operator](https://docs.databricks.com/aws/en/sql/language-manual/functions/like.md)
* [`regexp_extract_all` function](https://docs.databricks.com/aws/en/sql/language-manual/functions/regexp_extract_all.md)
* [`regexp_replace` function](https://docs.databricks.com/aws/en/sql/language-manual/functions/regexp_replace.md)
* [`split` function](https://docs.databricks.com/aws/en/sql/language-manual/functions/split.md)
* [`regexp` operator](https://docs.databricks.com/aws/en/sql/language-manual/functions/regexp.md)
* [`rlike` operator](https://docs.databricks.com/aws/en/sql/language-manual/functions/rlike.md)
