SAP REPLACE IN PATTERN ABAP Statements



Get Example source ABAP code based on a different SAP table
  


• SECTION OF REPLACE pattern IN
• IN REPLACE

REPLACE pattern IN
Short Reference

ABAP_SYNTAX
REPLACE $[${FIRST OCCURRENCE$}$| ${ALL OCCURRENCES$} OF$] pattern
IN $[section_of$] dobj WITH new
$[IN ${CHARACTER$|BYTE$} MODE$]
$[replace_options$].

ABAP Addition
1 ... ${FIRST OCCURRENCE$}$|${ALL OCCURRENCES$} OF
2 ... IN ${CHARACTER$|BYTE$} MODE

What does it do?
In pattern-based replacements, the data object dobj is searched for the character or byte strings specified in pattern and the occurrences are replaced by the content of the operand new. new is a functional operand position.
If a regular expression is used in pattern, special replacement patterns can be specified in new that allow a reference to the respective occurrence. It should be noted that, in regular replacement texts, the associated special characters $, , `, and ยด as well as the escape character must be transformed to literal characters using the prefix .
The syntax and effect of the addition section_of are the same as searching a data object for a substring using the statement FIND, and the same catchable exception can be raised. The addition MODE defines whether a character or byte string is processed, and the addition replace_options provides additional options for controlling and evaluating the statement.



Latest notes:

The statement REPLACE IN TABLE can be used to make replacements in internal tables.
NON_V5_HINTS
ABAP_HINT_END

ABAP_EXAMPLE_VX5
The result of the replacement is $bcd$. While $1 in the replacement pattern represents the parenthesized subgroup bcd of the regular expression a(bcd)e, the two $ stand for the literal character $.
ABEXA 00545
ABAP_EXAMPLE_END
• FIRST OCCURRENCE OF REPLACE
• ALL OCCURRENCES OF REPLACE

ABAP Addition

What does it do?
The optional addition ${FIRST OCCURRENCE$}$|${ALL OCCURRENCES$} OF specifies whether only the first occurrence or all occurrences of the search pattern are replaced by the content of new. If the addition FIRST OCCURRENCE or none of the additions are specified, the first occurrence is replaced. If ALL OCCURRENCES is used, all non-overlapping occurrences are replaced.
If substring is an empty string in pattern or is of the type c, n, d, or t and only contains blanks, the place in front of the first character or byte of the search range is found when searching for the first occurrence, and the content of new is inserted in front of the first character or byte. When searching for all occurrences, the exception CX_SY_REPLACE_INFINITE_LOOP is raised in this case.
If regex contains a regular expression in pattern that matches the empty character string, the content of new is also inserted in front of the first character when searching for the first occurrence. When searching for all occurrences, the content of new is inserted in front of the first character in all spaces that are not in front of or inside a match, and after the last character.



Latest notes:

If the addition ALL OCCURRENCES is used, no recursive processing takes place. This means that, if the statement finds further occurrences of the search pattern, they are not replaced automatically, since this could create endless loops.
NON_V5_HINTS
ABAP_HINT_END

ABAP_EXAMPLE_VX5
After the replacement, text contains the value x-xx-x.
ABEXA 00546
ABAP_EXAMPLE_END

ABAP_EXAMPLE_VX5
After the replacement, text still contains the substring abcde once.
ABEXA 00547
ABAP_EXAMPLE_END
• IN BYTE MODE REPLACE
• IN CHARACTER MODE REPLACE

ABAP Addition

What does it do?
The optional addition IN ${CHARACTER$|BYTE$} MODE determines whether character string or byte string processing is performed. If the addition is not specified, character string processing is performed. Depending on the processing type, dobj, new, and substring in pattern must be character-like or byte-like. If regular expressions are used in pattern, only character string processing is allowed.



Latest notes:

For replacements with character-like content that contains trailing blanks, new must have the data type string.
NON_V5_HINTS
ABAP_HINT_END

ABAP_EXAMPLE_VX5
Replacement of all bytes that represent a hyphen in code page UTF-8 with bytes for an underscore.
ABEXA 00548
ABAP_EXAMPLE_END



Runtime Exceptions



Catchable Exceptions
CX_SY_REPLACE_INFINITE_LOOP
Reason for error:
Substring of length 0 creates an endless loop when searching for all occurrences.
Runtime error:
REPLACE_INFINITE_LOOP
CX_SY_INVALID_REGEX
Reason for error:
Invalid expression after the addition PCRE$|REGEX.
Runtime error:
INVALID_REGEX
CX_SY_REGEX_TOO_COMPLEX
Reason for error:
See Exceptions in Regular Expressions.
Runtime error:
REGEX_TOO_COMPLEX
CX_SY_INVALID_REGEX_FORMAT
Reason for error:
Invalid replacement pattern after the addition WITH.
Runtime error:
INVALID_FORMAT

Return to menu