Reputation: 1851
I am trying to replace some characters inside
And the results differ.
Example:
remove the last occurence of a semicolon ";", issued via.
REPLACE REGEX ';$' IN lv_atwrt_txt WITH ''.
WORKS for strings, but seems to fail for CHAR(30 in my case).
On the other hand, removing a last pipe ( if only ONE exists) in char(30) works, via this line
REPLACE REGEX '\|$' IN <content>-atnam WITH space.
BUT, then, aterwards, after 2 pipes do exist in the char(30) the replacement like this fails.
REPLACE REGEX '\|$' IN lv_atnam_txt WITH ''
Look for semicolons
Everything works perfekt if operating on a string and removing semicolons, regardless,how many, I want to find the last one and remove it... it works, but again, not for the "pipe" on a string, when it occurs more than once.
I replaced the pipe with an ampersand and the proplem is still the same...
I created a testprogram, which contained 3 pipes in the string and also one at the end. THIS WORKED as expected.
The difference is, that in my testprogram the string is populated directly during definition.
In my productive program the string is concatenated, sometimes I put an pipe at the end of the string. Later I need to remove this LAST pipe at the end... Now I debugged and figured out, that this DOES NOT fail, when I modify the string in the debugger... though it looks the same.
My testscenario is as follows:
When conatenating my string and then wanting to remove the last pipe... it fails, when initializing the string during definition, it works as expected. When modifying my concatenated string in the debugger, it works.
In the debugger I have a concatenated string with the length of 26 ( last pipe " | " ).
When I modify this, (I erase the space before the pipe and then enter it again), the string gets length of 25, and then the regex finds it and deletes it.
Can anybody explain this ?
UPDATED WITH CORRESPONDING LINES OF CODE:
LOOP AT lt_atnams_atwrts INTO DATA(line_x).
lv_atnam_txt = lv_atnam_txt && line_x-atnam_txt
&& ` `
&& zif_atnam_atwrt_const=>c_atnam_group_delim
&& ` `.
IF line_exists( inob_details[ objek = <content>-matnr ] ).
DATA(inob) = inob_details[ objek = <content>-matnr ] .
LOOP AT ausp_details INTO DATA(atinn) WHERE objek = inob-cuobj.
IF line_exists( cabn_details[ cabn_atnam = line_x-atnam
cawn_atinn = atinn-atinn ]
).
lv_atwrt_txt = lv_atwrt_txt && cabn_details[ cabn_atnam = line_x-atnam
cawn_atinn = atinn-atinn
]-cawn_atwtb
&& zif_atnam_atwrt_const=>c_atnam_atwrt_delim.
ENDIF.
ENDLOOP.
REPLACE REGEX ';$' IN lv_atwrt_txt WITH ''.
ENDIF.
IF lv_atwrt_txt IS NOT INITIAL.
lv_atwrt_txt = lv_atwrt_txt && ` | `.
ENDIF.
ENDLOOP.
ENDLOOP.
" THOSE TWO FAIL
REPLACE REGEX '\|$' IN lv_atwrt_txt WITH space.
REPLACE REGEX '\|$' IN lv_atnam_txt WITH space.
Upvotes: 0
Views: 295
Reputation: 5554
That is because the REGEX considers the whole 30 characters of CHAR30. Then you are replacing the last character which is a SPACE
.
Try this:
data: lv_atwrt_txt type atwrt,
lv_atwrt_str type string.
START-OF-SELECTION.
lv_atwrt_txt = 'Hello ;'.
lv_atwrt_str = 'Hello;'.
REPLACE REGEX ';$' IN lv_atwrt_txt WITH ''.
REPLACE REGEX ';$' IN lv_atwrt_str WITH ''.
WRITE lv_atwrt_txt.
WRITE / lv_atwrt_str.
You will have this output:
Hello
Hello
Hexadecimal content of lv_atwrt_txt
:
480065006C006C006F002000200020002000200020002000200020002000200020002000200020002000200020002000200020002000200020002000
Hexadecimal content of lv_atwrt_str
:
480065006C006C006F00
Upvotes: 3