Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Re: Replacing commas in a substring when it is between quotes (2 ways)

by tye (Sage)
on Mar 28, 2012 at 19:38 UTC ( [id://962246]=note: print w/replies, xml ) Need Help??


in reply to Replacing comas in a substring when it is between quotes

Using Text::CSV seems like a more likely way to get it right on the first try. But your idea isn't hard to do:

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

You can also replace the regex with something that handles escapes.

Just for variety, here is another approach:

my $in = 0; s{(")|,}{ if($1){ $in= !$in; $1 }elsif($in){ '-' }else{ ',' } }ge

- tye        

Replies are listed 'Best First'.
Re^2: Replacing commas in a substring when it is between quotes (2 ways)
by ZlR (Chaplain) on Mar 29, 2012 at 04:59 UTC
    Thanks Tye !! That's nice and neat, just what i need and a real time saver right now :)

    I had no idea one could use commands from inside the substitute side, that's great.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (2)
As of 2025-11-11 22:43 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    What's your view on AI coding assistants?





    Results (68 votes). Check out past polls.

    Notices?
    hippoepoptai's answer Re: how do I set a cookie and redirect was blessed by hippo!
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.