SAP NetWeaver AS ABAP Release 740, ©Copyright 2014 SAP AG. All rights reserved.
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
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.
... [SUBSTRING] substring.
Effect
In this variant, the program searches for the exact occurrence of a substring specified in a character-like 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 occurrence, and the empty substring is always found before the first character or byte.
In character string processing, the trailing blanks are ignored for substring data objects of fixed length.
Note
If trailing blanks are not to be ignored in the substring, substring must have the data type string.
Example
Searches 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>.
cl_demo_output=>write( |{ <match>-offset } {
<match>-length }| ).
ENDLOOP.
cl_demo_output=>display( ).
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.
cl_demo_output=>write_data( moff ).
off = moff + mlen.
ENDIF.
ENDWHILE.
cl_demo_output=>display( ).
... REGEX regex.
Effect
In this variant, the program searches for a match with a regular expression specified in regex. For regex, either a character-like operand can be specified 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 multiple matches in this position, the longest of these substrings is selected.
An empty substring in regex is not a valid regular expression and raises
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
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.