ARTICLE
replace - Replace Function
Syntax Forms
1. ... replace( val = text [
off = off ] [
len = len ]
with = new ) ...
2. ... replace( val = text {
sub = substring }|{
regex = regex }
with = new [ case = case
] [ occ = occ ] ) ...
Effect
This function replaces a subfield of
text with the character string specified in new and returns
the changed text.
The substring is determined as follows:
The variant with the arguments off
and len replaces the
subfield defined by the offset off and the length len . At
least one of these additions must be defined.
The variant with the arguments sub or
regex scans the text
for the occurrence specified in occ
for a match with the substring specified in
substring or with a
regular expression specified in
regex and replaces the found
location. If occ contains the value 0, all found locations are
replaced. If substring is empty, an exception from the class
CX_SY_STRG_PAR_VAL is raised. The search is case-sensitive by
default, but you can override this with the parameter case . If
no substring is found, the unchanged content of text is returned.
new is a character-like
expression position . If they have a fixed length, trailing blanks are
ignored.
The return code has the type string accordingly.
Notes
Borderline cases for the variants with the arguments off and
len :
If only off is specified or if the value 0 is specified for
len , then replace functions like
insert .
If only len is specified or if off has the value 0, then
the first segment of the length len is replaced.
If the value of off is equal to the length of text , then
the value of len must be equal to 0 or len is not
specified. The character string new is then attached to the end
of text .
If a regular expression is used with
regex then special replacement models
that allow references to particular found locations can be specified
in new .
Example
The result of the following replacement is
"<(><<)>title>Th<(>
<<)>b>i<(><<)>/b>s <(><<)>b>i<(><<)>/b>s the <(><<)>i>T<(><<)>b>i<(>
/b tle /i /title " . In an HTML line, a
particular letter is placed in format tags if it is not itself in
a tag.
DATA: html TYPE string,
repl TYPE string.
html = `<(><<)>title>This is the <(><<)>i>Title<(><<)>/i><(>
<<)>/title>`.
repl = `i`.
html = replace( val = html
regex = repl <(> <)><(> <)> `(?![^<(><<)>>]*>)`
with = `<(><<)>b>$0<(><<)>/b>`
occ = 0 ).
Runtime Exceptions
Catchable Exceptions
CX_SY_RANGE_OUT_OF_BOUNDS
Reason for error: Illegal offset or length specification in the off
and len .
Runtime error: STRING_OFFSET_TOO_LARGE
CX_SY_REGEX_TOO_COMPLEX
Reason for error: More information: Exceptions
in Regular Expressions .
Runtime error: REGEX_TOO_COMPLEX
CX_SY_STRG_PAR_VAL
Reason for error: Substring in sub or regular expression in
regex is empty.
Runtime error: STRG_ILLEGAL_PAR
Documentation extract taken from SAP system, � Copyright SAP AG. All rights reserved