08-07-2008 8:56 AM
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
08-07-2008 9:01 AM
Hi,
Use
REPLACE ALL OCCURANCES OF ',' IN <variable> WITH '|' IN CHARACTER MODE
Regards,
Mohaiyuddin
08-07-2008 9:17 AM
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
08-07-2008 10:37 AM
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
08-07-2008 10:48 AM
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.
08-08-2008 12:58 AM
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
09-03-2008 4:47 AM
09-16-2014 6:01 PM
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