Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Re: Search & Replace doesn't work with string containing certain special characters

by wrog (Friar)
on Sep 09, 2014 at 11:03 UTC ( [id://1099973]=note: print w/replies, xml ) Need Help??


in reply to Search & Replace doesn't work with string containing certain special characters

Extracting the quoted strings and then searching for them again is kind of inefficient. Likewise for having to create a new regexp for every single quoted string. Perl goes to a fair amount of trouble to compile a regexp into something that walks through a string; the whole point of that is so that you can re-use that walk routine lots of times.

Meaning what you really want is a single regexp fixed in advance that gets applied to every line. Bonus points if you can have it be a walk-thingie that only has to do a single pass through the string (which then means you want to think about how you'd do the walk if you had to do it yourself). Try this

use strict; use warnings; while ( my $line = <DATA> ) { print "# ORIG: $line"; if ($line =~ m/"/g) { $line =~ s/\G([^,"]*(?:"[^"]*"[^",]*)*),/$1|/g; } print "# NEW: $line"; } __DATA__ ONE,"TWo,2",Three,Four,Five ONE,"$TWo,2",Three,Four,"Fi,ve,," ONE,"$TWo2","Three,(3)",Four,Five ONE,"$TWo2","Three,(3",Four,Five
  • Comment on Re: Search & Replace doesn't work with string containing certain special characters
  • Download Code

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (2)
As of 2024-04-24 23:00 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found