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
Other versions: 7.31 | 7.40 | 7.54
Syntax
... {[SUBSTRING] substring} | {REGEX regex} ... .
Variants
1. ... [SUBSTRING] substring.
2. ... REGEX regex.
Effect
Defines a search string for the statements FIND
and FIND IN TABLE
. The search
can either search for an exact substring substring
or for a substring that matches a regular expression regex
.
Variant 1
... [SUBSTRING] substring.
Effect
In this variant, a search is performed 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 optional word SUBSTRING
can be specified for emphasis.
If substring
is an empty string or is of type c
,
n
, d
, or t
and only
contains blanks, a search is performed for 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 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
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>.
cl_demo_output=>write( |{ <match>-offset } {
<match>-length }| ).
ENDLOOP.
cl_demo_output=>display( ).
Example
Searches 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( ).
Variant 2
... REGEX regex.
Effect
In this variant, a search is performed 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.
In searches using a regular expression, specific search strings can be entered that permit further conditions including forecast conditions. The occurrences 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 raises
an exception. A character string is empty if regex
is either an empty string
or is of type c
, n
, d
, or t
and only contains blanks.
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 have correct syntax, but be too complex for the execution of the statement
FIND
, which raises a handleable exception of the class CX_SY_REGEX_TOO_COMPLEX. See Exceptions in Regular Expressions.
Example
The following search finds the substring 'ababb' from offset 3. The other matching substring 'babboo' from offset 4 is not found using the "leftmost-longest" rule.
DATA: moff TYPE i,
mlen TYPE i.
FIND REGEX 'a.|[ab]+|b.*' IN 'oooababboo'
MATCH OFFSET moff
MATCH LENGTH mlen.