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
-
String
Unicode character string to search.
Format: script literal or script variable
-
Regex
Regular expression to be used.
The regular expression use the Boost C++ Libraries syntax. For more information, see the official Boost C++ Libraries documentation.
-
Replacement String
Replacement rule to be used.
For instance, 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.
-
Flags
(Optional) Use flags to modify the operation. The flags available are:
-
i: Case-insensitive
By default, the search is case sensitive.
-
g: Globally (applies to all occurrences in a line instead of only one or only the first)
When provided, the function finds all matches in a line; if it is not provided, the function only finds the first match in a line
-
n: Not match null (for example, with "\d*")
The "*" qualifier searches for zero or more occurrences of a pattern and tries to find as many occurrences as possible. This might return zero occurrences. Setting this flag avoids that behavior.
-
u: Only unique lines
Only unique lines are selected.
Flags can be combined and provided in any order. For example, if you combine flag i with flag n, you can do so using "in" or "ni", or the script variable &FLAG#. An exception is the flag "d", which can be used to specify a multi character sequence as a delimiter used to separate columns in a text.
-
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 ®EX# = "\+.*"
:SET &REPLACE# = "\..*"
!* Get Agent's version and main version
:SET &VERS# = REGEX_REPLACE(&BUILD_VERSION#, ®EX_VERS#)
:SET &VERS_MAIN# = REGEX_REPLACE(&BUILD_VERSION#, ®EX_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 ®EX_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#, ®EX_VERS#, &RULE_VERS#)
:SET &VERS_MAIN# = REGEX_REPLACE(&BUILD_VERSION#, ®EX_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 ®EX_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#, ®EX_VERS#, &RULE_VERS#, 'u')
:SET &VERS_MAIN# = REGEX_REPLACE(&BUILD_VERSION#, ®EX_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: