ABAP Keyword Documentation → ABAP − Reference → Processing Internal Data → Character String and Byte String Processing → Statements for Character String and Byte String Processing → FIND
FIND - options
Other versions: 7.31 | 7.40 | 7.54
Syntax
... [{RESPECTING|IGNORING} CASE]
[MATCH COUNT mcnt]
{ {[MATCH OFFSET moff]
[MATCH LENGTH mlen]}
| [RESULTS result_tab|result_wa] }
[SUBMATCHES s1 s2 ...] ...
Extras
1. ... {RESPECTING|IGNORING} CASE
2. ... MATCH COUNT mcnt
3. ... MATCH OFFSET moff
4. ... MATCH LENGTH mlen
5. ... RESULTS result_tab|result_wa
6. ... SUBMATCHES s1 s2 ...
Effect
These additions control the FIND
statement and provide advanced evaluation options. The addition CASE
can
be used to specify whether the search is case-sensitive. The additions MATCH
,
SUBMATCHES
, and RESULTS
can be used to determine the number, position, and length of the sequence(s) found.
Addition 1
... {RESPECTING|IGNORING} CASE
Effect
This addition can be used only when processing strings. It specifies whether pattern
and dobj
are to be handled as case-sensitive in the search. When RESPECTING
CASE is used, the search is case-sensitive and when IGNORING CASE
is used, it is not. If neither of these additions is specified, RESPECTING CASE
is used implicitly. If a regular expression is passed as an object of class CL_ABAP_REGEX
for pattern
, this addition is not permitted. Instead, the properties of the object are respected by the search.
Example
The first search finds the letter "a", the second finds the letter "A".
FIND 'A' in `aA` IGNORING CASE MATCH OFFSET DATA(moff1).
FIND 'A' in `aA` RESPECTING CASE MATCH OFFSET DATA(moff2).
cl_demo_output=>display( |{ moff1 }, { moff2 }| ).
Addition 2
... MATCH COUNT mcnt
Effect
If the search pattern pattern
is found in the search range, the MATCH
COUNT addition saves the number of occurrences in mcnt
. The following can be specified for mcnt
:
-
An existing variable that expects the data type
i
. -
An inline declaration
DATA(var)
. The declared variable has the data typei
.
If FIRST OCCURRENCE
is used, the value after a successful search is always 1. If the search is not successful, mcnt
is set to 0.
Example
Checks whether the numbers of opening and closing tags in an HTML file match.
DATA html TYPE string.
...
FIND ALL OCCURRENCES OF '<div' IN html IGNORING CASE
MATCH COUNT DATA(divo).
FIND ALL OCCURRENCES OF '</div' IN html IGNORING CASE
MATCH COUNT DATA(divc).
ASSERT divo = divc.
Addition 3
... MATCH OFFSET moff
Effect
If the search pattern pattern
is found in the search range, the MATCH OFFSET
addition saves the
offset of the last occurrence
in relation to the dobj
operands in moff
. The following can be specified for moff
:
-
An existing variable that expects the data type
i
. -
An inline declaration
DATA(var)
. The declared variable has the data typei
.
If FIRST OCCURRENCE
is used, the result is the offset of the first occurrence.
If the search is not successful, moff
retains its previous value or remains initial.
Addition 4
... MATCH LENGTH mlen
Effect
If the search pattern pattern
is found in the search range, the MATCH
LENGTH addition saves the length of the last substrings found in mlen
. The following can be specified for mlen
:
-
An existing variable that expects the data type
i
. -
An inline declaration
DATA(var)
. The declared variable has the data typei
.
If FIRST OCCURRENCE
is used, the result is the length of the first occurrence.
If the search is not successful, mlen
retains its previous value or remains initial.
Example
Determination of the first string of digits within a string, and its output.
FIND REGEX '\d+' IN 'abc123def'
MATCH OFFSET DATA(moff)
MATCH LENGTH DATA(mlen).
cl_demo_output=>display( substring( val = 'abc123def'
off = moff
len = mlen ) ).
Addition 5
... RESULTS result_tab|result_wa
Effect
If the search pattern pattern
is found in the search range, the RESULTS
addition saves the offsets of the occurrences, the lengths of the substrings found, and information
about the registers for the subgroups of regular expressions either in an internal table result_tab
or in a structure result_wa
. The internal table or structure can be specified as follows:
-
An existing internal table with the table type MATCH_RESULT_TAB with the
row type MATCH_RESULT from ABAP Dictionary is specified for
result_tab
. A structure of the type MATCH_RESULT from ABAP Dictionary is specified forresult_wa
-
An inline declaration
DATA(var)
is specified afterRESULTS
. IfALL OCCURRENCES
is used, an internal table of the type MATCH_RESULT_TAB is declared; ifFIRST OCCURRENCE
is used, a structure of the type MATCH_RESULT is declared.
When an internal table is used, this table is initialized before the search starts. When the search
runs, a row is added to the internal table for each match found. If a structure is used, it is assigned
the values of the last occurrence. If FIRST OCCURRENCE
is used and an internal table is specified, precisely one row is inserted in this table if the search is successful.
The row type or structure type MATCH_RESULT has the following components:
- OFFSET of type INT4 for storing the offset of the substring
- LENGTH of type INT4 for storing the length of the substring
-
SUBMATCHES from table type SUBMATCH_RESULT_TAB with the row type
SUBMATCH_RESULT for storing the offset and length of the substring of the current occurrence, which are saved in the
subgroup registers of a regular expression
The rows from result_tab
are sorted according to the OFFSET and LENGTH columns.
An additional component LINE is only relevant for the FIND IN TABLE
variant.
If a search is not successful, the contents of the result_tab
internal table are empty, whereas the result_wa
structure retains its contents.
Note
The RESULTS
addition is particularly useful when used in conjunction with
the ALL OCCURRENCES
addition when an internal table is used, and in conjunction with the FIRST OCCURRENCE
addition when a structure is used.
Example
The following search for a regular expression finds the two substrings "ab" and "ba" at offset 0 and
offset 2, and fills the internal table result_tab
with two corresponding
values. Since the regular expression contains three subgroups, the submatches
component contains three lines. The first line of submatches
relates to the
outer parenthesis, the second line relates to the first inner parenthesis, and the third line relates
to the second inner parenthesis. The first and second lines contain the offset and length of the first
occurrence, and the third line remains undefined. The first and third lines contain the offset and length of the second occurrence, and the second line remains undefined.
DATA: result_tab TYPE match_result_tab.
FIND ALL OCCURRENCES OF REGEX `((ab)|(ba))`
IN 'abba'
RESULTS result_tab.
Addition 6
... SUBMATCHES s1 s2 ...
Effect
This addition can be used only when a regular
expression is used in pattern
. The current contents of the
registry of the subgroups of the regular expression
are written to s1
, s2
, ... for the current occurrence. The operands can be specified as follows:
- As existing variables that expect a character-like type.
-
As an inline declaration
DATA(var)
, where a variable of typestring
is declared.
When ALL OCCURRENCES
is used, the last occurrence is evaluated. If there
are more operands s1
, s2
, ... than subgroups,
the surplus variables of fixed length are filled with blanks and strings are initialized. If there are
fewer operands s1
, s2
, ... than subgroups, the surplus subgroups are ignored.
Example
The regular expression after REGEX
has two subgroups. The search finds the substring at offset 0 with length 14. The contents of the registry for the subgroups are "Hey" and "my".
DATA text TYPE string.
text = `Hey hey, my my, Rock and roll can never die`.
FIND REGEX `(\w+)\W+\1\W+(\w+)\W+\2`
IN text
IGNORING CASE
MATCH OFFSET DATA(moff)
MATCH LENGTH DATA(mlen)
SUBMATCHES DATA(s1) DATA(s2).