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.
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.
- Function notation
- Method notation
extractRegexAll(value: string, pattern: regexp, group: number): array<string>
(value: string).extractRegexAll(pattern: regexp, group: number): array<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 | false | Which 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
- Function notation
- Method notation
create str from 'GET /api/users 200, POST /login 401, DELETE /a 204'
| create codes from extractRegexAll(str, /d+/)
create str from 'GET /api/users 200, POST /login 401, DELETE /a 204'
| create codes from str.extractRegexAll(/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
- Function notation
- Method notation
create str from 'user=alice id=42, user=bob, user=carol id=7'
| create ids from extractRegexAll(str, /user=w+(?: id=(d+))?/, 1)
create str from 'user=alice id=42, user=bob, user=carol id=7'
| create ids from str.extractRegexAll(/user=w+(?: id=(d+))?/, 1)
Example output
{
"ids": ["42", null, "7"]
}