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

Extract data from a string

Former Member
0 Likes
1,818

Hi,

I need to <b>EXTRACT</b> YYYY in the string <u><</u>XX<u>></u>YYYY<u><u><u><\></u></u></u>

However, only the charachters underlined are constant, as in... <\> will be present at the end of the string, but the first <> can have varying data between it.

string examples:

<u><</u>PQRST<u>></u> YYYY <u><\></u>

<u><</u>ABC<u>></u> XXXXYYYY <u><\></u>

I need to extract the 'YYYY' or 'XXXXYYYY' or whatever else that comes after the closure of the first bracket '<*>' and the start of the second bracket '<\>'.

I tried sy-fdpos, but it only gives the START of the <*> part. What really helps me is the CLOSE of the first <> and START of the second <>.

Thanks in advance.

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
1,076

You can also use this:

while lv_string(1) ne '>'.

shift lv_string.

endwhile.

shift lv_string.

if lv_string CA '<'.

lv_result = lv_string(sy-fdpos).

endif.

lv_result will contain the values between the first > and the second <.

Regards,

Michael

6 REPLIES 6
Read only

Former Member
0 Likes
1,076

Hi,

You can split at each ">" and delete the 2 last caracters of the second data.

example :

<test>YYYY<\>

first data : <test

second data : YYYY<\ -> YYYY

Read only

Former Member
0 Likes
1,076

you can use split , that the only way to solve this

Read only

Former Member
0 Likes
1,077

You can also use this:

while lv_string(1) ne '>'.

shift lv_string.

endwhile.

shift lv_string.

if lv_string CA '<'.

lv_result = lv_string(sy-fdpos).

endif.

lv_result will contain the values between the first > and the second <.

Regards,

Michael

Read only

0 Likes
1,076

Thanks a bunch for your replies mates,

Absolute peach of a logic there by Michael, was best suited for my smartform. That's the one I used.

All rewarded

Read only

Former Member
0 Likes
1,076

Hi

<XX>YYYY<\>, as you told in this string </> will be in the last.

data: a type i,

str = <XX>YYYY<\> ." (string)

a = strlen( str ).

a = a - 7. " as last is </> -


3 chars plus YYYY --- 4 chars. so 7 chars

shift str left by a places.

if it doesn't work,

do a times.

shift str left.

enddo.

now you will get str = YYYY</>.

now ,

str = str(4). " finally you will get str = YYYY.

Read only

rainer_hbenthal
Active Contributor
0 Likes
1,076

Regular expressions are the best way of doing that:



REPORT  zregex.

DATA:
  tmp(20) TYPE c,
  pattern(50) TYPE c,
  s1(10) TYPE c,
  s2(10) TYPE c,
  s3(10) TYPE c,
  s4(10) TYPE c,
  s5(10) TYPE c,
  n      TYPE i,
  it_result TYPE match_result_tab.


tmp = '<ABC> XXXXYYYY <>'.
pattern = '.*(<.*>)(.*)(<>).*'.


FIND REGEX pattern
  IN tmp
  MATCH COUNT n
  RESULTS it_result
  SUBMATCHES s1 s2 s3 s4 s5.

write:/ s1, s2, s3.

Message was edited by:

Rainer Hübenthal