Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Re: CSV and regex mixups

by BrowserUk (Patriarch)
on Jul 02, 2003 at 23:35 UTC ( [id://271009]=note: print w/replies, xml ) Need Help??


in reply to CSV and regex mixups

If your trying to escape the embedded quotes before passing it to Text::CSV, then this might do the trick.

Note: I've added some extra embedded quotes to check that it doesn't escaped already escaped quotes and handles the edge cases at either end of the string. You'll need to throw a few more tests at this before using it in anger.

$s='"crosby","stills","nash",""and" ""sometimes"" "young""' # capture everything between a quote and a quote # follow by a comma or the end of string $s =~ s[ " (.*?) " ( , | $ ) ] { # Look for unescaped embedded quotes and escape them (my $t = $1) =~ s[ (?<!") " (?!") ][""]gx; # put back the outer quotes and the comma if there was one '"' . $t . '"' . ($2||''); }gex; print $s; "crosby","stills","nash","""and"" ""sometimes"" ""young"""

This assumes that $s would otherwise be parsed correctly by Text::CSV.


Examine what is said, not who speaks.
"Efficiency is intelligent laziness." -David Dunham
"When I'm working on a problem, I never think about beauty. I think only how to solve the problem. But when I have finished, if the solution is not beautiful, I know it is wrong." -Richard Buckminster Fuller


Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others perusing the Monastery: (3)
As of 2024-04-19 23:54 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found