John,

Thank you ever so much - that seemed to work.

In my code I'm actually using variables for the names to replace:

so my substitute looks like this :

$line =~ s/("[^"]+"|\w+)/
$1 eq $oldList[$i] ? $newList[$i] :
$1 eq "$oldList[$i]" ? "$newList[$i]" : $1
/eg;

(I removed the q( ) parts from your script as it did n't work when I used variables - what does the q( ) part mean?)

Again, thank you,

martin




John W. Krahn wrote:


Martin Costello wrote:


Hello,



Hello,




I'm trying to do a search and replace on a file (specifically the word
transformC). A cut down version of the file is below:

blahblah -blah blah
   -blah blah -blah $blah -blah "Ground"
   -c "blah transformC" transformC;

blahblah
   -af transformC    "blah"    ($blah)
   -af AtransformC "blah"     ($blah)
   -af transformC_2 "blah"     ($blah)

a pattern like this seems to take care of this:
$line =~ s/([\s"])transformC([\W|\D])/$1TRANSC$2/g;

giving

blahblah -blah blah
   -blah blah -blah $blah -blah "Ground"
   -c "blah TRANSC" TRANSC;

blahblah
   -af TRANSC    "blah"    ($blah)
   -af AtransformC "blah"     ($blah)
   -af transformC_2 "blah"     ($blah)

however I want a pattern that will NOT change "blah transformC", but
will still replace transformC; "transformC" transformC




Here is one way to do it:

$ perl -e'
$text = q/

blahblah -blah blah
   -blah blah -blah $blah -blah "Ground"
   -c "blah transformC" transformC;

blahblah
   -af transformC    "blah"    ($blah)
   -af AtransformC "blah"     ($blah)
   -af transformC_2 "blah"     ($blah)

/;
$text =~ s/("[^"]+"|\w+)/
$1 eq q(transformC) ? q(TRANSC) :
$1 eq q("transformC") ? q("TRANSC") : $1
/eg;
print "$text\n";
'


blahblah -blah blah
   -blah blah -blah $blah -blah "Ground"
   -c "blah transformC" TRANSC;

blahblah
   -af TRANSC    "blah"    ($blah)
   -af AtransformC "blah"     ($blah)
   -af transformC_2 "blah"     ($blah)



John





Reply via email to