Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot

Re: Recommendations for parsing invalid CSV

by mscharrer (Hermit)
on Apr 21, 2008 at 14:54 UTC ( #681931=note: print w/replies, xml ) Need Help??

in reply to Recommendations for parsing invalid CSV

You could use look-behind (?<= ) and look-ahead (?= ) expressions to look only for quotes which are not beside a comma or at end and start of the line:
s/(?<=.)(?<!,)"(?!,|$)/\\"/g A simple test in the command line brings me:
user@machine$ perl -pe 's/(?<=.)(?<!,)"(?!,|$)/\\"/g' "call from "friend"","call from "friend"","call from "friend"" "call from \"friend\"","call from \"friend\"","call from \"friend\""
The 2nd line is input, the 3rd output.

Looks good for me. Some special cases with escaped commas inside the strings might not match correctly. You should check this.

Replies are listed 'Best First'.
Re^2: Recommendations for parsing invalid CSV
by markjugg (Curate) on Apr 21, 2008 at 15:24 UTC

    I didn't mean for anyone else to do my work for me, but I certainly don't mind the help! Thank you! That's the kind of code I would need to write if the new release of Text::CSV_XS turns out not to work for some reason.

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://681931]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (6)
As of 2023-05-31 11:25 GMT
Find Nodes?
    Voting Booth?

    No recent polls found