Skip to Content

Regular Expression in ABAP

In an Upload file (Excel sheet ) I had a requirement to validate the amount ( currency ). Below were the validations to be considered for the string containing the amount

  1. The string must contains numbers and decimal dot only(optional decimal).
  2. There must be only 13 digits before decimal and 2 digits after decimal.
  3. There should not be any special characters in the string apart from decimal point.
  4. Only one decimal point should be used in the string.

To achieve this initially, I used the below logic on the sting <amount>.

  1. <amount> contains only (1234567890.)
  1. Separate the <amount> into <first_part> <second_part> <third_part> at ‘.’

   Find the length of <first_part>.

   Find the length of <second_part>.

   Check <first_part> < 13 and <second_part> < 2.

   Check <third_part> is initial.

  1. <amount> is less than 0.

This all validation takes nearly around 25 – 30 lines of code.

But, after knowing the Regular expression concept I revised this 25 – 30 lines of code into just 1 line as below.

FIND ALL OCCURRENCES OF regex ‘^([0-9]\d{0,12})(\.[0-9]{0,2})?$’ IN <amount> MATCH COUNT sy-tabix.

sy-tabix = 1 => its true/correct.

sy-tabix = 0 => its wrong/ incorrect.

Brief Explanation.

Syntax : FIND ALL OCCURRENCES OF regex <Expression> in <String> Match Count <Variable> .

Regular Expression : ^([0-9]\d{0,12})(\.[0-9]{0,2})?$

^ : Matches the starting position within the string

() : The string matched within the parentheses can be recalled later.

[] : Matches a character that is contained within the brackets.

: Specifies a range.

{} : Number of characters.

\d : Digits.

\. : Mandatory dot.

: Indicates there is zero or one of the preceding element.

$ : Matches the ending position of the string.

[0-9]       : Digits only any number between 0 to 9.

{0,12}     : Up to 13 characters allowed only before decimal.

{0,2}        : up to 2 characters allowed only after decimal.



You must be Logged on to comment or reply to a post.
  • You can avoid putting sy-tabix in write position.

    Regular expression can be simplified as below and sy-subrc = 0 means correct:

    FIND REGEX ‘^\d{0,13}\.\d{0,2}$’ IN lv_string.

  • Hi Rounak,


    I have a requirement to get values of currency from non-currency fields and validate for valid currency by adding decimal, separators. I tried using your regular expression but that does not seem to work in my case. I made a small change since XREF1 is only 12 characters I changed to {0,9} but the sy-tabix is always 0.

    Below is my code:

    DATA: lv_tabix TYPE sy-tabix.
    IF bseg-xref1 IS NOT INITIAL.
      IN bseg-xref1 WITH space.
      CONDENSE bseg-xref1.
      lv_aed_curr  = bseg-xref1.
      lv_aed_curr1 = lv_aed_curr.
      FIND ALL OCCURRENCES OF REGEX '^([0-9]\d{0,9})(\.[0-9]{0,2})?$'
      IN lv_aed_curr1 MATCH COUNT lv_tabix.
      IF  lv_tabix = 1.
    * Add separators to the acquired currency
           betrg  = lv_aed_curr1
           string = bseg-xref1.
        CONDENSE bseg-xref1.
        MESSAGE e730(zzfn) WITH 'Please enter a valid amount currency'.


    Expecting a reply soon.