Skip to Content
Author's profile photo Jerry Wang

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

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.



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

The population logic should be:

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

thumbnail 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.


DATA: lv_origin_url1 TYPE STRING value '',

lv_thumbnail_2 TYPE STRING value '',

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.​

Assigned Tags

      You must be Logged on to comment or reply to a post.
      Author's profile photo Rüdiger Plantiko
      Rüdiger Plantiko

      I would prefer this - shorter - regex version:

      data: thumbnail_filename type string.

      find regex '([^/]+)$' in lv_thumbnail_2 submatches thumbnail_filename.
      replace regex '[^/]+$' in lv_origin_url1 with thumbnail_filename.

      Author's profile photo Jerry Wang
      Jerry Wang
      Blog Post Author

      Hi Rüdiger,

      you are expert! That is fantastic. I will use it in my code 🙂

      Best regards,