ABAP regular expression verification of Excel data

Time:2021-5-4
FUNCTION ZTEST_XIAOY_CHECKTF.

*"----------------------------------------------------------------------

*"*" local interface:

*"  IMPORTING

*"    REFERENCE(IV_TABNAME) TYPE  TABNAME

*"  TABLES

*"      IT_DATA

*"      ET_MESSAGE STRUCTURE  ZSHR_MESSAGE

*"----------------------------------------------------------------------

DATA:    dyn_table    TYPE REF TO data,

              ”gs_display  type ref to data,

              lo_descr    type ref to cl_abap_structdescr,

              lt_fields    type ddfields,

              lv_zz        type string,

              ls_fields    like line of lt_fields,

              ls_message  type zshr_message,

              lv_text      type string,

              lv_line      type char20,

              dyn_wa      TYPE REF TO data.

FIELD-SYMBOLS: <dyn_table>  TYPE table,

              <dyn_wa>    TYPE ANY.

*IV_ Tabname is the structure or table defined by Excel se11

CREATE DATA dyn_table TYPE TABLE OF (IV_TABNAME).

ASSIGN dyn_table->* TO <dyn_table>.

CREATE DATA dyn_wa LIKE LINE OF <dyn_table>.

ASSIGN dyn_wa->* TO <dyn_wa>.

*<dyn_table> = IT_DATA[].

ASSIGN IT_DATA[] to <dyn_table>.

*The following is a simple way to get the structure field properties, of course, just read directly from dd03l

lo_descr ?= cl_abap_typedescr=>describe_by_data( <dyn_wa> ).

lt_fields = lo_descr->get_ddic_field_list( p_langu = 'E'

                                          p_including_substructres = abap_true ).

*sort lt_fields by tabname fieldname.

loop at <dyn_table> ASSIGNING <dyn_wa>.

    lv_line = sy-tabix.

    CONDENSE lv_line.

  loop at lt_fields into ls_fields.

   ASSIGN COMPONENT ls_fields-fieldname of STRUCTURE <dyn_wa> to FIELD-SYMBOL(<field>).

    if <field> is ASSIGNED.

    case ls_fields-inttype.

      when 'D'.

        if <field> is not initial.

              replace all occurrences of '\' in lv_text with space.

              replace all occurrences of '-' in lv_text with space.

              replace all occurrences of '/' in lv_text with space.

              condense: lv_text no-gaps.

              lv_text = <field> .

              lv_zz  = '^(?:(?:(?:(?:(?:1[6-9]|[2-9]\d)(?:0[48]|[2468][048]|[13579][26])|(?:(?:16|[2468][048]|[3579][26])00)))(?:0229))|' &&

                      '(?:(?:(?:1[6-9]|[2-9]\d)\d{2})(?:(?:(?:0[13578]|1[02])31)|(?:(?:0[13-9]|1[0-2])(?:29|30))|(?:(?:0[1-9])|(?:1[0-2]))(' &&

                      '?:0[1-9]|1\d|2[0-8]))))$'.

              if cl_abap_matcher=>matches(

                  pattern = lv_zz

                  text = lv_text ) ne abap_true.

                ls_message-msgty = 'E'.

                Concatenate 'excel' LV_ Line 'field' LS_ Fields fieldname 'data is not in date format yymmdd' into LS_ message-msgtx SEPARATED BY space.

                append ls_message to ET_MESSAGE.

                “exit .

                clear:ls_message.

              endif.

              <field> = lv_text.

            endif.

        when 'N' or 'I' .

            if <field> is not initial.

              lv_text = <field> .

              replace all occurrences of ',' in lv_text with space.

              condense: lv_text no-gaps.

              if cl_abap_matcher=>matches(

                  pattern = '-?[0-9]\d*'

                  text = lv_text ) ne abap_true.

                ls_MESSAGE-msgty = 'E'.

*                concatenate  ls_ Fields fieldname 'data is not an integer type' into ls'_ MESSAGE-msgtx.

                Concatenate 'excel' LV_ Line 'field' LS_ Fields fieldname 'data is not an integer type' into ls'_ message-msgtx SEPARATED BY space.

                append ls_MESSAGE to ET_MESSAGE.

                ”exit .

                clear:ls_MESSAGE.

              endif.

              <field>  = lv_text.

            endif.

      when  'P'.

            if <field> is not initial.

              lv_text = <field> .

              replace all occurrences of ',' in lv_text with space.

              condense: lv_text no-gaps.

              if cl_abap_matcher=>matches(

*                  pattern = '^(([1-9]\d*)(\.\d{1,2})?)$|^(0\.0?([1-9]\d?))$'

                  pattern =  '^[0-9]+.?[ 0-9] * $'"Xiao Yong changes regular expression 20190422

                  text = lv_text ) ne abap_true.

                ls_message-msgty = 'E'.

*                concatenate  ls_ Ztehrdt02 fielc 'data is not' into ls' of numeric type_ message-msgtx.

                Concatenate 'excel' LV_ Line 'field' LS_ Fields fieldname 'data is not' into ls' of numeric type_ message-msgtx SEPARATED BY space.

                append ls_message to et_message.

               “ exit .

                clear:ls_message.

              endif.

              <field> = lv_text.

            endif.

        when 'C'.

*          clear:len.

            Data (len) = strlen (< field >)

            if len > ls_fields-leng.

              ls_message-msgty = 'E'.

*              concatenate  ls_ Ztehrdt02 fielc 'data length too long' into LS_ message-msgtx.

              Concatenate 'excel' LV_ Line 'field' LS_ Fields fieldname 'data length is too long' into LS_ message-msgtx SEPARATED BY space.

              append ls_message to ET_MESSAGE.clear:ls_message.

            endif.

            clear:len.

    endcase.

    endif.

  endloop.

ENDLOOP.

ENDFUNCTION.

Code snippet for testing

select * into CORRESPONDING FIELDS OF TABLE lt_mara from mara UP TO 10 ROWS.

CALL FUNCTION 'ZTEST_XIAOY_CHECKTF'

  EXPORTING

    IV_TABNAME = 'MARA'

  TABLES

    IT_DATA    = lt_mara[]

    ET_MESSAGE = ET_MESSAGE.



We can change it when we debug_ The value in Mara is used to verify the code.

Recommended Today

Large scale distributed storage system: Principle Analysis and architecture practice.pdf

Focus on “Java back end technology stack” Reply to “interview” for full interview information Distributed storage system, which stores data in multiple independent devices. Traditional network storage system uses centralized storage server to store all data. Storage server becomes the bottleneck of system performance and the focus of reliability and security, which can not meet […]