---
title: "Formula - regex: Extract Values with Regular Expressions"
description: The regex Formula function returns every whole match of a .NET regular expression in text content. Includes syntax, parameters, and examples from the message body, a context property, and several context properties.
---

# :fa-function: Formula - regex

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

![Find the ERP reference with regex, then strip its prefix with replaceregex, animated walkthrough (7.12)][39]  
*Animated walkthrough: on the Order Entry XML of the [Order-to-Cash demo][], `regex` returns the ERP reference `ERP-2025-10005`. Nested in [replaceregex][], the `ERP-2025-` prefix is removed and `10005` remains.*  

## :fa-code: Syntax

```text
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][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][Context] and [contextKey][], where it selects context keys by pattern.

## :fa-list-ol: Examples

### Example 1: Find a number in the body

#### Input

```text
This is number 5
```

#### Formula

```text
regex('\d+', body)
```

#### Result

```text
5
```

![Formula regex expression returning the number 5 from a text message body in the Test Expression tab (7.12)][36]  
*`\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

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

#### Result

```text
GMT
```

![Formula regex expression returning GMT from the id context property in the Message Context tab (7.12)][37]  
*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

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

#### Result

```text
UTC
CTO
```

![Formula regex over two context properties returning UTC and CTO in the Test Expression tab (7.12)][38]  
*`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

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

#### Formula

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

#### Result

```text
INV-2024-0042
```

![Formula regex returning the invoice number INV-2024-0042 from a text message body in the Test Expression tab (7.12)][ex4]  
*Only the `INV-` pattern matches; the order number `PO-1001` and the due date are left out.*  

## :fa-circle-info: 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][].

## :fa-hand-point-right: Next Steps

* :fa-magnifying-glass-plus: [Add or manage Search Field][]  
* :fa-flask: [Test Your Expression][]  
* :fa-hard-drive: [Add or manage Log View][]  

## :fa-link-horizontal: Related Topics

* :fa-filter: [Extract functions][Extract Group]  
* :fa-file-csv: [csv][]  
* :fa-sitemap: [jsonPath][]  
* :fa-key: [jsonPathKey][]  
* :fa-layer-group: [regexGroup][]  
* :fa-database: [sqllookup][]  
* :fa-code: [xPath][]  
* :fa-code: [xPath2][]  
* :fa-function: [Formula][]  
* :fa-scroll: [Formula language rules][]  
* :fa-puzzle-piece: [Search Field Expressions][]  
* :fa-magnifying-glass: [RegEx plugin][]  
* :fa-magnifying-glass: [RegEx on Message Context plugin][]  

[36]:{ndm}/Documentation%20Pictures/IM/WebClient/Admin/Log/Search%20Fields/Plugins/Formula/Extract/regex_simpleParameter_body.png
[37]:{ndm}/Documentation%20Pictures/IM/WebClient/Admin/Log/Search%20Fields/Plugins/Formula/Extract/regex_simpleParameter_messageContext.png
[38]:{ndm}/Documentation%20Pictures/IM/WebClient/Admin/Log/Search%20Fields/Plugins/Formula/Extract/regex_nestingFunction_multipleKeysMessageContext.png
[39]:{ndm}/Documentation%20Pictures/IM/WebClient/Admin/Log/Search%20Fields/Plugins/Formula/Formula_RegexReplace.gif

[Add or manage Log View]:{ndr}/Administration/1.%20Log%20Management/1.%20Log%20Views/Features/Add%20or%20Manage%20Log%20View.md
[Add or manage Search Field]:{ndr}/Administration/1.%20Log%20Management/2.%20Search%20Fields/Features/Add%20or%20manage%20Search%20Field.md
[body]:{ndr}/Administration/1.%20Log%20Management/2.%20Search%20Fields/Features/Search%20Field%20Expressions/Formula/Source/body.md
[Choose a plugin]:{ndr}/Administration/1.%20Log%20Management/2.%20Search%20Fields/Features/Search%20Field%20Expressions/Overview.md#fa-signs-post-choose-a-plugin
[Context]:{ndr}/Administration/1.%20Log%20Management/2.%20Search%20Fields/Features/Search%20Field%20Expressions/Formula/Source/context.md
[contextKey]:{ndr}/Administration/1.%20Log%20Management/2.%20Search%20Fields/Features/Search%20Field%20Expressions/Formula/Source/contextKey.md
[csv]:{ndr}/Administration/1.%20Log%20Management/2.%20Search%20Fields/Features/Search%20Field%20Expressions/Formula/Extract/csv.md
[ex4]:{ndm}/Documentation%20Pictures/IM/WebClient/Admin/Log/Search%20Fields/Plugins/Formula/Extract/regex_example4.png
[Extract Group]:{ndr}/Administration/1.%20Log%20Management/2.%20Search%20Fields/Features/Search%20Field%20Expressions/Formula/Overview.md#extract
[Formula]:{ndr}/Administration/1.%20Log%20Management/2.%20Search%20Fields/Features/Search%20Field%20Expressions/Formula/Overview.md
[Formula language rules]:{ndr}/Administration/1.%20Log%20Management/2.%20Search%20Fields/Features/Search%20Field%20Expressions/Formula/Overview.md#fa-scroll-formula-language-rules
[jsonPath]:{ndr}/Administration/1.%20Log%20Management/2.%20Search%20Fields/Features/Search%20Field%20Expressions/Formula/Extract/jsonPath.md
[jsonPathKey]:{ndr}/Administration/1.%20Log%20Management/2.%20Search%20Fields/Features/Search%20Field%20Expressions/Formula/Extract/JsonPathKey.md
[Order-to-Cash demo]:{ndr}/Repository%20Model/BPMs/Examples/Order-to-Cash/Generate-O2C-Demo-Data.md
[RegEx on Message Context plugin]:{ndr}/Administration/1.%20Log%20Management/2.%20Search%20Fields/Features/Search%20Field%20Expressions/RegEx%20on%20Message%20Context.md
[RegEx plugin]:{ndr}/Administration/1.%20Log%20Management/2.%20Search%20Fields/Features/Search%20Field%20Expressions/RegEx.md
[regexGroup]:{ndr}/Administration/1.%20Log%20Management/2.%20Search%20Fields/Features/Search%20Field%20Expressions/Formula/Extract/regexGroup.md
[replaceregex]:{ndr}/Administration/1.%20Log%20Management/2.%20Search%20Fields/Features/Search%20Field%20Expressions/Formula/Alter/replaceregex.md
[Search Field Expressions]:{ndr}/Administration/1.%20Log%20Management/2.%20Search%20Fields/Features/Search%20Field%20Expressions/Overview.md
[sqllookup]:{ndr}/Administration/1.%20Log%20Management/2.%20Search%20Fields/Features/Search%20Field%20Expressions/Formula/Extract/sqllookup.md
[Test Your Expression]:{ndr}/Administration/1.%20Log%20Management/2.%20Search%20Fields/Features/Add%20or%20manage%20Search%20Field.md#test-your-expression
[xPath]:{ndr}/Administration/1.%20Log%20Management/2.%20Search%20Fields/Features/Search%20Field%20Expressions/Formula/Extract/xPath.md
[xPath2]:{ndr}/Administration/1.%20Log%20Management/2.%20Search%20Fields/Features/Search%20Field%20Expressions/Formula/Extract/xPath2.md