2008 Apr 04 9:45 AM
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.
2008 Apr 04 9:48 AM
data val(20) type c.
val = '20 30 65 84 999'.
condense val no-gaps.
write val.
Reward if usefull
2008 Apr 04 9:50 AM
Sorry if I was not clear I was not looking for a specific series of numeric digits but and series of numeric digits.
Thanks
2012 Apr 24 9:56 AM
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.
2012 Apr 24 10:22 AM
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
2012 Apr 24 12:12 PM
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.