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

# `regexp_instr` 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 position of the first substring in `str` that matches `regexp`.

## Syntax

```
regexp_instr( str, regexp )
```

## Arguments

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

## Returns

An `INTEGER`.

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 either argument is `NULL`, the result is `NULL`. If the pattern is not found, the result is `0`.

## Common error conditions

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

## Examples

### Find the position of the first match

The pattern `Ste(v|ph)en` matches `Steven` at the start of the string, so the position is 1.

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

### Find the position of the first digit

Positions are 1-based.

```sql
> SELECT regexp_instr('Order 1234 shipped', r'\d');
 7
```

### Find the position of a character

```sql
> SELECT regexp_instr('alice@example.com', r'@');
 6
```

### No match returns 0

```sql
> SELECT regexp_instr('Mary had a little lamb', 'Ste(v|ph)en');
 0
```

### `NULL` input returns `NULL`

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

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

### Invalid regex pattern

```sql
> SELECT regexp_instr('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_replace` function](https://docs.databricks.com/aws/en/sql/language-manual/functions/regexp_replace.md)
* [`regexp_substr` function](https://docs.databricks.com/aws/en/sql/language-manual/functions/regexp_substr.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)
