... {[SUBSTRING] sub_string} | {REGEX regex} ... .
1. ... [SUBSTRING] sub_string.
2. ... REGEX regex.
: Definition of a search string for the statements
FIND and FIND IN TABLE.
The search can either search for a substring sub_string or for a regular expression regex.
: The statements REPLACE
and REPLACE IN TABLE use the same search string.
... [SUBSTRING] sub_string.
: In this variant, the program searches for the exact instance of a substring specified in a byte-type
or character-type data object sub_string. The specification of SUBSTRING is optional.
If sub_string is either an empty string or is a string
of type c, d,
n, or t
and only contains blank characters, the program searches 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 closing blanks are not taken into account for sub_string data objects of fixed length.
: If you want to take closing blanks into account in the substring, sub_string must have the data type string.
: Search for all occurrences of the string "now" in a 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.
: Search for all occurrences of the string "now" in a string literal using a WHILE loop. After every successful search, the search range is redefined to start after the found location. This enables you to find all occurrences of the search string even in releases before 7.0.
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.
... REGEX regex.
: In this variant, the program searches for a match with a regular expression specified in regex. For regex, either a character-type data object that contains a valid regular expression when the statement is executed, or an object reference variable that refers to an instance of the class CL_ABAP_REGEX.
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.
: The following search finds the substring 'ababb' after offset 3. In accordance with the "leftmost-longest" rule, the other matching substring 'babboo' after offset 4, is not found.
DATA: moff TYPE i,
mlen TYPE i.
FIND REGEX 'a.|[ab]+|b.*' IN 'oooababboo'
MATCH OFFSET moff
MATCH LENGTH mlen.