SAP FIND PATTERN ABAP Statements

Get Example source ABAP code based on a different SAP table
  



FIND - pattern

Short Reference


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

ABAP_VARIANTS:
1 ... [SUBSTRING] substring.
2 ... REGEX regex.

What does it do? 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.



Latest notes:The statements REPLACE and
REPLACE IN TABLE use the same search string.
• SUBSTRING FIND

ABAP_VARIANT_1 ... [SUBSTRING] substring.

What does it do? 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.



Latest notes:If trailing blanks are not to be ignored in the substring,
substring must have the data type string.



Example ABAP Coding
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 ABAP Coding
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 FIND

ABAP_VARIANT_2 ... REGEX regex.

What does it do? 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.

Latest 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. Refer to Exceptions in Regular Expressions.



Example ABAP Coding
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.

Return to menu