extractRegexGroups
Description
Return the positional capture groups of the first match of a regular expression, as an array of strings. A pattern that does not match returns null.
Capture groups that are optional and did not take part in the match are returned as null.
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, extractRegexGroups supports two notations, function and method. These interchangeable forms allow flexibility in how you structure expressions.
- Function notation
- Method notation
extractRegexGroups(value: string, pattern: regexp): array<string>
(value: string).extractRegexGroups(pattern: regexp): 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: Split an IP address into its octets
Groups are numbered by the order of their opening parenthesis, and the array follows that order.
Example query
- Function notation
- Method notation
create str from '192.168.1.1'
| create octets from extractRegexGroups(str, /(d+).(d+).(d+).(d+)/)
create str from '192.168.1.1'
| create octets from str.extractRegexGroups(/(d+).(d+).(d+).(d+)/)
Example output
{
"octets": ["192", "168", "1", "1"]
}
Example 2
Use case: Read a pair of values out of one field
Every captured value is a string, so numeric captures need `convert` before arithmetic.
Example query
- Function notation
- Method notation
create str from 'name=alice age=30'
| create parts from extractRegexGroups(str, /name=(w+) age=(w+)/)
create str from 'name=alice age=30'
| create parts from str.extractRegexGroups(/name=(w+) age=(w+)/)
Example output
{
"parts": ["alice", "30"]
}