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 → Expressions and Functions for String Processing → String Functions → Examples of String Functions →String Functions, count, find, and match
The example demonstrates the string functions count, find, and match.
Source Code
REPORT demo_find_and_match.
CLASS demo DEFINITION.
PUBLIC SECTION.
CLASS-METHODS main.
ENDCLASS.
CLASS demo IMPLEMENTATION.
METHOD main.
DATA: text TYPE c LENGTH 120
VALUE `Cathy's cat with the hat sat on Matt's mat.`,
regx TYPE c LENGTH 120
VALUE `\<.at\>`.
DATA: result TYPE i,
substr TYPE string.
data out TYPE c LENGTH 120.
cl_demo_input=>add_field( CHANGING field = text ).
cl_demo_input=>request( CHANGING field = regx ).
cl_demo_output=>write( text ).
result = count( val = text
regex = regx ).
DO result TIMES.
result = find( val = text
regex = regx
occ = sy-index ).
substr = match( val = text
regex = regx
occ = sy-index ).
data(len) = strlen( substr ).
out+result(len) = substr.
ENDDO.
cl_demo_output=>display( out ).
ENDMETHOD.
ENDCLASS.
START-OF-SELECTION.
demo=>main( ).
Description
In the text field text all the occurrences are searched for (using count and find) that correspond to a regular expression. When a search is successful, the subfield found is read out and displayed with the help of the match function.
Instead of using the count function, you could also use an unlimited DO loop that is left using EXIT if the result of find has the value -1.