Get all occurrences of the string between any two specific characters in SAP ABAP

I usually use REGEX in all such cases as it is faster and easily readable and would recommend the same to you.

You can use something similar as the snippet to get your job done.

Example

Here's a complete example that extracts all text between ampersand characters ?

DATA: lv_para TYPE string,
      lv_result TYPE string.

lv_para = ' You &are like& kite &flying& in a &hurricane&'.

" Remove all text between & characters including the & symbols
lv_result = lv_para.
REPLACE ALL OCCURRENCES OF REGEX '&[^&]+&' IN lv_result WITH ''.
WRITE lv_result.

The output of the above code is ?

 You  kite  in a 

Regex Pattern Explanation

Let me explain the regex pattern &[^&]+& for you:

  • & ? Matches the first ampersand character
  • [^&]+ ? Matches one or more characters that are NOT ampersand (the ^ inside brackets means "not")
  • & ? Matches the closing ampersand character

This pattern effectively captures any text enclosed between two ampersand characters, including the ampersands themselves.

Alternative Approach

If you want to extract the content between the characters instead of removing it, you can use the FIND statement with regex ?

DATA: lv_para TYPE string,
      lv_match TYPE string.

lv_para = ' You &are like& kite &flying& in a &hurricane&'.

FIND REGEX '&([^&]+)&' IN lv_para SUBMATCHES lv_match.
IF sy-subrc = 0.
  WRITE: 'Found:', lv_match.
ENDIF.

Conclusion

Using REGEX with the REPLACE or FIND statements in SAP ABAP provides an efficient way to handle text between specific delimiter characters. The pattern &[^&]+& is particularly useful for extracting or removing content enclosed within ampersand characters.

Updated on: 2026-03-13T18:53:55+05:30

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements