Skip to main content

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.

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, extractRegexGroups supports two notations, function and method. These interchangeable forms allow flexibility in how you structure expressions.

extractRegexGroups(value: string, pattern: regexp): 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: 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

create str from '192.168.1.1'
| create octets from extractRegexGroups(str, /(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

create str from 'name=alice age=30'
| create parts from extractRegexGroups(str, /name=(w+) age=(w+)/)

Example output

{
"parts": ["alice", "30"]
}
Last updated on
Was this page helpful?