Quantcast
Channel: ABAP Development
Viewing all articles
Browse latest Browse all 948

A simple example of using ABAP regular expression: Should I use it at all in this case?

$
0
0

Currently I am working on the project to enable CRM system with social media integration.

We need to extract all social posts with different social channels into CRM system like twitter, facebook and sina weibo etc.

The detail about integration would be found in my blog.

 

Sina weibo is a very popular social media channel in Chinese with more than half a billion.

 

users can create weibo posts with multiple pictures uploaded from local laptops.

clipboard1.png

clipboard2.png

I found in the json string, two thumbnail pic urls are available there. However, only one original url exists. It means I need to populate the original url for the second picture based on its thumbnail url http://ww3.sinaimg.cn/thumbnail/d19bb9dfgw1ebdk3zp82mj20bi07omxx.jpg.

 

The population logic should be:

 

original picture url of picture1: http://A/B/1.jpg - url1

thubmnail url of picture2: http://C/D/2.jpg - url2

 

the calculated original url has format like <large picture url prefix got from url1>/<file name got from url2>, that is http://A/B/2.jpg

 

 

 

I compared the normal way and the way using regular expression. The normal way only uses 6 lines and the regular expression uses 11 lines.

In this very case, it seems the regular expression does not show its power. Which one do you prefer? Please kindly let me know your suggestion.

 

REPORT ZTESTREG1.

DATA: lv_origin_url1 TYPE STRING value 'http://ww2.sinaimg.cn/large/d19bb9dfgw1ebdk3zbk3rj20ch0a5jrv.jpg',

      lv_thumbnail_2 TYPE STRING value 'http://ww3.sinaimg.cn/thumbnail/d19bb9dfgw1ebdk3zp82mj20bi07omxx.jpg',

      lv_result1 TYPE string,

      lv_result2 TYPE string.

" normal solution

SPLIT lv_thumbnail_2 AT '/' INTO TABLE DATA(lt_temp).

READ TABLE lt_temp ASSIGNING FIELD-SYMBOL(<file_name>) INDEX ( lines( lt_temp ) ).

FIND ALL OCCURRENCES OF '/' IN lv_origin_url1 RESULTS DATA(lt_match_result).

READ TABLE lt_match_result ASSIGNING FIELD-SYMBOL(<last_match>) INDEX ( lines( lt_match_result ) ).

DATA(lv_origin_prefix) = lv_origin_url1+0(<last_match>-offset).

lv_result1 = lv_origin_prefix && '/' && <file_name>.

" use regular expression

DATA(reg_pattern) = '(http://)([\.\w]+)/(\w+)/([\.\w]+)'.

DATA(lo_regex) = NEW cl_abap_regex( pattern = reg_pattern ).

DATA(lo_matcher) = lo_regex->create_matcher( EXPORTING text = lv_thumbnail_2 ).

CHECK lo_matcher->match( ) = abap_true.

DATA(lt_reg_match_result) = lo_matcher->find_all( ).

READ TABLE lt_reg_match_result ASSIGNING FIELD-SYMBOL(<reg_entry>) INDEX 1.

READ TABLE <reg_entry>-submatches ASSIGNING FIELD-SYMBOL(<match>) INDEX lines( <reg_entry>-submatches ).

DATA(file_name) = lv_thumbnail_2+<match>-offset(<match>-length).

DATA(lv_new) = |$1$2/$3/{ file_name }|.

lv_result2 = lv_origin_url1.

REPLACE ALL OCCURRENCES OF REGEX reg_pattern IN lv_result2 WITH lv_new.

ASSERT lv_result2 = lv_result1.

 


Viewing all articles
Browse latest Browse all 948

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>