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: 

Regular Expression Help

GrahamRobbo
Active Contributor
0 Kudos

Hi there,

can anyone help me with a regular expression.

I want to replace all delimiting commas in a CSV string with another character. The string contents look something like this...

1,10,Johnson,"Stores and Sales",High,10031984,paycycle1,"red,blue,green",full

...so what I want to change it to is this...

1|10|Johnson|"Stores and Sales"|High|10031984|paycycle1|"red,blue,green"|full

My problem is with the regex syntax. The closest I have got to is...

,(?![^"]*")

..but this produces the result...

1,10,Johnson,"Stores and Sales",High,10031984,paycycle1,"red,blue,green"|full

...as it only matches the commas after the last " delimited field.

Any help appreciated.

Cheers

Graham Robbo

7 REPLIES 7

Former Member
0 Kudos

Hi,

Use

REPLACE ALL OCCURANCES OF ',' IN <variable> WITH '|' IN CHARACTER MODE

Regards,

Mohaiyuddin

0 Kudos

replace all gives


1|10|Johnson|"Stores and Sales"|High|10031984|paycycle1|"red|blue|green"|full

not


1|10|Johnson|"Stores and sales"|High|10031984|paycycle1|"red,blue,green"|full

0 Kudos

Thanks, I missed that part.

Maybe some logic can help.



data : var_overlay(1000) type c.
data : var1(1000) type c,
         l_total_len type i,
         l_count type i,
         l_index type i,
         l_remainder type i

var1 = <your string>.

l_total_len = strlen( var1 ).

clear l_index.
clear l_count.
clear var_overlay.

do l_total_len times.
l_index = sy-index - 1.
if var1+l_index(1) = '"'.
l_count = l_count + 1.
endif.

l_remainder = l_count MOD 2.
if l_remainter = 1. " start of pattern
var_overlay+l_index(1) = var1+l_index(1).
else. " end of pattern
endif.

enddo.

REPLACE ALL OCCURANCES OF ',' in var1 with '|' RESULTS var1.

OVERLAY var_overlay WITH var1.

*var_overlay will contain your desired result.

Regards,

Mohaiyuddin

Former Member
0 Kudos

data:char(125) .

char = '1,10,Johnson,"Stores and Sales",High,10031984,paycycle1,"red,blue,green",full'.

replace all occurrences of ',' in char with '/' .

write: char.

0 Kudos

Thanks for your contributions, but my question is regarding "[Regular Expressions|http://en.wikipedia.org/wiki/Regular_expressions]" not string handling.

If anyone can help me with a [regular expression|http://help.sap.com/saphelp_erp2005/helpdata/en/42/9d6ceabb211d73e10000000a1553f6/content.htm] to achieve the desired result I would appreciate it.

Thanks

Graham Robbo

GrahamRobbo
Active Contributor
0 Kudos

No solution provided. Closing thread.

0 Kudos

Hi Graham,

You may need to employ 3 steps to accomplish your goal:

1. Break up the string into fields using the regex

(([^,]+)|([\s]*"([^"]|"[^,])*")),?

for example

FIND ALL OCCURRENCES OF REGEX regexp IN string RESULTS lt_result.

2. Replace last comma in each field with '|'

REPLACE FIRST OCCURRENCE OF REGEX '(?:,)$' IN field WITH '|'.

3. Concatenate the fields back into one string

Imad