Page 1 of 1

Replace commas within quotes

Posted: Thu Mar 12, 2009 10:58 pm
by obfusc88
Problem:
Cannot parse string with comma delimiters. Plan to temp replace some commas with tildes, parse the result, then replace the original commas.

Need help to replace "internal" commas with tildes, and remove double quotes. Would like to do in one RegEx if possible.

Replace commas within double quotes with tildes and eliminate double quotes
Length of double quoted strings may vary

Original string:
red,white,blue,"555,000,000",green,"23,000",yellow,...

Final result:
red,white,blue,555~000~000,green,23~000,yellow,...

The sample line is abbreviated, it could be hundreds of values on the line, it is a database dump

After I have final result each value will go on its own line and the tildes will be replaced with commas. Don't need help with that part.

Posted: Fri Mar 13, 2009 1:41 am
by Bob Hansen
Was not able to do this in one RegEx, but try this, allows for long strings with 3 commas:

Search for: "([^,]*),*([^,]*),*([^,]*),*([^,]*)"
Replace with: \1~\2~\3~\4

Then remove trailing tildes in center of string:
Search for: ~*,
Replace with: ,

Then remove trailing tildes at end of string:
Search for: ~*\n
Replace with: \n