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

Re: Replace commas with spaces between quotes, parsing CSV

by bart (Canon)
on Apr 16, 2016 at 04:28 UTC ( #1160598=note: print w/replies, xml ) Need Help??


in reply to Replace commas with spaces between quotes, parsing CSV

Here's mine, a different approach, based on the idea "replace what you want to keep by itself":
s/(,)|(".*?")/ $1 || $2 =~ s(,)( )gr /ge;
This requires a "fairly recent" perl, though I'm unsure exactly when s///r was introduced.

For older (and for newer) perls, you can use the slightly more verbose:

s/(,)|(".*?")/ $1 || do { (my $s = $2) =~ s(,)( )g; $s } /ge;

Replies are listed 'Best First'.
Re^2: Replace commas with spaces between quotes, parsing CSV
by kcott (Bishop) on Apr 17, 2016 at 04:13 UTC
    "... I'm unsure exactly when s///r was introduced."

    It was added in v5.14.0. See the Non-destructive substitution section of perl5140delta: Regular Expressions.

    — Ken

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (3)
As of 2020-06-02 00:42 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Do you really want to know if there is extraterrestrial life?



    Results (12 votes). Check out past polls.

    Notices?