REGEX_REPLACE

Use the REGEX_REPLACE script function to replace text in a string according to a regular expression and a replacement string.

Syntax

REGEX_REPLACE (String, Regex, Replacement String [, Flags])

Parameters

Parameter Description Format Allowed Values
String Unicode character string to search Script literal or script variable n.a.
Regex Regular expression to use. The regular expression uses the Boost C++ Libraries syntax. For more information, see the official Boost C++ Libraries documentation. Script literal or script variable n.a.
Replacement String Replacement rule to use. For example, in regex group matching, $1, $2, and so on represent the matched groups 1, 2, and so forth, while $& signifies the complete match. For more information on marked sub-expressions, see the official Boost C++ Libraries documentation. Script literal or script variable n.a.
Flags

(Optional)

Use flags to modify the operation. Flags can be combined and provided in any order — for example, combine i with n as "in" or "ni", or use the script variable &FLAG#. Script literal or script variable
  • i — Case-insensitive. By default, the search is case sensitive.

  • g — Globally (applies to all occurrences in a line instead of only one or the first). When provided, the function finds all matches in a line; otherwise it finds only the first match in a line.

  • n — Not match null (for example, with "\d*"). The "*" qualifier searches for zero or more occurrences and tries to find as many as possible, which can return zero occurrences; this flag avoids that behavior.

  • u — Only unique lines are selected.

Examples

Example 1

Getting an agent's version:

!* Input

:SET &AGENT# = "AR_MAIN_JWX6_VFRKARWIN01_01"

:SET &BUILD_VERSION# = SYS_INFO(AGENT, VERSION,, &AGENT#)

:PRINT Build version: &BUILD_VERSION#

!* Regex

:SET &REGEX# = "\+.*"

:SET &REPLACE# = "\..*"

:SET &REGEX_VERS# = "((\d+)\.\d+\.\d+).*"

:SET &RULE_VERS_MAIN# = "$2"

!* Get Agent's version and main version

:SET &VERS# = REGEX_REPLACE(&BUILD_VERSION#&REGEX_VERS#)

:SET &VERS_MAIN# = REGEX_REPLACE(&BUILD_VERSION#&REGEX_VERS_MAIN#)

:PRINT Agent '&AGENT#' is of version &VERS# and of main version &VERS_MAIN#

After the execution, the activation report (ACT) displays the following:

2025-08-04 18:08:08 - U00020408 Build version: 24.6.0+low.build.1754215201098
2025-08-04 18:08:08 - U00020408 Agent 'AR_MAIN_JWX6_VFRKARWIN01_01' is of version 24.6.0 and of main version 24

Example 2

Getting an agent's version using a replacement rule:

!* Input

:SET &AGENT# = "AR_MAIN_JWX6_VFRKARWIN01_01"

:SET &BUILD_VERSION# = SYS_INFO(AGENT, VERSION,, &AGENT#)

:PRINT Build version: &BUILD_VERSION#

!* Regex and replacement rule

:SET &REGEX_VERS# = "((\d+)\.\d+\.\d+).*"

:SET &RULE_VERS# = "$1"

:SET &RULE_VERS_MAIN# = "$2"

!* Get Agent's version and main version using replacements rules

:SET &VERS# = REGEX_REPLACE(&BUILD_VERSION#&REGEX_VERS#&RULE_VERS#)

:SET &VERS_MAIN# = REGEX_REPLACE(&BUILD_VERSION#&REGEX_VERS#&RULE_VERS_MAIN#)

:PRINT Agent '&AGENT#' is of version &VERS# and of main version &VERS_MAIN#

After the execution, the activation report (ACT) displays the following:

2025-08-04 18:13:11 - U00020408 Build version: 24.6.0+low.build.1754215201098
2025-08-04 18:13:11 - U00020408 Agent 'AR_MAIN_JWX6_VFRKARWIN01_01' is of version 24.6.0 and of main version 24

Example 3

Getting an agent's version using a replacement rule and the u flag:

!* Input

:SET &AGENT# = "AR_MAIN_JWX6_VFRKARWIN01_01"

:SET &BUILD_VERSION# = SYS_INFO(AGENT, VERSION,, &AGENT#)

:PRINT Build version: &BUILD_VERSION#

!* Regex and replacement rule

:SET &REGEX_VERS# = "((\d+)\.\d+\.\d+).*"

:SET &RULE_VERS# = "$1"

:SET &RULE_VERS_MAIN# = "$2"

!* Get Agent's version and main version using replacements rules and flag 'u'

:SET &VERS# = REGEX_REPLACE(&BUILD_VERSION#&REGEX_VERS#&RULE_VERS#'u')

:SET &VERS_MAIN# = REGEX_REPLACE(&BUILD_VERSION#&REGEX_VERS#&RULE_VERS_MAIN#'u')

:PRINT Agent '&AGENT#' is of version &VERS# and of main version &VERS_MAIN#

After the execution, the activation report (ACT) displays the following:

2025-08-04 18:16:04 - U00020408 Build version: 24.6.0+low.build.1754215201098
2025-08-04 18:16:04 - U00020408 Agent 'AR_MAIN_JWX6_VFRKARWIN01_01' is of version 24.6.0 and of main version 24

Example 4

! Extract start and end time of the job from the messenger output of the job report from a Windows job in Post Processing

:SET &HND# = PREP_PROCESS_REPORT ()

:PROCESS &HND#

:   SET &REP_LINE# = GET_PROCESS_LINE(&HND#)

:   SET &TIME_N_DATE# = REGEX_REPLACE(&REP_LINE#"UCMDJP:.*(START AT |ENDED AT)(.*) .*", "$1: $2", "u")

:   IF &TIME_N_DATE# <> ""

:      PRINT &TIME_N_DATE#

:   ENDIF

:ENDPROCESS

After running the Job, the post processing report (POST) displays the following:

2025-08-04 18:21:31 - U00020408 START AT : 04.08.2025/18:21:31
2025-08-04 18:21:31 - U00020408 ENDED AT : 04.08.2025/18:21:31

See also: