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`.
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.
- Function notation
- Method notation
extractRegexAllGroups(value: string, pattern: regexp): array<array<string>>
(value: string).extractRegexAllGroups(pattern: regexp): array<array<string>>
Arguments
| Name | Type | Required | Description |
|---|---|---|---|
value | string | true | The string to search |
pattern | regexp | true | The 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
- Function notation
- Method notation
create str from 'Server: nginx, Content-Type: text/html'
| create headers from extractRegexAllGroups(str, /([w-]+): ([^,]+)/)
create str from 'Server: nginx, Content-Type: text/html'
| create headers from str.extractRegexAllGroups(/([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
- Function notation
- Method notation
create str from '1.2 and 3.4 and 5.6'
| create pairs from extractRegexAllGroups(str, /(d+).(d+)/)
create str from '1.2 and 3.4 and 5.6'
| create pairs from str.extractRegexAllGroups(/(d+).(d+)/)
Example output
{
"pairs": [["1", "2"], ["3", "4"], ["5", "6"]]
}