how to escape a quote

In a script that does a search and replace in multiple documents I am doing the following.

$search=""MEDHOD="POST"";
$replace=""METHOD="GET"";

The problem is that the quotes that are in the string that I am searching for conflict with the quotes that surround the entire string in perl.  

How do I make it so this works.

Thanks

1stomniAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

olthoffCommented:
Two possible ways

$search='"MEDHOD="POST"';
$replace='"METHOD="GET"';

OR

$search="\"MEDHOD=\"POST\"";
$replace="\"METHOD=\"GET\"";
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
jhurstCommented:
$search="\"METHOD=\"POST\"";

However, I think you are still one set short since you will note that there are quotes starting at METHOD and ending after the =
0
1stomniAuthor Commented:
This worked :) thanks....

$search='"MEDHOD="POST"';
$replace='"METHOD="GET"';

However I need one more thing.....

When I tried to do this it can't find it when the question mark is in there...  I have tried with and without the \.  If I change the string to only include up to php3 it works, only when I include the question mark it won't find it... please help :)  I included the script below.

$search="http://www.1888wwsports.com/phpshop/cart.php3\?action=add";

$replace="http://www.1888wwsports.com/phpshop/cart.php3";


#!/bin/perl
#
# Kevin Kadow (kadokev@msg.net) <A HREF="http://www.msg.net/kadow/">http://www.msg.net/kadow/</A>
# Can be downloaded from
#<A HREF="http://www.msg.net/utility/small/search.html">http://www.msg.net/utility/small/search.html</A>
# For 1stomni, free for all to use as long as the comments are kept intact!
#
# Search and replace strings in html files
#
require 'find.pl';

#
# Change these variables as needed.
#
$dir="/export/home/worldwide/htdocs/clothingtest";

$search="http://www.1888wwsports.com/phpshop/cart.php3\?action=add";

$replace="http://www.1888wwsports.com/phpshop/cart.php3";

warn <<"EOF";
Starting search/replace script $0
  Search all files below $dir
  Looking for string $search
  Replace the string with $replace

EOF


#
# Do the actual work
#
&find($dir);
warn "Scanned $files making $changes changes.\n";
exit(0);

################ SUBROUTINES FOLLOW #################################

#wanted
#
# This routine is called by 'find', once for each file, divectory, etc.
#
sub wanted {

#Skip everything except files named .html or .shtml
#return unless( -f $_ && m/\.[s]*html$/);
#Skip everything except files named .html or .shtml
return unless( -f $_ && m/\.s?HTML?$/i);

&update("$dir/$_");
}


#update
#
# Do the search-and-replace on a given file
#
sub update {
local($file)=@_;

local(@contents);

# Open the file for reading and writing (the + does that)
unless(open(FILE,"+<$file")) {
      warn "Cannot open $file for updating, Error: $!\n";
      return;
      }

warn "Updating $file\n";
$files++;

# Read the contents into an array, replacing the string as we go
while ($_=<FILE>) {
      $changes += s/$search/$replace/ig;
      push(@contents,$_);
      }

# Go to the beginning of the file
seek(FILE,0,0);

# Update the contents with the data from our array
print FILE (@contents);
close(FILE);
}

###EOF###

0
olthoffCommented:
I have the same problem and haven't figured out.  I use index and substr as a replacement.

$x = index($Search, $Replace);
if ($x > -1) {
  $_ = substr($_, 0 $x) . $Replace . substr($_, $x+length($Search)+1);
};

0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Perl

From novice to tech pro — start learning today.