---
title: extractRegex
description: |
  Return the first substring of a string that matches a regular expression, or
  `null` when the pattern does not match.

  The optional `group` argument selects what the match returns: `0`, the
  default, returns the whole match, a positive number returns that positional
  capture group, and a string returns the capture group with that name.

  !!! note
      Captured values are always strings. Use
      [`convert`](../../commands-reference/convert.mdx) when a value needs
      another datatype.
url: https://coralogix.com/docs/dataprime/language-reference/functions-reference/string/extractregex/
product: dataengine
feature: DataPrime Language
feature_id: dataprime-language
feature_identifier: dataengine/dataprime-language
last_updated: '2026-12-01'
---

> Documentation index: https://coralogix.com/docs/llms.txt

# `extractRegex`

## Description

Return the first substring of a string that matches a regular expression, or `null` when the pattern does not match.

The optional `group` argument selects what the match returns: `0`, the default, returns the whole match, a positive number returns that positional capture group, and a string returns the capture group with that name.

> **Note:**
>
> Captured values are always strings. Use [\`convert\`](https://coralogix.com/docs/dataprime/language-reference/commands-reference/convert.md) when a value needs another datatype.

## Syntax

Like many functions in DataPrime, `extractRegex` supports [two notations](https://coralogix.com/docs/dataprime/language-reference/functions-reference.md), **function** and **method**. These interchangeable forms allow flexibility in how you structure expressions.

**Function notation**

```dataprime
extractRegex(value: string, pattern: regexp, group: number | string): string
```

**Method notation**

```dataprime
(value: string).extractRegex(pattern: regexp, group: number | string): string
```

## Arguments

| Name      | Type               | Required | Description                                                                                                                                                             |
| --------- | ------------------ | -------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `value`   | `string`           | **true** | The string to search                                                                                                                                                    |
| `pattern` | `regexp`           | **true** | The regular expression to match. Must be a literal, not a dynamic expression                                                                                            |
| `group`   | `number \| string` | false    | Which part of the match to return: `0` (default) for the whole match, `N` for the N-th positional capture group, or a name for a named capture group. Must be a literal |

## Example 1

**Use case: Pull the status code out of a request line**

Without a `group` argument, the whole match is returned. A pattern that finds no match returns `null`, which is how you tell an empty match apart from no match at all.

### Example query

**Function notation**

```dataprime
create str from 'GET /api/users 200 OK'
| create status from extractRegex(str, /d+/)
```

**Method notation**

```dataprime
create str from 'GET /api/users 200 OK'
| create status from str.extractRegex(/d+/)
```

### Example output

```json
{
    "str": "GET /api/users 200 OK",
    "status": "200"
}
```

## Example 2

**Use case: Return one capture group instead of the whole match**

Pass a capture group name to return just that part. The same works positionally: `extractRegex(str, /name=(w+)/, 1)` returns `alice`, while group `0` returns `name=alice`.

### Example query

**Function notation**

```dataprime
create str from 'name=alice'
| create user from extractRegex(str, /name=(?<user>w+)/, 'user')
```

**Method notation**

```dataprime
create str from 'name=alice'
| create user from str.extractRegex(/name=(?<user>w+)/, 'user')
```

### Example output

```json
{
    "str": "name=alice",
    "user": "alice"
}
```
