> ## Documentation Index
> Fetch the complete documentation index at: /documents/llms.txt
> Use this file to discover all available pages before exploring further.

# regexp_count

> Counts the number of times a pattern occurs in a string.

Counts the number of times a pattern occurs in a string. It returns the number of occurrences of the regular expression pattern in the target string.

## Syntax

```Haskell theme={null}
INT regexp_count(VARCHAR str, VARCHAR pattern)
```

## Parameters

* `str`: The string to search in. Supported data type is VARCHAR. If the input is NULL, NULL is returned.

* `pattern`: The regular expression pattern to search for. Supported data type is VARCHAR. If the pattern is NULL, NULL is returned.

## Return value

Returns an INT representing the count of occurrences. Returns 0 if no matches are found or if the input string is empty.

## Examples

***

***

**Tip**

This example uses the Local Climatological Data(LCD) dataset featured in the [the database Basics](/documents/4.2/quick_start/shared-nothing.md) Quick Start. You can load the data and try the example yourself.

```SQL theme={null}
-- Count occurrences of digits
SELECT regexp_count('abc123def456', '[0-9]');
```

```plaintext theme={null}
+---------------------------------------+
| regexp_count('abc123def456', '[0-9]') |
+---------------------------------------+
|                                     6 |
+---------------------------------------+
```

```SQL theme={null}
-- Count occurrences of dots
SELECT regexp_count('test.com test.net test.org', '\\.');
```

```plaintext theme={null}
+---------------------------------------------------+
| regexp_count('test.com test.net test.org', '\\.') |
+---------------------------------------------------+
|                                                 3 |
+---------------------------------------------------+
```

```SQL theme={null}
-- Count occurrences of whitespace sequences
SELECT regexp_count('a b  c   d', '\\s+');
```

```plaintext theme={null}
+----------------------------------------+
| regexp_count('a b  c   d', '\\s+')     |
+----------------------------------------+
|                                      3 |
+----------------------------------------+
```

```SQL theme={null}
-- Count occurrences of a repeated pattern
SELECT regexp_count('ababababab', 'ab');
```

```plaintext theme={null}
+------------------------------------+
| regexp_count('ababababab', 'ab')   |
+------------------------------------+
|                                  5 |
+------------------------------------+
```

```SQL theme={null}
-- Using with NULL and empty values
SELECT 
  regexp_count('', '.') AS empty_str,
  regexp_count(NULL, '.') AS null_str,
  regexp_count('abc', NULL) AS null_pattern;
```

```plaintext theme={null}
+------------+----------+--------------+
| empty_str  | null_str | null_pattern |
+------------+----------+--------------+
|          0 |     NULL |         NULL |
+------------+----------+--------------+
```

```SQL theme={null}
-- Count occurrence of Unicode/Chinese characters
SELECT regexp_count('abc中文def', '[\\p{Han}]+');
```

```plaintext theme={null}
+-----------------------------------------------+
| regexp_count('abc中文def', '[\\p{Han}]+')     |
+-----------------------------------------------+
|                                             1 |
+-----------------------------------------------+
```

### With table data

```SQL theme={null}
CREATE TABLE sample_text (
  str VARCHAR(65533) NULL,
  regex VARCHAR(65533) NULL
);

INSERT INTO sample_text VALUES 
  ('abc123def456', '[0-9]'), 
  ('test.com test.net test.org', '\\.'), 
  ('a b  c   d', '\\s+'), 
  ('ababababab', 'ab');

SELECT str, regex, regexp_count(str, regex) AS count 
FROM sample_text 
ORDER BY str;
```

```plaintext theme={null}
+-----------------------------+------+-------+
| str                         | regex| count |
+-----------------------------+------+-------+
| a b  c   d                  | \s+  |     3 |
| abc123def456                | [0-9]|     6 |
| ababababab                  | ab   |     5 |
| test.com test.net test.org  | \.   |     3 |
+-----------------------------+------+-------+
```

## keyword

REGEXP\_COUNT,REGEXP,COUNT


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.