The REPLACE statement in ABAP is used to find occurrences of a search pattern (either a fixed string/substring or a pattern defined by a regular expression) within a character-like data object (e.g., STRING, C, N, D, T) and replace them with another string.
An important aspect is that REPLACE directly modifies the variable being replaced in (“in place” modification).
Syntax
REPLACE [{FIRST OCCURRENCE}|{ALL OCCURRENCES OF}] " What to replace? (First or All) {<substring>}|{REGEX <regex>} " What to search for? (Text or Regex) IN <variable> " Where to search/replace? (Gets modified!) WITH <replacement> " What to replace with? [<options>]. " Additional optionsFIRST OCCURRENCE: Replaces only the first found occurrence of the search pattern.ALL OCCURRENCES OF(Default, ifFIRST OCCURRENCEis omitted): Replaces all non-overlapping occurrences of the search pattern.<substring>: The exact text (literal or variable) to search for.REGEX <regex>: A regular expression (literal or variable, PCRE syntax) describing the pattern to search for. This enables flexible pattern matching.IN <variable>: The character-like variable whose content is searched and modified. Caution: The content of this variable is directly changed!WITH <replacement>: The string (literal or variable) to insert in place of the found search patterns.<options>: (Selection)IGNORING CASE/RESPECTING CASE(Default): Controls whether case is considered during the search.REPLACEMENT COUNT <cnt>: Stores the number of replacements performed in the variable<cnt>(TypeI).REPLACEMENT OFFSET <off>: Stores the offset of the position after the last replacement in<off>.REPLACEMENT LENGTH <len>: Stores the length of the last inserted replacement string in<len>.RESULTS <results>: Allows storing detailed information about each replacement (position, length, etc.) in a structure or internal table (similar toFIND).IN SECTION OFFSET <sec_off> LENGTH <sec_len> OF: Restricts the search and replacement to a subsection of the<variable>.
Special Characters in Replacement (<replacement>) with REGEX
When REGEX is used, special placeholders can be used in the <replacement> string:
$0or$&: Represents the entire text section found by the regex.$1,$2,$3…: Represents the text captured by the first, second, third… capture group (parenthesized group) in the regular expression.`(Backtick): Represents the part of the<variable>before the found text section.$': Represents the part of the<variable>after the found text section.$$: Represents a literal dollar sign ($) in the replacement text.
Behavior with Fixed Length (Type C)
When <variable> is of type C (fixed length):
- If the
<replacement>is shorter than the found text, the remaining space is right-padded with spaces. - If the
<replacement>is longer than the found text and the result would exceed the defined length of the variable, the result is truncated on the right. Data loss is possible! - Recommendation: For replacements where the length may change significantly, using a target variable of type
STRINGis safer.
System Fields
sy-subrc:0: At least one replacement was performed.4: The search pattern was not found, no replacement occurred.
sy-fdpos: Contains the offset of the position after the last replaced or inserted text.
Modern Alternatives
Especially for STRING variables, modern ABAP often offers alternatives that are sometimes more readable or don’t modify the original variable:
- Function
replace(...): Returns a new string with replacements made.DATA(new_string) = replace( val = old_string sub = 'old' with = 'new' occ = 0 ). " occ=0 -> all - String Templates (
|...|): Can be used for more complex string reconstructions.
REPLACE remains a powerful tool, especially with the REGEX option.
Examples
1. Replace First Occurrence
DATA text1 TYPE string VALUE 'Test: Apple, Pear, Apple'.REPLACE FIRST OCCURRENCE OF 'Apple' IN text1 WITH 'Orange'.WRITE: / text1. " Output: Test: Orange, Pear, Apple2. Replace All Occurrences (Case-Insensitive)
DATA text2 TYPE string VALUE 'Error: ERROR Code 1, Error Code 2'.REPLACE ALL OCCURRENCES OF 'error' IN text2 WITH 'WARNING' IGNORING CASE.WRITE: / text2. " Output: Error: WARNING Code 1, WARNING Code 23. Count the Number of Replacements
DATA text3 TYPE string VALUE 'a b c a b c a'.DATA count TYPE i.REPLACE ALL OCCURRENCES OF 'a' IN text3 WITH 'X' REPLACEMENT COUNT count.WRITE: / text3. " Output: X b c X b c XWRITE: / 'Number of replacements:', count. " Output: 34. Replace with Regular Expression (Multiple Spaces -> 1 Space)
DATA text4 TYPE string VALUE 'Word1 Word2 Word3 Word4'.REPLACE ALL OCCURRENCES OF REGEX `\s{2,}` IN text4 WITH ` `. " \s{2,} = 2 or more spacesWRITE: / text4. " Output: Word1 Word2 Word3 Word45. Regex with Capture Groups (Reformat Date YYYYMMDD -> DD.MM.YYYY)
DATA date_in TYPE string VALUE '20250421'.REPLACE REGEX `(\d{4})(\d{2})(\d{2})` IN date_in WITH '$3.$2.$1'. " $1=YYYY, $2=MM, $3=DDWRITE: / date_in. " Output: 21.04.20256. Type C and Truncation
DATA text_c TYPE c LENGTH 15 VALUE 'Short Text'.
WRITE: / 'Before:', |'{ text_c }'|.REPLACE 'Text' IN text_c WITH 'longer content'. " Replacement is much longerWRITE: / 'After:', |'{ text_c }'|.Output:
Before: '{Short Text }'After: '{Short longer co}'(The replacement “longer content” was truncated after “longer co” to maintain the field length of 15).
Best Practices
- Remember that
REPLACEdirectly modifies the variable. Make a copy beforehand if needed. - Be careful with
REPLACEon fixed-length fields (Type C), as data loss through truncation can occur. Preferably useSTRINGfor such cases. - Use the
REGEXoption for flexible and powerful pattern replacements. - Check
sy-subrcafter the statement to determine if replacements occurred. - Consider the
replace(...)function and string templates as modern alternatives, especially when the original value should be preserved.