REPLACE Statement

The REPLACE statement is used to replace source program text.

The REPLACE statement can occur anywhere in the source program where a character-string can occur. It must be preceded by a separator period except when it is the first statement in a separately compiled program. It must be terminated by a separator period.

The REPLACE statement resembles the REPLACING phrase of the COPY statement, except that it acts on the entire source program, not just the text in COPY libraries. It is an easy method of doing simple string substitutions.

REPLACE Statement - Format 1

Read syntax diagramSkip visual syntax diagramREPLACE==pseudo-text-1==BY==pseudo-text-2==LEADINGTRAILING==partial-word-1==BY==partial-word-2==.
Each matched occurrence of pseudo-text-1 is replaced by its corresponding pseudo-text-2 and each matched occurrence of a LEADING or TRAILING partial-word-1 is replaced by its corresponding partial-word-2. This process continues until any of the following are met:
  • The next occurrence of the REPLACE statement
  • End of the program
  • REPLACE OFF (see Format 2 below)

REPLACE Statement - Format 2

Read syntax diagramSkip visual syntax diagramREPLACEOFF.

Format 2 ends current text replacement specified by Format 1.

pseudo-text-1, pseudo-text-2
Pseudo-text is a sequence of text words, comment lines, or separator spaces bounded by, but not including, the pseudo-text delimiter (==).

Pseudo-text-1 must contain at least one text word other than a separator comma or separator semicolon. Beginning and ending spaces are not included in the text comparison process, and multiple embedded spaces are considered to be a single space.

Pseudo-text-2 does not need to contain a text word; it may consist solely of space characters and/or comment lines.

Since pseudo-text-1 requires a text word, which must be bounded by separators, pseudo-text cannot be used to replace part of a data name (for example, a prefix); the entire data name must be used.

IBM Extension

Pseudo-text-1 or pseudo-text-2 can contain DBCS or national character-strings. Such pseudo-text cannot be continued across lines.

End of IBM Extension
IBM Extension

When a REPLACE statement is in effect, there are certain restrictions on the layout of a Format 2 - DDS Translate COPY statement. (See COPY Statement - Format 2 - DDS Translate.)

End of IBM Extension
partial-word-1, partial-word-2
A single text word that is bounded by, but not including, pseudo-text delimiters (==). Both characters of each pseudo-text delimiter must appear on one line. However, the text word within a partial-word can be continued.
The following rules apply to partial-word-1 and partial-word-2:
  • partial-word-1 consists of one text word.
  • partial-word-2 consists of zero or one text word.
  • partial-word-1 and partial-word-2 cannot be an alphanumeric literal, national literal, DBCS literal, or DBCS word.

REPLACE statements are processed after all COPY statements have been processed. COPY must be processed first to assemble complete source text. Then, REPLACE can be used to modify that source text, performing simple string substitution. REPLACE statements cannot themselves contain COPY statements, and the text that results from the processing of a REPLACE statement must not include a REPLACE statement.