- 2 minutes to read

Formula - regexgroup

The regexGroup Formula function returns the value of a capturing group of a regular expression, instead of the whole match that regex returns.

Syntax

regexGroup('expression', 'groups', content)

Parameters

Parameter Required Description
'expression' Yes A .NET regular expression with at least one capturing group ( ), in single quotes. Backslashes are written once ('\d+').
'groups' Yes The number of the capturing group to return, such as '1' for the first group. The screenshots show both '1' and 1.
content Yes The text to search: body, context, or the result of another function (see Formula language rules).

Returns

The value of the selected group for every match, one value each; duplicates are collapsed. When a group repeats inside one match (for example (…){4}), the value from its last repetition is returned. When nothing matches, nothing is returned.

Examples

Example 1: Read the order IDs from the body

Input

ID=123,CUSTOMER=1
ID=456,CUSTOMER=2

Formula

regexGroup('ID=(\d+)', '1', body)

Result

123
456

Formula regexGroup expression returning two ID values from a text message body in the Test Expression tab
Group 1 captures only the digits after ID=, so the ID= prefix is not part of the result.

Example 2: Read the fourth field of a pipe-delimited context value

Input

The Message Context key OrderRecord holds a pipe-delimited order record:

Key Value
OrderRecord ORD|101|ExampleCompany1|456|Company Name ACME |Dieselgate valley 1|123 45|Flameburg|

Formula

regexGroup('^(?:(.*?)\|){4}', 1, context('OrderRecord'))

Result

456

Formula regexGroup expression returning the fourth pipe-delimited field from the OrderRecord context property
(?:(.*?)\|){4} repeats four times from the start of the value; group 1 keeps the last repetition, the fourth field (counted from 1), 456.

Notes

  • \| matches a literal pipe; an unescaped | means "or" in a regular expression.
  • To return the whole match instead of a group, use regex.
  • For one pattern on the body, the RegEx with capturing groups plugin returns groups without a formula, and accepts several group numbers or names. See Choose a plugin.

Next Steps