REPLACE IN TABLE itab

Short Reference

Syntax

REPLACE [{FIRST OCCURRENCE}|{ALL OCCURRENCES} OF] pattern
        IN TABLE itab [table_range] WITH new
        [IN {BYTE|CHARACTER} MODE]
        [{RESPECTING|IGNORING} CASE]
        [REPLACEMENT COUNT rcnt]
        { {[REPLACEMENT LINE rlin]
           [REPLACEMENT OFFSET roff]
           [REPLACEMENT LENGTH rlen]}
        | [RESULTS result_tab|result_wa] }.

Addition:

... REPLACEMENT LINE rlin

Effect

: The internal table itab is searched line by line for the byte/character string defined in pattern and the found locations are replaced with the content of the data object new. For itab, a standard table must be specified. The line type of the table must be either byte-type or character-type depending on the addition BYTE CHARACTER MODE. Byte or character strings that span more than one line of a table are not replaced.

With the addition table_range,the search area can be restricted to the table. The addition REPLACEMENT LINE returns the last line number in which the search string was found. The other additions have the same effect on replacing in the individual table lines as in pattern-based replacement with REPLACE in elementary byte or character strings. When using the addition RESULTS, the line numbers of all found locations are also stored in the component LINE of the corresponding table line of result_tab and the line number of the last found location is stored in result_wa.

In character string processing, the closing blanks are taken into account for line types of fixed length, but not for new.

System fields

sy-subrc Explanation
0 The search string has been replaced by the content of new and the full result is available in the table row.
2 The search string has been replaced by the content of new and the result of the replacement was cut off to the right in at least one table line.
4 The search string in pattern was not found in the internal table.
8 The data objects pattern or new do not contain interpretable double-byte characters.

The values of sy-tabix and sy-fdpos are not changed.

Addition

... REPLACEMENT LINE rlin

Effect

: The addition REPLACEMENT LINE contains the number of the line in the data object rlin in which the last found location has been replaced. If no replacement is made, rlin retains its previous value.

Example

: A simple DM-Euro conversion.

DATA itab TYPE TABLE OF string.

APPEND 'Beer  - 3 DM' TO itab.
APPEND 'Pizza - 8 DM' TO itab.

REPLACE ALL OCCURRENCES OF REGEX '\b(DM)\b'
  IN TABLE itab WITH 'EUR'
  RESPECTING CASE.

Exceptions

Catchable Exceptions

CX_SY_REPLACE_INFINITE_LOOP

CX_SY_INVALID_REGEX