Skip to main content

extractRegexAll

Description

Return every substring of a string that matches a regular expression, as an array. A pattern that does not match returns an empty array.

The optional group argument works as it does in `extractRegex`: 0, the default, returns the whole match, and a positive number returns that positional capture group. Unlike extractRegex, it doesn't take a capture group name.

Note

The array has one slot per match. When the requested group is optional and did not take part in a match, its slot is null rather than skipped, so the positions stay aligned with the matches.

Syntax

Like many functions in DataPrime, extractRegexAll supports two notations, function and method. These interchangeable forms allow flexibility in how you structure expressions.

extractRegexAll(value: string, pattern: regexp, group: number): array<string>

Arguments

NameTypeRequiredDescription
valuestringtrueThe string to search
patternregexptrueThe regular expression to match. Must be a literal, not a dynamic expression
groupnumberfalseWhich part of each match to return: 0 (default) for the whole match, or N for the N-th positional capture group. Must be a literal

Example 1

Use case: Collect every status code in a request log line

Each match contributes one element, in the order the matches occur.

Example query

create str from 'GET /api/users 200, POST /login 401, DELETE /a 204'
| create codes from extractRegexAll(str, /d+/)

Example output

{
"codes": ["200", "401", "204"]
}

Example 2

Use case: Collect one capture group from every match

Asking for group 1 returns that group from each match. Where the optional group did not participate, the slot holds null.

Example query

create str from 'user=alice id=42, user=bob, user=carol id=7'
| create ids from extractRegexAll(str, /user=w+(?: id=(d+))?/, 1)

Example output

{
"ids": ["42", null, "7"]
}
Last updated on
Was this page helpful?