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

ABAP REGEX Question

Former Member
0 Likes
1,050

Hi everybody,

I got a string like:

data: mystring type string value 'DE11111X1111DE222222222222222ZPDE33333333333333DE44444444444'.


How can I find the string from the first <DE> to the second <DE>  (= DE11111X1111) ?


I tried


data: mystring type string value 'DE1111X11111DE222222222222222ZPDE33333333333333DE44444444444'.
DATA: moff TYPE i,
       mlen TYPE i.

FIND REGEX 'DE.*DE' IN mystring  MATCH COUNT sy-tabix
      MATCH OFFSET moff
      MATCH LENGTH mlen.
write:/ mystring+moff(mlen).

But this gives:

DE1111X11111DE222222222222222ZPDE33333333333333DE

Thanks, Regards

Mario



1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
975

I found it : DE.*?DE

Hi everybody,

I got a string like:

data: mystring type string value 'DE11111X1111DE222222222222222ZPDE33333333333333DE44444444444'.


How can I find the string from the first <DE> to the second <DE>  (= DE11111X1111) ?


I tried


data: mystring type string value 'DE1111X11111DE222222222222222ZPDE33333333333333DE44444444444'.
DATA: moff TYPE i,
       mlen TYPE i.

FIND REGEX 'DE.*DE' IN mystring  MATCH COUNT sy-tabix
      MATCH OFFSET moff
      MATCH LENGTH mlen.
write:/ mystring+moff(mlen).

But this gives:

DE1111X11111DE222222222222222ZPDE33333333333333DE

Thanks, Regards

Mario



6 REPLIES 6
Read only

Former Member
0 Likes
976

I found it : DE.*?DE

Read only

0 Likes
975

DE.*?DE  dows not work in ABAP



Read only

0 Likes
975

Hi Mario

\DE Regex will provide you the postion of DE and then you can derive accordingly

Nabheet

Read only

0 Likes
975

Hi Nabheet,

sorry. I don't understand.

When I try:

FIND REGEX '\DE' IN mystring  MATCH COUNT sy-tabix
      MATCH OFFSET moff
      MATCH LENGTH mlen.


offset = 0,

length = 2.

Regards Mario

Read only

0 Likes
975

DE.*?DE does not work in ABAP because non-greedy regex syntax is not yet implemented.

It is reserved for future enhancements.

As a workaround, i am assuming the string starts with DE. Second DE occurrence can be found using sample below, which is then replaced with nothing.

  1. DATA lv_input TYPE string
  2.      VALUE 'DE11111X1111DE222222222222222ZPDE33333333333333DE44444444444'.
  3. DATA lv_output TYPE string.
  4. lv_output = lv_input.
  5. REPLACE REGEX '(.)DE.*' IN lv_output WITH '$1'.
  6. WRITE:/ lv_output.

//

Read only

0 Likes
975

Thanks to Manish,

I did it that way:

DATA lv_input TYPE string
        VALUE 'xDE11111X1111DE222222222222222ZPDE33333333333333DE44444444444'.
   DATA lv_output TYPE string.
   lv_output = lv_input.
   WRITE:/ lv_input.
   while lv_output cs 'DE'.
     REPLACE REGEX '(.)DE.*' IN lv_output WITH '$1'.
     WRITE:/ lv_output.
     REPLACE FIRST OCCURRENCE OF  lv_output IN lv_input  WITH ''.
     lv_output = lv_input.
   endwhile.



Regards

Mario