Replace in ABAP Strings - Pattern-based replacement



User Rating:  / 3
PoorBest 
Details

REPLACE pattern IN

Syntax

REPLACE [{FIRST OCCURRENCE}|{ALL OCCURRENCES} OF] pattern

IN [section_of] dobj WITH new
[IN {BYTE|CHARACTER} MODE]
[{RESPECTING|IGNORING} CASE]
[REPLACEMENT COUNT rcnt]
{ {[REPLACEMENT OFFSET roff]
[REPLACEMENT LENGTH rlen]}
| [RESULTS result_tab|result_wa] }.

Extras:

1. ... {FIRST OCCURRENCE}|{ALL OCCURRENCES} OF

2. ... IN {BYTE|CHARACTER} MODE

3. ... {RESPECTING|IGNORING} CASE

4. ... REPLACEMENT COUNT rcnt

5. ... REPLACEMENT OFFSET roff

6. ... REPLACEMENT LENGTH rlen

7. ... RESULTS result_tab|result_wa

Effect

When making replacements based on patterns, the system searches the data object dobj for the byte or character string specified by pattern and replaces the target location with the content of the data object new. If a regular expression is used in pattern, special replacements patterns can be used in new that permit a reference to each target location.

The syntax and effect of the addition section_of are the same as searching for a substring in a data object using the statement FIND, and the same treatable exception can occur.

Note

The REPLACE IN TABLE statement is available for replacements in internal tables.

Addition 1

... {FIRST OCCURRENCE}|{ALL OCCURRENCES} OF

Effect

The optional {FIRST OCCURRENCE}|{ALL OCCURRENCES} OF addition specifies whether only the first or all occurrences of the search pattern are replaced by the content of new. If the FIRST OCCURENCE addition or none of the additions is specified, the first occurrence is replaced. In the case of ALL OCCURRENCES, all non-overlapping occurrences are replaced.

 If sub_string in pattern is an empty string or is of type c, d, n, or t and only contains empty characters, the position before the first character or byte of the search area is located when the first occurrence is being searched for, and the content of new is inserted in front of the first character. If all occurrences are being searched for, the CX_SY_REPLACE_INFINITE_LOOP exception is triggered in this case.

If regex in pattern contains a regular expression that is the same as the empty character string, the content of new is also inserted before the first character when the first occurrence is being searched for. When all occurrences are being searched for, the content of new is inserted in this case in front of the first character, in all spaces that do not come before or inside a match, and after the last character.

Example

After replacement, text contains value "x-xx-x".

DATA text TYPE string VALUE '-uu-'.

REPLACE ALL OCCURRENCES OF REGEX 'u*' IN text WITH 'x'.

Addition 2

... IN {BYTE|CHARACTER} MODE

Effect

The optional IN {BYTE|CHARACTER} MODE addition determines whether byte or character string processing is carried out. If the addition is not specified, character string processing is carried out. Depending on the processing type, dobj, new and sub_string in pattern have to be byte-type or character-type. If regular expressions are used in pattern, only character string processing is permitted.

Note

For replacement with character-type content that contains trailing blank characters, new has to have the data type string.

Addition 3

... {RESPECTING|IGNORING} CASE

Effect

This addition is only permitted for processing character strings. It has the same syntax and effect as the corresponding addition for searching for a substring in a data object using the statement FIND. When an instance of the CL_ABAP_REGEX class is used, this addition is not permitted.

Addition 4

... REPLACEMENT COUNT rcnt

Effect

This addition causes the number of replacements made within the data object dobj to be assigned to the data object rcnt. A variable of the type i is expected for rcnt. If no replacement takes place, rcnt is set to 0.

Addition 5

... REPLACEMENT OFFSET roff

Effect

This addition stores the offset relating to the data object dobj, of the found location that was last replaced in dobj in the data objectroff, after all replacements. A variable of the data type i is expected for roff. If there is no replacement, roff retains its previous value.

Addition 6

... REPLACEMENT LENGTH rlen

Effect

This addition stores that length of the last section replaced in dobj, in the data object rlen. A variable of the data type i is expected for rlen. If no replacement is carried out, rlen retains its previous value.

Note

In data objects of fixed length, the number of replacements made within the data object dobj can be smaller than the number of found locations, and the length of the last replaced section can be smaller than the length of new, if the intermediate result is truncated.

Addition 7

... RESULTS result_tab|result_wa

Effect

If at least one replacement is made, the RESULTS addition saves the offsets of the replaced positions and the lengths of the found sub-strings, either in an internal table result_tab or in a structure result_wa. The syntax and meaning of the addition are the same as with the FIND statement, with the exception that the data types for result_tab and result_wa have to be REPL_RESULT_TAB or REPL_RESULT here, for which there is no SUBMATCHES component.

Example

Pattern-based replacement of all found locations of the word "know" in the data objects text1 and text2 by "should know that". After the first REPLACE statement, text1 contains the complete content "I should know that You should know that ", and sy-subrc contains the value 0. The data objects cnt, off, and len contain the values 2, 23, and 16 respectively. After the second REPLACE statement, text2 contains the truncated content "I should know that" and sy-subrc contains the value 2. The data objects cnt, off, and len contain the values 1, 2, and 16.

View source
DATA: text1 TYPE string, 
text2(18) TYPE c, 
cnt TYPE i, 
off TYPE i, 
len TYPE i.
 
text1 = text2 = 'I know you know'.
 
REPLACE ALL OCCURRENCES OF 'know' IN: 
text1 WITH 'should know that' 
REPLACEMENT COUNT cnt 
REPLACEMENT OFFSET off 
REPLACEMENT LENGTH len, 
text2 WITH 'should know that' 
REPLACEMENT COUNT cnt 
REPLACEMENT OFFSET off 
REPLACEMENT LENGTH len. 

You have no rights to post comments

   

Login  

   

     

© Developerpages