## Convert 2[3[a]b] to aaabaaab

Few days ago one of the member of our telegram group aked how to convert 2[3[a]b] to aaabaaab, 2[a]3[b]c to aabbbc and so on.

## Question and decision how to solve the task

1. How can we seperate the right opening and closing bracket including the number infront
• This can be done with the simple regex (\d{0,})[([^[]]+)]

## Solution

I implemented the logic in a class. I implemented the Unit Test to the class too, so that the logic will still work correct when making changes to it.

 `````` 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 `````` ``````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

For the Unit Test I have created three methods with different bracket combinations, from simple to very deep.

 `````` 1 2 3 4 5 6 7 8 9 10 11 12 `````` ``````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.``````

## Get the source code from GitHub

Copy the repo link `https://github.com/stekoester/convert_bracketstring_2_string`