Skip to content

ABAP Keyword Documentation →  ABAP - Reference →  Processing Internal Data →  Character String and Byte String Processing →  Statements for Character String and Byte String Processing →  FIND 

FIND - pattern

Short Reference

Other versions: 7.31 | 7.40 | 7.54

Syntax


...  {[SUBSTRING] substring} | {REGEX regex} ... . 

Variants

1. ... [SUBSTRING] substring.

2. ... REGEX regex.

Effect

Definition of a search string for the statements FIND and FIND IN TABLE. The search can either search for a substring substring or for a regular expression regex.


Note

The statements REPLACE and REPLACE IN TABLE use the same search string.

Variant 1

... [SUBSTRING] substring.

Effect

In this variant, the program searches for the exact instance of a substring specified in a character- or byte-like operand substring. substring is a character-like expression position. The word SUBSTRING is optional.

If substring is an empty string or is of type c, d, n or t and only contains empty characters, the system searches an empty substring. This is only possible when searching for the first instance, and the empty substring is always found before the first character or byte. In character string processing, the closing blanks are not taken into account for substring data objects of fixed length.


Note

If you want to take closing blanks into account in the substring, substring must have the data type string.


Example

Search for all occurrences of the string "now" in a text string literal. The offsets 11 and 24 of both found locations are displayed as output.

DATA: patt TYPE string VALUE `now`, 
      text TYPE string, 
      result_tab TYPE match_result_tab. 

FIELD-SYMBOLS <match> LIKE LINE OF result_tab. 


FIND ALL OCCURRENCES OF patt IN 
     `Everybody knows this is nowhere` 
     RESULTS result_tab. 

LOOP AT result_tab ASSIGNING <match>. 
  WRITE: / <match>-offset, <match>-length. 
ENDLOOP. 

Example

Search for all occurrences of the string "now" in a text string literal using a WHILE loop. After every successful search, the search range is redefined to start after the found location. This enabled all occurrences of the search pattern to be found even before the addition ALL OCCURRENCES was introduced.

DATA: patt TYPE string VALUE `now`, 
      text TYPE string, 
      off  TYPE i, 
      moff TYPE i, 
      mlen TYPE i. 

off = 0. 
WHILE sy-subrc = 0. 
  FIND patt IN SECTION OFFSET off OF 
       `Everybody knows this is nowhere` 
       MATCH OFFSET moff 
       MATCH LENGTH mlen. 
  IF sy-subrc = 0. 
    WRITE / moff. 
    off = moff + mlen. 
  ENDIF. 
ENDWHILE. 

Variant 2

... REGEX regex.

Effect

In this variant, the program searches for a match with a regular expression specified in regex. For regex, you can either specify character-like operand that contains a valid, regular expression when the statement is executed, or an object reference variable that points to an instance of the class CL_ABAP_REGEX. If specified directly, regex is a character-like expression position.

When searching for a regular expression, specific search strings can be entered that permit further conditions including forecast conditions. The found locations are determined according to the "leftmost-longest" rule. Of all the possible matches between the regular expression and the required character string, the substring starting in the furthest position to the left is selected. If there are several matches in this position, the longest of these substrings is selected.

An empty substring in regex is not a valid regular expression and leads to an exception. A character string is empty if regex is either an empty string or is of type c, d, n, or t and only contains blank characters.


Notes

  • Some regular expressions that are not empty, such as a*, are used to search for empty character strings. This is possible when searching for the first occurrence or all occurrences. The relevant empty substrings are found before the first character, between all characters, and after the last character of the search ranges. A range of this type is always successful.
  • A regular expression can be syntactically correct, but too complex for the execution of the statement FIND, which leads to a treatable exception of the class CX_SY_REGEX_TOO_COMPLEX. Refer to Exceptions in Regular Expressions.

Example

The following search finds the substring 'ababb' from offset 3 or higher. Using the "leftmost-longest" rule, the other matching substring'babboo' from offset 4 or higher is not found.

DATA: moff TYPE i, 
      mlen TYPE i. 

FIND REGEX 'a.|[ab]+|b.*' IN 'oooababboo' 
     MATCH OFFSET moff 
     MATCH LENGTH mlen.