Skip to main content

extractRegexAllGroups

Description

Return the positional capture groups of every match of a regular expression, as an array of arrays. A pattern that does not match returns an empty array.

Each element holds the groups of one match, in the order the groups appear in the pattern. Other behavior matches `extractRegexGroups`.

Note

The pattern can't contain named capture groups. A named group fails the query with a named capture group error.

Syntax

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

extractRegexAllGroups(value: string, pattern: regexp): array<array<string>>

Arguments

NameTypeRequiredDescription
valuestringtrueThe string to search
patternregexptrueThe regular expression to match. Capture groups can't be named. Must be a literal, not a dynamic expression

Example 1

Use case: Read every header name and value from one string

Each match contributes an inner array holding that match's groups.

Example query

create str from 'Server: nginx, Content-Type: text/html'
| create headers from extractRegexAllGroups(str, /([w-]+): ([^,]+)/)

Example output

{
"headers": [["Server", "nginx"], ["Content-Type", "text/html"]]
}

Example 2

Use case: Collect every pair of numbers in a string

A pattern that matches nothing returns an empty array rather than null, so the result stays an array you can keep working with.

Example query

create str from '1.2 and 3.4 and 5.6'
| create pairs from extractRegexAllGroups(str, /(d+).(d+)/)

Example output

{
"pairs": [["1", "2"], ["3", "4"], ["5", "6"]]
}
Last updated on
Was this page helpful?