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

# `regexp_count` 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 number of times `str` matches the `regexp` pattern.

## Syntax

```
regexp_count( 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`.

## Common error conditions

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

## Examples

### Count occurrences of a pattern

The pattern `Ste(v|ph)en` matches both `Steven` and `Stephen`.

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

### Count the numbers in a string

```sql
> SELECT regexp_count('There are 10 cats, 20 dogs, and 30 birds', r'\d+');
 3
```

### Count the words in a string

```sql
> SELECT regexp_count('one two three four', r'\w+');
 4
```

### Count case-insensitively

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

```sql
> SELECT regexp_count('Yes yes YES', r'(?i)yes');
 3
```

### No match returns 0

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

### `NULL` input returns `NULL`

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

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

### Invalid regex pattern

```sql
> SELECT regexp_count('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_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)
* [`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)
