CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Regular Expressions:
Need solution for replacing commas in a comma separated string

 



abhinavg6
New User

Apr 3, 2009, 7:50 AM

Post #1 of 2 (2800 views)
Need solution for replacing commas in a comma separated string Can't Post

Hi,

I have a comma (,) separated csv file as input to the process. Now in some of the lines in that csv, there are fields which are enclosed between "" but there's a comma in the field value too. For example, a CSV record could look like:

MOD,USD,22-Oct-09,Pay,USNY,"11.21 partial, 1.25mm remains",FALSE,,MOD,USD

If you look at the field in Bold, it's enclosed in "", but there's a comma in the value. Now when we split this whole record using comma (,) as the separator, the split function is not able to differentiate between the CSV comma separator and the comma in field value. Hence it recognizes (11.21 Partial) and (1.25mm remains) as different fields. This totally screws up our backend process. Can somebody suggest a generic regex here, which can replace any comma that appears in the "" fields by a colon (;) ? Please note that there could be multiple "" fields with commas inside in a CSV record. For example:

MOD,USD,22-Oct-09,Pay,USNY,"11.21 partial, 1.25mm remains",FALSE,"paint 22, leave 43",MOD,"drive neo, zion is falling",USD

Thanks in advance for a solution.

Regards,

Abhi


KevinR
Veteran


Apr 3, 2009, 9:54 AM

Post #2 of 2 (2795 views)
Re: [abhinavg6] Need solution for replacing commas in a comma separated string [In reply to] Can't Post

You can use the core module Text::ParseWords or you can install Text::CSV_XS, both can handle embedded commas.
-------------------------------------------------

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives