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: 

How to use a regural expression to get all digit from a string.

Former Member
0 Kudos

Hi All,

Do you know how to use regural expression to get all digits from the following string via ABAP program?

"'Log Attributes 0 ( 0 ) ( 10 % Available )"

Thanks,

Andrew

1 ACCEPTED SOLUTION

former_member387317
Active Contributor
0 Kudos

Hi Andrew Liu,

PARAMETERS: p_string(50) TYPE C DEFAULT 'Log Attributes 0 ( 0 ) ( 43 % Available )'.

DATA : v_num(20) TYPE N.

DATA: v_int TYPE i.

v_num = p_string.

v_int = v_num.

WRITE 'All digit at once'.

SKIP.

WRITE : v_num.
SKIP.
WRITE : v_int.


DATA: var1(20) TYPE c,
      var2(20) TYPE c,
      var3(20) TYPE c.

SPLIT p_string at '(' INTO var1 var2 var3.

SKIP.
WRITE '*******************************'.

WRITE : / 'seperate digits'.

SKIP.

v_num = var1.

v_int = v_num.

WRITE : v_num.
SKIP.
WRITE : v_int.
SKIP.


v_num = var2.

v_int = v_num.

WRITE : v_num.
SKIP.
WRITE : v_int.
SKIP.



v_num = var3.

v_int = v_num.

WRITE : v_num.
SKIP.
WRITE : v_int.
SKIP.

if you want digits in separted form then also you can... if you have specific char where u can split your expression... in above case i have seperated it at (.

Hope it will solve your problem..

Thanks & Regards

ilesh 24x7

ilesh Nandaniya

18 REPLIES 18

Former Member
0 Kudos

Hi,

Declare the field of data type N.

and move the string to field..only intergers get moved to the field.

DATA : l_num(20) TYPE N.
DATA : l_char(20) TYPE C VALUE 'A1V2I3N4A5S6H7'.

l_num = l_char.

L_num contains '1234567'.

Former Member
0 Kudos

Hi,

Try the code mentioned below:


  DATA: STR_LEN  LIKE SY-FDPOS,
        RSTR_LEN LIKE SY-FDPOS,
        OFF      LIKE SY-FDPOS.
  DATA: IDX      LIKE SY-FDPOS,        "mn B20K054003
        CL       LIKE SY-FDPOS.        "mn B20K054003
  DATA: RSTRING(40).
  DATA: STRING(40).   " value 'A,N,I,L'.
  FIELD-SYMBOLS: <NLS_CHAR>.           "mn B20K054003

 MOVE I_REGUH-ZNME1 TO STRING.

  MOVE SPACE TO RSTRING.
  STR_LEN = STRLEN( STRING ).
  DESCRIBE FIELD RSTRING LENGTH RSTR_LEN.
  IF RSTR_LEN < STR_LEN. RAISE TOO_SMALL. ENDIF.

  WHILE IDX < STR_LEN.                 "mn B20K054003
    ASSIGN STRING+IDX(*) TO <NLS_CHAR>.   "mn B20K054003
    IF SY-LANGU EQ '2'.                "mn B20K054003
      CALL FUNCTION 'NLS_THAI_CHARLEN' "mn B20K054003
           EXPORTING                   "mn B20K054003
                THAI_STRING  = <NLS_CHAR>       "mn B20K054003
           CHANGING                    "mn B20K054003
                THAI_CHARLEN = CL.     "mn B20K054003
    ELSE.                              "mn B20K054003
      CL = CHARLEN( <NLS_CHAR> ).      "mn B20K054003
    ENDIF.                             "mn B20K054003
    IF IDX NE 0.                       "mn B20K054003
      SHIFT RSTRING RIGHT BY CL PLACES."mn B20K054003
    ENDIF.                             "mn B20K054003

    RSTRING+0(CL) = STRING+IDX(CL).    "mn B20K054003

    IDX = IDX + CL.                    "mn B20K054003
  ENDWHILE.                            "mn B20K054003

Regds,

Anil

Edited by: Matt on Jul 1, 2009 9:36 AM -added code tags

Former Member
0 Kudos

Hi,

Use as follows;


lv_str_digit = lv_str.
REPLACE ALL OCCURRENCES OF REGEX '\D' IN lv_str_digit WITH ''.

This will remove all other characters other than digits.

Regards

Karthik D

0 Kudos

Hi ,

maybe I need to get the digit 0, 0, 10 to store the relevant variable, thanks.

Best Regards, Andrew

0 Kudos

>

> Hi ,

>

> maybe I need to get the digit 0, 0, 10 to store the relevant variable, thanks.

>

> Best Regards, Andrew

Hi,

Try the below code;

data : lv_str TYPE String VALUE 'Log Attributes 0 ( 0 ) ( 10 % Available )'.
Data : lv_str_tmp TYPE string.
data : lv_num1 TYPE String,
       lv_num2 TYPE String,
       lv_num3 TYPE String.
Data : lv_len TYPE i.

lv_str_tmp = lv_str.

REPLACE ALL OCCURRENCES OF REGEX '\D+' in lv_str_tmp WITH '*'.
lv_len = strlen( lv_str_tmp ).
lv_len = lv_len - 2.
lv_str_tmp = lv_str_tmp+1(lv_len).
SPLIT lv_str_tmp AT '*' INTO lv_num1 lv_num2 lv_num3.

This will solve your problem.

Regards

Karthik D

0 Kudos

Hi,

however, if the string changed to "Log Attributes 0 ( 0 ) ( 10.5 % Available )", how to get 10.5 from this string? thanks.

Best Regards, Andrew

former_member156446
Active Contributor
0 Kudos

[how to get only digits (numbers only)|http://www.realsoftware.com/listarchives/realbasic-nug/2003-12/msg01857.html]

[The following search for a regular expression finds the two substrings "ab" at offset 0 and "ba" at offset 2, and fills the internal table result_tab with two rows accordingly. As the regular expression contains three subgroups, the component submatches contains three lines in each case. The first line of submatches refers to the outermost bracket, the second line refers to the first internal bracket, and the third line refers to the second internal bracket. For the first found location, the first and second lines contains the offset and length while the third line is undefined. For the second found location, the first and third lines contains the offset and length, while the second line is undefined.|http://help.sap.com/abapdocu/en/ABAPFIND.htm]

DATA: result_tab TYPE match_result_tab.

FIND ALL OCCURRENCES OF REGEX `((ab)|(ba))`

IN 'abba'

RESULTS result_tab.

former_member387317
Active Contributor
0 Kudos

Hi Andrew Liu,

PARAMETERS: p_string(50) TYPE C DEFAULT 'Log Attributes 0 ( 0 ) ( 43 % Available )'.

DATA : v_num(20) TYPE N.

DATA: v_int TYPE i.

v_num = p_string.

v_int = v_num.

WRITE 'All digit at once'.

SKIP.

WRITE : v_num.
SKIP.
WRITE : v_int.


DATA: var1(20) TYPE c,
      var2(20) TYPE c,
      var3(20) TYPE c.

SPLIT p_string at '(' INTO var1 var2 var3.

SKIP.
WRITE '*******************************'.

WRITE : / 'seperate digits'.

SKIP.

v_num = var1.

v_int = v_num.

WRITE : v_num.
SKIP.
WRITE : v_int.
SKIP.


v_num = var2.

v_int = v_num.

WRITE : v_num.
SKIP.
WRITE : v_int.
SKIP.



v_num = var3.

v_int = v_num.

WRITE : v_num.
SKIP.
WRITE : v_int.
SKIP.

if you want digits in separted form then also you can... if you have specific char where u can split your expression... in above case i have seperated it at (.

Hope it will solve your problem..

Thanks & Regards

ilesh 24x7

ilesh Nandaniya

0 Kudos

Hi ilesh,

In case the following string 'Log Attributes 0 ( 0 ) ( 43.56 % Available )', so the program can not get 43.56.

Best Regards,

Andrew

0 Kudos

This message was moderated.

0 Kudos

Change the '*' in REPLACE and SPLIT statements with '.' (dot) and my code will work well for 43 as well as 43.56.

Regards

Karthik D

0 Kudos

>

> In case the following string 'Log Attributes 0 ( 0 ) ( 43.56 % Available )', so the program can not get 43.56.

Change the '*' in REPLACE and SPLIT statements and my code will work well for 43 as well as 43.56.

Regards

Karthik D

0 Kudos

Hi Karthik D,

in case the string 'Log Attributes 0 ( 0.5 ) ( 43.56 % Available )', how to handle? thanks.

Best Regards,

Andrew

0 Kudos

Hi,

The below code will hold good for all three number even it is decimal or not.

data : lv_str TYPE String VALUE 'Log Attributes 0.76 ( 0.5 ) ( 43.56 % Available )'.
Data : lv_str_tmp TYPE string.
data : lv_num1 TYPE string,
       lv_num2 TYPE string,
       lv_num3 TYPE string.
Data : lv_len TYPE i.
DATA: result_tab TYPE match_result_tab.
DATA : wa_result LIKE LINE OF result_tab.

lv_str_tmp = lv_str.

FIND ALL OCCURRENCES OF REGEX '(\d+.\d*)' IN LV_STR_TMP RESULTS result_tab.
LOOP AT result_tab INTO wa_result FROM 1 TO 3.
CASE sy-tabix.
  WHEN 1.
    lv_num1 = lv_str_tmp+wa_result-offset(wa_result-length).
  WHEN 2.
    lv_num2 = lv_str_tmp+wa_result-offset(wa_result-length).
  WHEN 3.
    lv_num3 = lv_str_tmp+wa_result-offset(wa_result-length).
ENDCASE.
ENDLOOP.

This solved your problem.

Regards

Karthik D

P.S.: Don't keep on changing your requirement, give it all in the question itself.

0 Kudos

Hi Karthik D,

In case for the following string,

'Log Attributes 0.5 ( a0 ) ( 43.56 % Available )'

this program still can get 0.5, 0, 43.56, however, the actual result should be 0.5, 43.56, thanks.

Best Regards, Andrew

0 Kudos

Andrew. Please stop drip feeding the requirement. Specify it in full. You are simply wasting people's time. Same as here:

matt

Edited by: Matt on Jul 1, 2009 9:39 AM

0 Kudos

Hi,

My Final Attempt;

Change the regex to '(\s\d+.\d*)' , this will solve the problem, close the thread if this solves your latest requirement. Otherwise have a glimpse at the F1 help of REGEX to fine tune my solution as per your growing requirements.

Regards

Karthik D

former_member387317
Active Contributor
0 Kudos

Hi Andrew Liu,

I am agree with Karthik, For saving Time of us and yours both please give your requirement fully at first itself.

request you to not follow Incremental development scenario in SCN...

Thanks & Regards

ilesh 24x7

ilesh Nandaniya