2023 Aug 09 5:19 PM
Hi,
I am trying to find a solution to break this string into 3 variables
(240)1007712(10)0001212585(21)10
Var1 = 1007712
Var2 = 0001212585
Var3 = 10
Appreciate for your help.
2023 Aug 09 6:59 PM
2023 Aug 09 7:00 PM
I guess it's not what you are asking?
data(text) = '(240)1007712(10)0001212585(21)10'.
data(var1) = substring( val = text off = 5 len = 7 ).
...
Maybe you could tell us what the rules are?
2023 Aug 09 8:04 PM
Var1 = 1007712 -> This is not fixed. This can vary
Var2 = 0001212585 -> This is not fixed. This can vary
That is the reason i cant put an offset. I am trying to find a way to read the values between ) and (
2023 Aug 09 8:49 PM
ok thank you
trying to find a way to read the values between ) and (
Use FIND.
2023 Aug 10 2:32 AM
DATA(text) = '(240)1007712(10)0001212585(21)10'.
DATA(regex) = '\(\d+\)(\d+)\(\d+\)(\d+)\(\d+\)(\d+)'.
DATA(matcher) = cl_abap_regex=>create_pcre( pattern = regex
)->create_matcher( text = text ).
IF matcher->match( ).
DATA(lv_str1) = matcher->get_submatch( 1 ).
DATA(lv_str2) = matcher->get_submatch( 2 ).
DATA(lv_str3) = matcher->get_submatch( 3 ).
cl_demo_output=>display( |str1:{ lv_str1 } str2:{ lv_str2 } str3:{ lv_str3 }| ).
ENDIF.
2023 Aug 10 2:38 AM
If string contains letters,you can change the regex to
DATA(regex) = '\(\w+\)(\w+)\(\w+\)(\w+)\(\w+\)(\w+)'.
2023 Aug 10 7:58 AM
BIG WARNING: PCRE exists only since ABAP 7.55
Before 7.55, using REGEX POSIX is highly NOT recommended due to performance, failures at runtime depending on input string, etc., classic FIND stuff is recommended.
2023 Aug 10 8:56 AM
Hi salil.vaidya4,
More generically, we can do something like this.
DATA: lt_result TYPE TABLE OF string.
DATA(lv_string) = '(240)1007712(10)0001212585(21)10'.
REPLACE ALL OCCURRENCES OF PCRE '\(\d+\)' IN lv_string WITH '-'.
DO.
SPLIT lv_string AT '-' INTO DATA(lv_var1) DATA(lv_var2).
APPEND lv_var1 TO lt_result.
lv_string = lv_var2.
IF lv_var2 IS INITIAL.
EXIT.
ENDIF.
ENDDO.
cl_demo_output=>display( lt_result ).
Regards,
Aditya
2023 Aug 10 9:57 AM
I put this question through chat-gpt
Write me a clean ABAP OO program that will parse a string like this: (240)1007712(10)0001212585(21)10 The (n*) are the delimiter.Do not use REGEX.
It proved incapable.
2023 Aug 10 3:38 PM
Thanks all for your help and comments. Unfortunately we are on 750, so PCRE is not available.
I took some suggestion from the answers above and wrote the following. This seems to work. Please let me know if you see any issues here.
text = (240)1007712(10)0001212585(21)10
replace ALL OCCURRENCES OF '(' in text WITH '-'.
replace ALL OCCURRENCES OF ')' in text WITH '-'.
SPLIT text AT '-' INTO DATA(lv_var1) DATA(lv_var2) DATA(lv_var3) DATA(lv_var4) DATA(lv_var5) DATA(lv_var6).
2023 Aug 10 8:06 PM
Just a comment about the way of writing the answer, variable numbers are confusing, compared to the question, I would write:
text = '(240)1007712(10)0001212585(21)10'.
replace ALL OCCURRENCES OF '(' in text WITH '-'.
replace ALL OCCURRENCES OF ')' in text WITH '-'.
ASSERT text = '-240-1007712-10-0001212585-21-10'.
SPLIT text AT '-' INTO DATA(dummy1) DATA(dummy2) DATA(Var1) DATA(dummy3) DATA(Var2) DATA(dummy4) DATA(Var3).
ASSERT Var1 = '1007712'.
ASSERT Var2 = '0001212585'.
ASSERT Var3 = '10'.
ASSERT dummy1 = ''.
ASSERT dummy2 = '240'.
ASSERT dummy3 = '10'.
ASSERT dummy4 = '21'.
ASSERT "does nothing" but aborting if the condition is false, so it's just for the demo and ease of reading.
Disclaimer: code above not tested.