ABAP FIND Statement: Search Strings and Patterns (Regex)

Category
ABAP-Statements
Published
Author
Johannes

The FIND statement in ABAP is used to search within character-like data objects (variables of type STRING, C, N, D, T) for strings or patterns. It is a very flexible tool for text analysis and offers two main modes:

  1. Search for a fixed substring: Finds the exact occurrence of a specific string.
  2. Search using Regular Expressions (Regex): Finds patterns described by a regular expression (following the PCRE standard - Perl Compatible Regular Expressions). This allows for much more complex and flexible searches.

Syntax

The syntax varies slightly depending on the mode and desired options:

1. General Structure

FIND [{FIRST OCCURRENCE}|{ALL OCCURRENCES OF}]
{<substring>}|{REGEX <regex>}
IN [SECTION OFFSET <off> LENGTH <len> OF] <data_object>
[IGNORING CASE|RESPECTING CASE] " Ignore/respect case
[MATCH OFFSET <m_off>] " Store offset of match
[MATCH LENGTH <m_len>] " Store length of match
[MATCH COUNT <m_cnt>] " Store count of all matches (only with ALL OCCURRENCES)
[RESULTS <result_tab>|<result_wa>] " Store detailed results
[SUBMATCHES <sm1> <sm2> ...]. " Store subgroups for regex
FIND [{FIRST OCCURRENCE}|{ALL OCCURRENCES OF}] <substring>
IN <data_object>
[<options>]. " e.g., IGNORING CASE, MATCH OFFSET, MATCH LENGTH, MATCH COUNT, RESULTS

3. Syntax for Regular Expressions (Regex)

FIND [{FIRST OCCURRENCE}|{ALL OCCURRENCES OF}] REGEX <regex>
IN <data_object>
[<options>]. " e.g., IGNORING CASE, MATCH OFFSET, MATCH LENGTH, MATCH COUNT, RESULTS, SUBMATCHES

Explanation of Key Components and Options

  • FIND: The keyword that initiates the statement.
  • FIRST OCCURRENCE: (Default if nothing specified) Searches only for the first occurrence of the pattern from left to right.
  • ALL OCCURRENCES: Searches for all non-overlapping occurrences of the pattern.
  • <substring>: A variable or literal containing the exact text to search for (in substring mode).
  • REGEX <regex>: A variable or literal containing a Regular Expression (in regex mode).
  • IN <data_object>: The character-like data object (STRING, C, N, D, T) to search in.
  • IN SECTION OFFSET <off> LENGTH <len> OF: Restricts the search to a specific section within <data_object> (starting at offset <off> with length <len>; first offset is 0).
  • IGNORING CASE: Performs the search without considering case.
  • RESPECTING CASE: (Default) Performs the search with consideration of case.
  • MATCH OFFSET <m_off>: Assigns the start offset (position, 0-based) of the found pattern to variable <m_off> (type I).
  • MATCH LENGTH <m_len>: Assigns the length of the found pattern to variable <m_len> (type I).
  • MATCH COUNT <m_cnt>: (Only with ALL OCCURRENCES) Assigns the total count of found occurrences to variable <m_cnt> (type I).
  • RESULTS <result_tab | result_wa>: Stores detailed information about the matches.
    • With ALL OCCURRENCES: <result_tab> (type match_result_tab or compatible) is filled with information about all matches (offset, length, submatch offsets and lengths for regex).
    • With FIRST OCCURRENCE: <result_wa> (type match_result or compatible) is filled with information about the first match.
  • SUBMATCHES <sm1> <sm2> ...: (Only with REGEX) If the regular expression contains parenthesized groups (capture groups), the text found by the first group is stored in <sm1>, by the second in <sm2>, etc. (variables usually of type STRING).

System Fields

sy-subrc: Shows the result of the search:

  • 0: Successful (at least one occurrence found).
  • 4: Not successful (no occurrence found).
  • 8: Error (e.g., invalid regex with certain additions).

sy-fdpos: Contains the offset of the last found occurrence relative to the start of <data_object> after a successful search (sy-subrc = 0).

Examples

1. Find First Occurrence of a Substring

DATA text TYPE string VALUE `This is an example text. Exemplary!`.
DATA position TYPE i.
FIND 'example' IN text MATCH OFFSET position.
IF sy-subrc = 0.
WRITE: / 'Substring "example" found at position (offset):', position. " Output: ... at position (offset): 11
ELSE.
WRITE: / 'Substring "example" not found.'.
ENDIF.

2. Count All Occurrences (Case-Insensitive)

DATA count TYPE i.
FIND ALL OCCURRENCES OF 'example' IN text IGNORING CASE MATCH COUNT count.
IF sy-subrc = 0.
WRITE: / '"example" (without case) was found', count, 'times.'. " Output: ... 2 times found.
ENDIF.

3. Find First Occurrence with Regex (e.g., Find a Number)

DATA text2 TYPE string VALUE `Order 4711 from 20.04.2025 contains 15 items.`.
DATA rx_number TYPE string VALUE `\b\d+\b`. " Regex for one or more digits (as word)
DATA first_num_off TYPE i.
DATA first_num_len TYPE i.
FIND REGEX rx_number IN text2
MATCH OFFSET first_num_off
MATCH LENGTH first_num_len.
IF sy-subrc = 0.
WRITE: / 'First number found:', text2+first_num_off(first_num_len). " Output: 4711
WRITE: / 'At offset:', first_num_off, ', length:', first_num_len. " Output: At offset: 6 , length: 4
ENDIF.

4. All Occurrences with Regex and RESULTS Table

DATA match_results TYPE match_result_tab.
FIND ALL OCCURRENCES OF REGEX rx_number IN text2 RESULTS match_results.
IF sy-subrc = 0.
WRITE: / 'All numbers found:'.
LOOP AT match_results INTO DATA(match_line).
WRITE: / text2+match_line-offset(match_line-length),
'(Offset:', match_line-offset, 'Length:', match_line-length, ')'.
ENDLOOP.
" Output:
" 4711 (Offset: 6 Length: 4 )
" 20 (Offset: 16 Length: 2 )
" 04 (Offset: 19 Length: 2 )
" 2025 (Offset: 22 Length: 4 )
" 15 (Offset: 36 Length: 2 )
ENDIF.

5. Regex with Submatches (Extract Date)

DATA rx_date TYPE string VALUE `(\d{2})\.(\d{2})\.(\d{4})`. " Groups for day, month, year
DATA day TYPE string.
DATA month TYPE string.
DATA year TYPE string.
FIND REGEX rx_date IN text2 SUBMATCHES day month year.
IF sy-subrc = 0.
WRITE: / 'Date found:'.
WRITE: / 'Day:', day, 'Month:', month, 'Year:', year. " Output: Day: 20 Month: 04 Year: 2025
ENDIF.

6. Search in a Section

DATA name TYPE c LENGTH 30 VALUE 'Hans Peter Mustermann Berlin'.
FIND 'Peter' IN SECTION OFFSET 5 LENGTH 15 OF name.
IF sy-subrc = 0.
WRITE: / '"Peter" found in section 5-19, at offset:', sy-fdpos. " Output: ... at offset: 5
ELSE.
WRITE: / '"Peter" not found in section 5-19.'.
ENDIF.
FIND 'Berlin' IN SECTION OFFSET 5 LENGTH 15 OF name.
IF sy-subrc <> 0.
WRITE: / '"Berlin" not found in section 5-19.'. " Output: "Berlin" not found in section ... found.
ENDIF.

Important Notes

  • FIND is designed for character-like data objects.
  • Regular expressions are extremely powerful, but their syntax must be learned (ABAP uses PCRE).
  • The older SEARCH statement is functionally very limited and should be replaced by FIND.
  • To replace found patterns, use the REPLACE statement, which offers very similar search options as FIND.