Application Development 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: 

ABAP Help with REPLACE REGEX

sap_cohort
Active Contributor
0 Kudos

Hi, I need to replace a Fiscal Period value YYYYPPP found in a very long string 2000+ in length. I have a routine that determines the correct Fiscal Period to replace the current value in the string.

Below is an example of the string with the Fiscal Period entry in it. Its currently "2019001" but could be any value and needs to be replaced.

So if I sum it up I need to replace the 7 digit value that is found IMMEDIATELY after the following string token
<ID>0FISCPER</ID><Operator>1</Operator><LowValue>

Does anyone know how to create an ABAP Regex to accomplish this?
I am trying to accomplish this with REGEX_TOY to no avail.

Thanks for any thoughts.

...<Attribute><ID>0GL_ACCOUNT</ID><Operator>3</Operator><LowValue>0090000000</LowValue><HighValue>0099999999</HighValue></Attribute>###<Attribute><ID>0FISCPER</ID><Operator>1</Operator><LowValue>2019001</LowValue><HighValue /></Attribute>...
6 REPLIES 6

DoanManhQuynh
Active Contributor

That string in xml format so you should use sXML Library to read and write instead of replace.

sap_cohort
Active Contributor
0 Kudos

Hi, Thanks for the input all!
This is the solution I came up with. Probably not the most elegant or proper, but I thought I would share in case it helps anyone down the line.
This basically reads(imports) an ABAP Variant in table VARI for program UJD_TEST_PACKAGE_LINK or UJD_TEST_PACKAGE and updates the PROMPT Parameter with a new FISCPER Value. The value was located and updated using REPLACE REGEX.

I was not able to use any of the standard FM to read the ABAP Variant Parameter value because the value length was greater than 255 and is stored in a scrambled format so I had to use IMPORT/EXPORT for table VARI.
Hope that helps.

DATA: BEGIN OF varid_key,
        report  TYPE raldb_repo,
        variant TYPE raldb_vari,
      END OF varid_key.
DATA: lv_prompt      TYPE string,
      lv_src_token   TYPE string,
      lv_tgt_token   TYPE string,
      lv_date        TYPE datum,
      lv_fiscper     TYPE /bi0/oifiscper,
      lv_offset_char TYPE rvari_val_255,
      lv_offset_int  TYPE i,
      lo_fm          TYPE REF TO zcl_fm_wrapper.

PARAMETERS: p_report TYPE raldb_repo DEFAULT 'UJD_TEST_PACKAGE_LINK'.
PARAMETERS: p_varnt  TYPE raldb_vari.

START-OF-SELECTION.
  lo_fm = NEW #( ).

  SELECT SINGLE low FROM tvarvc INTO lv_offset_char WHERE name = 'ZPKG_LINK_DAY_OFFSET'.
  IF sy-subrc = 0.
    lv_offset_int = CONV int4( lv_offset_char ).
    lv_date = sy-datum - lv_offset_int.
  ENDIF.

  lv_fiscper   = lo_fm->convert_date_to_fiscper( i_date = lv_date i_periv = 'V3').
  lv_src_token = '(<ID>0FISCPER</ID><Operator>1</Operator><LowValue>)(\d{7})'.
  lv_tgt_token = |<ID>0FISCPER</ID><Operator>1</Operator><LowValue>{ lv_fiscper }|.

  varid_key-report  = p_report.
  varid_key-variant = p_varnt.
  IMPORT prompt TO lv_prompt FROM DATABASE vari(va) ID varid_key.
  REPLACE REGEX lv_src_token IN lv_prompt WITH lv_tgt_token.
  EXPORT prompt FROM lv_prompt TO DATABASE vari(va) ID varid_key.

Sorry, but is not better create a transformation of this?

it better to use xml library to read/write the prompt value, you can see in that program itself the subroutine PARSE_PROMPT_XML, how SAP parse the prompt parameter.

sap_cohort
Active Contributor
0 Kudos

Not sure how one would accomplish the same logic in a BPC Transformation File.
So you are saying that the Transformation could change the Package Selection for Fiscal Period?
I'm open to any suggestions that would help the cause... Thanks

former_member564522
Active Participant

It is an XML string. Advise you to use XML class library and then update the value . You can then convert it back using Parse.