- 2 minutes to read

Formula - regex

The regex Formula function returns every whole match of a regular expression in text content.

Syntax

regex('expression', content)

Parameters

Parameter Required Description
'expression' Yes A .NET regular expression in single quotes. Backslashes are written once ('\d+'); a literal ' is written ''.
content Yes The text to search: body, context, or the result of another function (see Formula language rules).

Returns

Every whole match, one value each; duplicates are collapsed. Capturing groups in the pattern don't change the result: regex always returns the whole match. To return a group instead, use regexGroup. When nothing matches, nothing is returned.

regex(...) is also the argument form of context and contextKey, where it selects context keys by pattern.

Examples

Example 1: Find a number in the body

Input

This is number 5

Formula

regex('\d+', body)

Result

5

Formula regex expression returning the number 5 from a text message body in the Test Expression tab
\d+ matches the run of digits; the backslash is written once.

Example 2: Match a value in a context property

Input

Key Value
id GMT

Formula

regex('GMT|UTC', context('id'))

Result

GMT

Formula regex expression returning GMT from the id context property in the Message Context tab
The alternation GMT|UTC matches GMT in the value of id.

Example 3: Match across several context properties

Input

Key Value
id GMT UTC
id2 GMT CTO

Formula

regex('UTC|CTO', context(regex('id.*')))

Result

UTC
CTO

Formula regex over two context properties returning UTC and CTO in the Test Expression tab
context(regex('id.*')) returns both values; regex searches each and returns one match from each.

Example 4: Pick the invoice number from a text message

Input

Invoice INV-2024-0042 for order PO-1001 is due 2024-11-30

Formula

regex('INV-\d{4}-\d{4}', body)

Result

INV-2024-0042

Formula regex returning the invoice number INV-2024-0042 from a text message body in the Test Expression tab
Only the INV- pattern matches; the order number PO-1001 and the due date are left out.

Notes

  • Patterns are not anchored; add ^ and $ when the whole value must match.
  • Formula regexes ignore unescaped whitespace and treat # as the start of a comment: write a literal space as \s or [ ] and a literal # as \# or [#]. Matching is case-insensitive. See the Formula language rules.
  • For large text bodies, keep the pattern specific so the engine doesn't scan more than needed.
  • For one pattern on the body, the RegEx plugin does the same without a formula; for context values, use the RegEx on Message Context plugin. See Choose a plugin.

Next Steps