Replace

The Replace function uses Java regular expressions to replace a substring of a specified string.

Note: To replace a backslash character (\) in a string, you must escape the character twice in the expression, resulting in a string with four backslash characters (\\\\). For example, to replace the substring first\second in a string, you must specify it as first\\\\second.

Syntax

The Replace function has the following syntax:

String = Replace(Expression, Pattern, Substitution, MaxNum)

Parameters

The Replace function has the following parameters.

Table 1. Replace function parameters

Parameter

Format

Description

Expression

String

String that contains the substring to be replaced.

Pattern

String

Regular expression that matches the substring to be replaced.

Substitution

String

String to substitute for the substring.

MaxNum

Integer

Maximum number of replacements to perform.

Return value

The resulting string.

Example 1

The following example shows how to replace a substring in a string.

MyString = "New York";
Pattern = "York";
Substitution = "Jersey";
MyReplace = Replace(MyString, Pattern, Substitution, 1);
Log(MyReplace);

This example prints the following message to the policy log:

Parser Log: New Jersey

Example 2

The following example shows how to replace whitespace in a string.

MyString = "New York City";
Pattern = "\\s";
Substitution = "_";
MyReplace = Replace(MyString, Pattern, Substitution);
Log(MyReplace);

This example prints the following message to the policy log:

Parser Log: New_York_City

Example 3

The following example shows how to replace all digits in a string.

MyString = "2 hours and 3 days";
Pattern = "[0-9]";
Substitution = "six";
MyReplace = Replace(MyString, Pattern, Substitution);
Log(MyReplace);

This example prints the following message to the policy log:

Parser Log: six hours and six days 

Example 4

The following example shows how to replace a special character in a string by escaping the regular expression pattern.

MyString = "SELECT * FROM TABLE";
Pattern = "\*";
Substitution = "ID";
MyReplace = Replace(MyString, Pattern, Substitution);
Log(MyReplace);

This example prints the following message to the policy log:

Parser log: SELECT ID FROM TABLE