- 3 minutes to read

Formula - replaceregex

The replaceregex Formula function returns the content with every match of a regular expression replaced by newValue.

Syntax

replaceregex('pattern', 'newValue', content)

Parameters

Parameter Required Description
'pattern' Yes A .NET regular expression. Write backslashes once, for example '\d+'; see the Formula language rules.
'newValue' Yes The text to substitute for each match. $1, $2, and so on insert the capturing groups of the match (Example 4).
content Yes The text to change: body, context, or the result of another function, such as xPath2.

Returns

The content with all matches replaced. Content without a match is returned unchanged. When the content holds several values, the replacement runs on each value.

Examples

Example 1: Mask every digit

Input

<ProjectNumber>1401518</ProjectNumber>

Formula

replaceregex('[0-9]', 'X', body)

Result

<ProjectNumber>XXXXXXX</ProjectNumber>

Test Expression tab: replaceregex with the digit class 0-9 and X on the body turns ProjectNumber 1401518 into XXXXXXX
Each digit is a separate match, so all seven digits are replaced one by one.

Example 2: Replace a fixed word

Input

<Action>DELETE</Action>

Formula

replaceregex('DELETE', 'UPDATE', body)

Result

<Action>UPDATE</Action>

Test Expression tab: replaceregex('DELETE', 'UPDATE', body) changes the Action element from DELETE to UPDATE
A pattern without special characters matches the word as is; replace gives the same result without regular expression syntax.

Example 3: Mask an element value

Input

<Company>400</Company>

Formula

replaceregex('<Company>[0-9]+</Company>', '<Company>XXX</Company>', body)

Result

<Company>XXX</Company>

Test Expression tab: replaceregex masks the Company value 400 as XXX and keeps the element tags
The pattern matches the whole element, so newValue writes the tags back around the mask.

Example 4: Build one value per record with capturing groups

xPath2 returns each inner TypedPollingResultSet0 record. The pattern captures Company, Activity, and Action, and '$1|$2|$3' replaces the whole record with the three captured values.

Input

<TypedPolling>
    <TypedPollingResultSet0>
        <TypedPollingResultSet0>
            <ProjectNumber>1401518</ProjectNumber>
            <Company>400</Company>
            <Activity>1000</Activity>
            <Version>20250301060028</Version>
            <Action>DELETE</Action>
        </TypedPollingResultSet0>
        <TypedPollingResultSet0>
            <ProjectNumber>1401518</ProjectNumber>
            <Company>400</Company>
            <Activity>1001</Activity>
            <Version>20250301060028</Version>
            <Action>DELETE</Action>
        </TypedPollingResultSet0>
        <TypedPollingResultSet0>
            <ProjectNumber>4831351</ProjectNumber>
            <Company>355</Company>
            <Activity>1063</Activity>
            <Version>20250301060028</Version>
            <Action>DELETE</Action>
        </TypedPollingResultSet0>
        <TypedPollingResultSet0>
            <ProjectNumber>9233120</ProjectNumber>
            <Company>700</Company>
            <Activity>3400</Activity>
            <Version>20250301060028</Version>
            <Action>DELETE</Action>
        </TypedPollingResultSet0>
        <TypedPollingResultSet0>
            <ProjectNumber>9402000</ProjectNumber>
            <Company>734</Company>
            <Activity>5104</Activity>
            <Version>20250301060028</Version>
            <Action>DELETE</Action>
        </TypedPollingResultSet0>
    </TypedPollingResultSet0>
</TypedPolling>

Formula

replaceregex('.+<Company>(.+)</Company>.+<Activity>(.+)</Activity>.+<Action>(.+)</Action>','$1|$2|$3',xPath2('/TypedPolling/TypedPollingResultSet0/TypedPollingResultSet0',body))

Result

400|1000|DELETE
400|1001|DELETE
355|1063|DELETE
700|3400|DELETE
734|5104|DELETE

Test Expression tab: replaceregex with capturing groups on xPath2 records returns five values such as 400|1000|DELETE
Five records give five unique values, each built from the Company, Activity, and Action of one record.

Notes

  • To replace plain text without regular expression syntax, use replace; it treats every character literally.
  • To extract matches instead of replacing them, use regex or regexGroup.

Next Steps