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: 

Can I use REGEX to remove last character in a file

former_member201275
Active Contributor
0 Kudos
408

I have a file on the sap server which looks something like this:

0001#Text#gfdkgh#

0002#more#ljjpoir#

0003#then#ljddpoiw#

I want a program to remove the last hash character from each line.

I can read this file with OPEN DATASET etc, then REVERSE the line, and REPLACE FIRST CHAR with SPACE. This works fine but I wanted to find out if it could be done with REGEX as it might be more economical?

All help greatly appreciated. Thank you.

1 ACCEPTED SOLUTION

Former Member
0 Kudos
166

You could use

REPLACE FIRST OCCURRENCE OF REGEX '(.*)#$' IN line WITH '$1'.


but a simply


SHIFT line RIGHT DELETING TRAILING '#'.


should also do the job.


BR,

Gábor

5 REPLIES 5

Former Member
0 Kudos
167

You could use

REPLACE FIRST OCCURRENCE OF REGEX '(.*)#$' IN line WITH '$1'.


but a simply


SHIFT line RIGHT DELETING TRAILING '#'.


should also do the job.


BR,

Gábor

0 Kudos
166

ah yes! good idea! i will try this.

Thank you.

Former Member
0 Kudos
166

Could it be that your server is UNIX and the file from Windows? I'd check the last character in debug - if it is x13 (CR), the file was likely FTPed in binary mode, not ASCII mode. Check the addition 'smart line feed' in 'open dataset'. It ignores CR.

I know, not a direct answer to your question, but it may save you some hassle...

Wolfgang

0 Kudos
166

Well well well......

Mr Propfe - A very long time no see.  I hope you are doing well ?

Regards,

Rich

Oh - and to answer the question,  a # is not always a # - it's a non-printable character with an ascii code less than $0F.

Rich

Clemenss
Active Contributor
0 Kudos
166

Hi Glen,

regex is extremely powerful but quite slow. Test the perfromance difference of

REPLACE FIRST OCCURRENCE OF REGEX '(.*)#$' IN line WITH '$1'

against

SHIFT line RIGHT DELETING TRAILING CL_ABAP_CHAR_UTILITIES=>NEWLINE. "or CR_LF

The character displayed as '#' is probably a contraol character, check out the CL_ABAP_CHAR_UTILITIES attributes.

Regards  Clemens

.