Application Development 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: 

Find the number of consecutive numeric digits in string

simon_broome
Explorer
0 Kudos
1,001

I am trying to see if a string has say 9 consecutive numeric digits in it but it will only work if the long string of numbers is the first string of numbers.

e.g.

This would recongnise the following string (if p_len = 9)

Tel number 20 20 30 369 for 2nd meeting

but would not recongnise the following

For 2nd meeting tel 20 20 30 369

Could anyone give guidance on how to proceed / a better way of find consecutive numbers in a string?

Thanks

Simon

Code sample -

LOOP AT TLINETAB.

  • Remove blank spaces

CONDENSE TLINETAB-TDLINE NO-GAPS.

  • Find first number and its position *

IF TLINETAB-TDLINE CA '0123456789'.

pos = sy-fdpos.

  • Does the next x characters contain only numbers?

IF TLINETAB-TDLINE+pos(p_len) CO '0123456789'.

append i_report.

endif.

ENDIF.

ENDLOOP.

5 REPLIES 5

Former Member
0 Kudos
329

data val(20) type c.

val = '20 30 65 84 999'.

condense val no-gaps.

write val.

Reward if usefull

0 Kudos
329

Sorry if I was not clear I was not looking for a specific series of numeric digits but and series of numeric digits.

Thanks

simon_broome
Explorer
0 Kudos
329

The code I decided to use was this

* Looping through text lines

    LOOP AT TLINETAB.

      i_report-tdline = TLINETAB-TDLINE.

      i_report-tdname = i_stxh-tdname.

* Remove blank spaces

      CONDENSE TLINETAB-TDLINE NO-GAPS.

      len = strlen( tlinetab-tdline ).

* Find first number and its position

      IF TLINETAB-TDLINE CA '0123456789'.

        pos = sy-fdpos.

        posplus = pos + 1.

* Does the next x characters contain only numbers?

        IF TLINETAB-TDLINE+pos(p_len) CO '0123456789'.

          append i_report.

          CONTINUE.

        endif.

      ENDIF.

    ENDLOOP.

  endloop.

0 Kudos
329

Dear Simon,

you want to check for consecutive numbers in a string. Then you may do the following -

1. Get all the numbers in the given string into a table in a sequencial order, that is the order in which they appear in the string

2. Once you have all the numbers you may find out if the numbers are consecutive or not. The following is the code which is not tested

lv_len = 1.
lv_pos = 1.

lv_strlen = strlen ( lv_str ).

do lv_strlen times.

  lv_alpha = lv_str+lv_pos(lv_len).

  if lv_alpha co '0123456789'.

   append lv_alpha to lt_number.

  endif.

  lv_pos = lv_pos + 1.

enddo.

loop at lt_number into ls_number.

  if lv_no is initial.

   lv_no = ls_number.
   lv_no = lv_no + 1.

  else.

   if ls_number eq lv_no and
      lv_consec is initial.

    lv_consec = c_x.

   else.

    clear lv_consec.

   endif.

  endif.

endloop.

if lv_consec eq c_x.

*  !!! string has consecutive numbers

endif.

Hope it helps. Thank you.

Regards,

kartik

Private_Member_49934
Product and Topic Expert
Product and Topic Expert
0 Kudos
329

Tried using regex. Create a z-test program let me know if it works. You test example works

   PARAMETERS : p_txt TYPE text100.

START-OF-SELECTION.
  CONSTANTS : c_regex_9_digit TYPE string VALUE
  '([[:digit:]][[:blank:]]*){9}',
              c_regex_10_digit  TYPE string VALUE
  '([[:digit:]][[:blank:]]*){10}',
              c_regext_1_9_digit TYPE string  VALUE
  '[[:digit:]].*([[:digit:]][[:blank:]]*){9}'.

  FIND REGEX c_regex_9_digit IN p_txt.
  IF sy-subrc = 0.
    FIND REGEX c_regex_10_digit IN p_txt.
    IF sy-subrc = 0.
      WRITE ' Not Matched'.
    ELSE.
      FIND REGEX c_regext_1_9_digit IN p_txt.
      IF sy-subrc = 0.
        WRITE ' Not Matched'.
      ELSE.
        WRITE : 'Matched'.
      ENDIF.
    ENDIF.
  ELSE.
    WRITE : 'Not Matched'.

  ENDIF.