# `REGEXP_SUBSTR`

**Applies to:** ✅ Data 360 SQL ✅ Tableau Hyper API

Returns the substring matching a regular expression pattern.

## Syntax

```sql
regexp_substr(<string>, <pattern>)
```

## Arguments

### Required

- `<string>`: The string to search within.
- `<pattern>`: The regular expression pattern to match.

## Returns

Returns a text value containing the matched substring, or `NULL` if no match is found.

## Considerations

- Returns the first match when multiple matches exist.
- For detailed regex syntax, see [Regular Expression Syntax](/docs/data/data-cloud-query-guide/references/dc-sql-reference/string-matching.md#regular-expression-syntax).

## Examples

### Extract Digits

Extract a sequence of digits from a string.

```sql
SELECT regexp_substr('abc123def', '\d+') AS result;
```

Returns `'123'`.

### No Match

Returns NULL when pattern is not found.

```sql
SELECT regexp_substr('abcdef', '\d+') AS result;
```

Returns `NULL`.

### Extract Email Domain

Extract the domain from an email address.

```sql
SELECT regexp_substr('user@example.com', '@(.+)$') AS result;
```

Returns `'@example.com'`.

### Extract Words

Extract a word pattern.

```sql
SELECT regexp_substr('Price: $99.99', '\$\d+\.\d+') AS result;
```

Returns `'$99.99'`, because the digit string is considered a word.

### Use with COALESCE

Provide a default when no match.

```sql
SELECT coalesce(regexp_substr(text, '\d+'), '0') AS number
FROM data;
```

Returns the matched digits or '0' if none are found.

## Related Documentation

- [SUBSTRING](/docs/data/data-cloud-query-guide/references/dc-sql-reference/substring.md)
- [REGEXP_INSTR](/docs/data/data-cloud-query-guide/references/dc-sql-reference/regexp-instr.md)
- [REGEXP_REPLACE](/docs/data/data-cloud-query-guide/references/dc-sql-reference/regexp-replace.md)
- [Regular Expression Syntax](/docs/data/data-cloud-query-guide/references/dc-sql-reference/string-matching.md#regular-expression-syntax)
