Application Development and Automation 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: 
Read only

RegEx ?

former_member194669
Active Contributor
0 Likes
651

All,

I need to remove trailing zeros in numbers. Given the value is held in a decimal variable called "duration", how do I achieve the following when outputting to String:


"Duration" value             Required Output
1.550                --------->     1.55
1.5000              --------->     1.5
2.0000              --------->     2
0.0100              --------->     0.01

I am doing this for PHP scripting

a®

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
611

Not 100% clear from the question if you wanted to do this with RegEx in PHP or just within ABAP... for the latter, there are probably several ways but this is the first that leapt to mind:


report zlocal_jc_sdn_regex.

data:
  g_number              type p decimals 4.

start-of-selection.
*1.550 ---------> 1.55
  g_number = '1.550'.
  perform fix_it.

*1.5000 ---------> 1.5
  g_number = '1.5000'.
  perform fix_it.

*2.0000 ---------> 2
  g_number = '2.0000'.
  perform fix_it.

*0.0100 ---------> 0.01
  g_number = '0.01'.
  perform fix_it.

*&---------------------------------------------------------------------*
*&      Form  fix_it
*&---------------------------------------------------------------------*
form fix_it.

  data:
    l_char(20)          type c.

  write: g_number to l_char right-justified.
  shift l_char right deleting trailing '0 0 0 0 0 0 0 0 0 0 0 0 0 0 0'.
  shift l_char right deleting trailing '.'. "for NNN.0000
  shift l_char left  deleting leading  space.

  write: / g_number, '--------->', l_char.

endform.                    "fix_it

which gives:

                               
      1.5500           > 1.55  
      1.5000           > 1.5   
      2.0000           > 2     
      0.0100           > 0.01  

Jonathan

4 REPLIES 4
Read only

Former Member
0 Likes
612

Not 100% clear from the question if you wanted to do this with RegEx in PHP or just within ABAP... for the latter, there are probably several ways but this is the first that leapt to mind:


report zlocal_jc_sdn_regex.

data:
  g_number              type p decimals 4.

start-of-selection.
*1.550 ---------> 1.55
  g_number = '1.550'.
  perform fix_it.

*1.5000 ---------> 1.5
  g_number = '1.5000'.
  perform fix_it.

*2.0000 ---------> 2
  g_number = '2.0000'.
  perform fix_it.

*0.0100 ---------> 0.01
  g_number = '0.01'.
  perform fix_it.

*&---------------------------------------------------------------------*
*&      Form  fix_it
*&---------------------------------------------------------------------*
form fix_it.

  data:
    l_char(20)          type c.

  write: g_number to l_char right-justified.
  shift l_char right deleting trailing '0 0 0 0 0 0 0 0 0 0 0 0 0 0 0'.
  shift l_char right deleting trailing '.'. "for NNN.0000
  shift l_char left  deleting leading  space.

  write: / g_number, '--------->', l_char.

endform.                    "fix_it

which gives:

                               
      1.5500           > 1.55  
      1.5000           > 1.5   
      2.0000           > 2     
      0.0100           > 0.01  

Jonathan

Read only

0 Likes
611

Jonathan,

Thanks for your reply. I wanted to do this for PHP scripting. I use several RegEx expressions like s/0*(+)/$1/g but nothing got the result.

I am use class CL_ABAP_REGEX & MATCHER for this

a®

Read only

0 Likes
611

I'm on an ECC5 system at the moment so those classes don't exist... but that didn't stop me trying!... I used http://www.solmetra.com/scripts/regex/index.php to test various patterns out and this appeared to be close:


"(\.0000\t?)|([0\t]+?)$"

with the various options generated calls like


preg_replace('"(\.0000\t?)|([0\t]+?)$"', '', '1.5500');

returned results like


string(4) "1.55"

... but this is the first time I've ever tried a RegEx so may have missed the point (give me ABAP any day!)...

Jonathan

Read only

0 Likes
611

Thanks Jonathan,

The link you provided help me lot http://www.solmetra.com/scripts/regex/index.php

Here is my RegEx


    move '(?:(.d*[1-9])|.)0*$' to p_regex.
    condense p_regex.
    create object regex
      exporting
        pattern     = p_regex
        ignore_case = ''.
* For REGEX match
    matcher = cl_abap_matcher=>create(
                   pattern     = p_regex
                   ignore_case = ' '
                   table       = i_files ).

    lt_result = matcher->replace_all( ).

Regarding sub matches i am not worrying about that currently.

a®