LOGIK

Convert 2[3[a]b] into aaabaaab

This article demonstrates how to step-by-step expand nested bracket strings like 2[3[a]b] into their fully expanded target string in ABAP. The solution combines regular expressions, string processing, and ABAP Unit Tests into a transparent, robust approach.

Problem Statement and Implementation Strategy

The central challenge lies in correctly identifying nested bracket structures prefixed with repetition factors and resolving them step-by-step from the inside out. The decision was made to use an iterative approach with a regular expression that captures the innermost level in each iteration and executes its multiplication. The used RegEx (\d{0,})\[([^\[\]]+)\] finds the deepest non-nested bracket combination, allowing a repeated reduction of the string down to its fully expanded form.

Solution

The implementation is encapsulated within a standalone ABAP class. The algorithm operates inside a DO loop that executes until no further patterns are detected. In each cycle, match identifies the innermost bracket level, substring_before and substring_after extract the repetition factor as well as the inner payload, and replace combines both utilizing the repeat function to construct the expanded sequence. An additional Unit Test class ensures that the logic continues to perform reliably during future modifications.

Show Solution (17 lines)
METHOD convert_bracketstring.
  rv_value = iv_value.
  DO.
    DATA(lv_match) = match( val = rv_value regex = '(\d{0,})\[([^\[\]]+)\]' ).
    IF lv_match IS INITIAL.
      EXIT.
    ENDIF.
    DATA(lv_repeat_times) = substring_before( val = lv_match sub = '[' ).
    rv_value = replace( val = rv_value
                        regex = '(\d{0,})\[([^\[\]]+)\]'
                        with = repeat( val = substring_before( val = substring_after( val = lv_match
                                                                                      sub = '[' )
                                                               sub = ']' )
                                       occ = CONV i( substring_before( val = lv_match
                                                                       sub = '[' ) ) ) ).
  ENDDO.
ENDMETHOD.

Unit Test

The Unit Test class contains three test methods covering varying complexity levels of the bracket structure: simple flat blocks (test_get_value_normal), a two-level nesting (test_get_value_nested), and a deeply nested multi-level combination (test_get_value_deepnested). This guarantees that the algorithm expands correctly for both straightforward and complex nested inputs.

Show Unit Test (14 lines)
METHOD test_get_value_normal.
  cl_abap_unit_assert=>assert_equals( act = zcl_kco_bracketstr2str=>convert_bracketstring( '1[a]2[b]3[c]4[d]' )
  exp = 'abbcccdddd' ).
ENDMETHOD.

METHOD test_get_value_nested.
  cl_abap_unit_assert=>assert_equals( act = zcl_kco_bracketstr2str=>convert_bracketstring( '2[3[a]b]' )
  exp = 'aaabaaab' ).
ENDMETHOD.

METHOD test_get_value_deepnested.
  cl_abap_unit_assert=>assert_equals( act = zcl_kco_bracketstr2str=>convert_bracketstring( '1[2[3[c]a]4[a]b]' )
  exp = 'cccacccaaaaab' ).
ENDMETHOD.

Unit Test Result

All three test methods execute successfully. The code coverage measurement indicates a full 100% coverage for the CONVERT_BRACKETSTRING method. This means that every single execution branch of the implementation is traversed by at least one test case. A coverage value below 100% would indicate that specific execution paths – such as edge cases without brackets or empty inputs – are not yet covered and would require additional test methods.

Figure 1: Successful ABAP Unit Test execution confirming a verified code coverage of 100%.

It is also clearly visible that 100% of the method is executed during the coverage measurement. If 100% is not reached in your scenario, you might consider implementing additional test methods to achieve full coverage.

Download Source Code on GitHub

Select the appropriate variant matching your SAP release version.

Minimum Release GitHub
SAP_BASIS 700 Open Repository
SAP_BASIS 702 Open Repository
SAP_BASIS 740 Open Repository
SAP_BASIS 740SP08 Open Repository

Import GitHub Repo via abapGit

You can also import the source code directly into your system using abapGit. You can find a step-by-step guide for importing from a GitHub repository in the linked blog post.

Open instructions for importing with abapGit

Repository URL zum Kopieren

https://github.com/stekoester/convert_bracketstring_2_string

Back to Blog Overview