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

Occurrence for a pattern

former_member188001
Active Participant
0 Kudos
1,715

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.

11 REPLIES 11
Read only

Sandra_Rossi
Active Contributor
0 Kudos
1,606

What did you find already, just to help us help you?

Read only

Sandra_Rossi
Active Contributor
0 Kudos
1,606

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?

Read only

former_member188001
Active Participant
0 Kudos
1,606

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 (

Read only

Sandra_Rossi
Active Contributor
0 Kudos
1,606

ok thank you

trying to find a way to read the values between ) and (

Use FIND.

ABAP Keyword Documentation (sap.com)

Read only

xiaosanyu
Participant
1,606
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.

Read only

0 Kudos
1,606

If string contains letters,you can change the regex to

DATA(regex) = '\(\w+\)(\w+)\(\w+\)(\w+)\(\w+\)(\w+)'.
Read only

1,606

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.

Read only

adityaIngale
Active Participant
0 Kudos
1,606

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

Read only

matt
Active Contributor
0 Kudos
1,606

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.

Read only

former_member188001
Active Participant
1,606

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).
Read only

0 Kudos
1,606

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.