Application Development and Automation Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

Pattern recognition in a given string

venkatesha_n
Product and Topic Expert
Product and Topic Expert
0 Likes
4,160

Hi Experts,

I have one problem in finding a repeated pattern in a given string.

For example: if the given string lv_string = '345723345982343452343345'.

you can check the lv_string, which contains '345' repeatedly in it.

so, i want a ABAP code to get list of all such type of patterns repeated in a given string.

Thanks in advance.

Venky.

1 ACCEPTED SOLUTION
Read only

kesavadas_thekkillath
Active Contributor
0 Likes
3,704

Vishnu,

The code is readily available in F1 help FIND - pattern

43 REPLIES 43
Read only

Former Member
0 Likes
359
REPORT z_test.
*--------------------------------------------------------------------*
TYPES: BEGIN OF ty_result_out,
         search_string TYPE string,
         offset        TYPE match_result-offset,
         length        TYPE match_result-length,
       END OF ty_result_out.
DATA: tp_string        TYPE string VALUE '1121112211',
      tp_string_copy   TYPE string,
      tp_real_offset   TYPE i,
      ltp_strlen       TYPE i,
      wa_search_str    TYPE string,
      wa_result        TYPE match_result,
      tp_dummy         TYPE string,
      ta_result_out    TYPE TABLE OF ty_result_out,
      wa_result_out    TYPE ty_result_out,
      ta_searchstrings TYPE STANDARD TABLE OF string,
      wa_searchstring  TYPE string,
      ltp_size         TYPE i,
      ltp_pos          TYPE i,
      ltp_check        TYPE i.
* References For ALV
DATA: rf_table        TYPE REF TO cl_salv_table,
      rf_functions    TYPE REF TO cl_salv_functions_list,
      rf_columns      TYPE REF TO cl_salv_columns_table,
      rf_column       TYPE REF TO cl_salv_column_table,
      rf_display      TYPE REF TO cl_salv_display_settings,
      tp_title        TYPE lvc_title.
* Determine the length of the string:
ltp_strlen = STRLEN( tp_string ).
* Get all possible search strings
DO ltp_strlen TIMES.
  ltp_size = sy-index.
  DO ltp_strlen TIMES.
    ltp_pos = sy-index - 1.
    ltp_check = ltp_pos + ltp_size.
    IF  ltp_check > ltp_strlen.
      EXIT.
    ELSE.
      wa_searchstring = tp_string+ltp_pos(ltp_size).
      APPEND wa_searchstring TO ta_searchstrings.
    ENDIF.
  ENDDO.
ENDDO.
SORT ta_searchstrings. DELETE ADJACENT DUPLICATES FROM ta_searchstrings.
TRY.
* Search for each possible search string:
  LOOP AT ta_searchstrings INTO wa_search_str.
    CLEAR: tp_real_offset.
    tp_string_copy = tp_string.
    DO.
      FIND FIRST OCCURRENCE OF wa_search_str IN tp_string_copy RESULTS wa_result.
      IF sy-subrc <> 0.
        EXIT.
      ENDIF.
      tp_real_offset = tp_real_offset + wa_result-offset.
      MOVE: tp_real_offset   TO wa_result_out-offset,
            wa_search_str    TO wa_result_out-search_string,
            wa_result-length TO wa_result_out-length.
      APPEND wa_result_out TO ta_result_out.
      SPLIT tp_string_copy AT wa_search_str INTO tp_dummy tp_string_copy.
      CONCATENATE wa_search_str tp_string_copy INTO tp_string_copy.
      SHIFT tp_string_copy LEFT.
      tp_real_offset = tp_real_offset + 1.
    ENDDO.
  ENDLOOP.
ENDTRY.
SORT ta_result_out BY length search_string offset.
DELETE ADJACENT DUPLICATES FROM ta_result_out COMPARING search_string offset.
PERFORM alv_grid_display.
*--------------------------------------------------------------------*
FORM alv_grid_display.
*--------------------------------------------------------------------*
  CLEAR : rf_table.
  TRY.
      CALL METHOD cl_salv_table=>factory
        EXPORTING
          list_display = if_salv_c_bool_sap=>false
        IMPORTING
          r_salv_table = rf_table
        CHANGING
          t_table      = ta_result_out.
    CATCH cx_salv_msg .
  ENDTRY.
  IF rf_table IS INITIAL.
    MESSAGE 'Error Creating ALV Grid ' TYPE 'I' DISPLAY LIKE 'E'.
    STOP.
  ENDIF.
  rf_functions = rf_table->get_functions( ).
  rf_functions->set_all( if_salv_c_bool_sap=>true ).
  rf_functions->set_view_lotus( ' ' ).
  rf_functions->set_view_excel( ' ' ).
  rf_functions->set_graphics( ' ' ).
  CLEAR: rf_display.
  MOVE:  tp_string TO tp_title.
  rf_display = rf_table->get_display_settings( ).             
  rf_display->set_striped_pattern( if_salv_c_bool_sap=>true ).  
  rf_display->set_list_header( tp_title ).                 
  rf_columns = rf_table->get_columns( ).
  IF rf_columns IS NOT INITIAL.
    TRY.
        rf_column ?= rf_columns->get_column( 'SEARCH_STRING' ).
        CALL METHOD rf_column->set_medium_text
          EXPORTING
            value = 'Searched for:'.
        CALL METHOD rf_column->set_alignment
          EXPORTING
            value = if_salv_c_alignment=>right.
      CATCH cx_salv_not_found.
      CATCH cx_salv_existing.
      CATCH cx_salv_data_error.
    ENDTRY.
    TRY.
        rf_column ?= rf_columns->get_column( 'OFFSET' ).
        CALL METHOD rf_column->set_medium_text
          EXPORTING
            value = 'At offset:'.
      CATCH cx_salv_not_found.
      CATCH cx_salv_existing.
      CATCH cx_salv_data_error.
    ENDTRY.
    TRY.
        rf_column ?= rf_columns->get_column( 'LENGTH' ).
        CALL METHOD rf_column->set_medium_text
          EXPORTING
            value = 'Length:'.
      CATCH cx_salv_not_found.
      CATCH cx_salv_existing.
      CATCH cx_salv_data_error.
    ENDTRY.
    rf_columns->set_optimize( if_salv_c_bool_sap=>true ).
    rf_columns->set_key_fixation( if_salv_c_bool_sap=>true ).
  ENDIF.
  CALL METHOD rf_table->display.
ENDFORM.
Read only

0 Likes
359

The above report takes into account overlapping strings and searches for all possible strings (parts).

I think it works, at least it seems to work for the example string I used (1121112211)...

Quite an interesting puzzle...

P.S. I did not check on repetitions, just for all occurrences, including overlap. Repetitions would be the number found minus 1.

Read only

0 Likes
359

So what about string '2222'?

The program returns:

Searched for   At offsets   Length
2	       0,1,2,3	    1
22	       0,1,2	    2
222	       0,1	    3

Read only

0 Likes
359

I think that that's what he wants to do. My point is that I am not sure it is what the user would want.

Rob