cancel
Showing results for 
Search instead for 
Did you mean: 

Read 4 digit using REPLACE REGEX '^.*[^\d](\d*)$' IN ls_num WITH '$1'.

0 Kudos

My requirement is to read 4 digit number in below String by using REGEX and I am able to read 1925 but I am also getting 2017 and 0001 and I want to avoid 0001 because total string is not 4 digit.

String : /DEV/NAN DE RANCE SA 1925 DU 15.11.2017 TR0478989117T0001 INBOUND MONEY TRANSFER

Below code for your reference

           DO ls_len TIMES.
              CLEAR:ls_num.
              ls_off = sy-index - 1.
              ls_num = gs_data-payadd_info.
              REPLACE REGEX '^(\d+).*$' IN SECTION OFFSET ls_off OF ls_num WITH '$1'.
              IF sy-subrc = 0.
                REPLACE REGEX '^.*[^\d](\d*)$' IN ls_num WITH '$1'.
                lv_length = strlen( ls_num ).
                IF lv_length = '4'.
                  FIND FIRST OCCURRENCE OF ls_num IN gs_data-payadd_info RESULTS ls_end_pos.
                  lv_length = ls_end_pos-offset - lv_start_pos.
                  EXIT.
                ENDIF.
              ENDIF.
            ENDDO.
Sandra_Rossi
Active Contributor
0 Kudos

Why do you say you shouldn't get 0001, it corresponds to the rule you defined i.e. 4 digits!

Why do you say "but I am also getting 2017" as if you don't want 2017?

Why not any sequence of 4 digits in "0478989117"?

Ok Rossi Don't get angry...😊

what I am trying to fetch is 4 Digit number and 1925 and 2017 is fine because 2017 is fallowed by a DOT. but 0001 is fallowed by letter T which I want to avoid. So now tell me if there is any feasibility in code to do that.

Second if we can avoid both 2017 and 0001 is also fine with me.

Sandra_Rossi
Active Contributor

I didn't 😉

You can search not-a-digit-or-latin-letter followed by four digits followed by not-a-digit-or-latin-letter maybe?

With POSIX REGEX, one solution is:

FIND REGEX '[^\d\w](\d{4})[^\d\w]' IN gs_data-payadd_info SUBMATCHES DATA(four_digits).

HI Rossi

Thanks for the help, this works for me.

Again my bad actually I don't want to hardcode the value 4 this will come from some table. Below code working for me without hardcode.

 Data: lv_leng type string.
       lv_leng = ls_zfi-start_length.
       CONCATENATE '[^\d\w](\d{' lv_leng '})[^\d\w]' into DATA(lv_regx).
       FIND REGEX lv_regx IN gs_data-payadd_info SUBMATCHES DATA(lv_val).

Accepted Solutions (0)

Answers (0)