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

# `regexp_substr` 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 11.3 LTS and above

Returns the first substring in `str` that matches `regexp`.

## Syntax

```
regexp_substr( str, regexp )
```

## Arguments

* `str`: A `STRING` expression to be matched.
* `regexp`: A `STRING` expression with a matching pattern.

## Returns

A `STRING`.

`regexp_substr` returns the entire substring that `regexp` matches, not an individual capture group.
To return a specific capture group, use [regexp\_extract](https://docs.databricks.com/aws/en/sql/language-manual/functions/regexp_extract.md).
Only the first match is considered. To return every match, use [regexp\_extract\_all](https://docs.databricks.com/aws/en/sql/language-manual/functions/regexp_extract_all.md).

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.

If `str` or `regexp` is `NULL`, or the pattern is not found, the result is `NULL`.

## Common error conditions

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

## Examples

### Match one of several alternatives

The pattern `Ste(v\|ph)en` matches either `Steven` or `Stephen`. The function returns the first match.

```sql
> SELECT regexp_substr('Steven Jones and Stephen Smith are the best players', r'Ste(v|ph)en');
 Steven
```

### Extract the first number from a string

Use a raw literal so that `\d` is passed to the regex engine without doubling the backslash.

```sql
> SELECT regexp_substr('item 42 costs $19', r'\d+');
 42
```

### Match case-insensitively

Use the `(?i)` inline flag to ignore case.

```sql
> SELECT regexp_substr('Error: disk full', r'(?i)error');
 Error
```

### Extract an email address

```sql
> SELECT regexp_substr('Contact alice@example.com for details', r'\w+@\w+\.\w+');
 alice@example.com
```

### Extract the first hashtag

`regexp_substr` returns only the first match. To return every match, use [regexp\_extract\_all](https://docs.databricks.com/aws/en/sql/language-manual/functions/regexp_extract_all.md).

```sql
> SELECT regexp_substr('Posts tagged #data #ai #sql', r'#\w+');
 #data
```

### No match returns `NULL`

Unlike [regexp\_extract](https://docs.databricks.com/aws/en/sql/language-manual/functions/regexp_extract.md), which returns an empty string, `regexp_substr` returns `NULL` when the pattern is not found.

```sql
> SELECT regexp_substr('Mary had a little lamb', r'Ste(v|ph)en');
 NULL
```

### `NULL` input returns `NULL`

```sql
> SELECT regexp_substr(NULL, r'Ste(v|ph)en');
 NULL

> SELECT regexp_substr('Mary had a little lamb', NULL);
 NULL
```

### Invalid regex pattern

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

## Related functions

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