• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 155
  • Last Modified:

simple replace fails...

My Perl script replaces certain text in certain files at certain times...

Problem is that script that is to be replaced contains many regular expression special characters, i.e. $, ., +, *...
I escape them all via a backward slash "\"
original excerpts: "+AFw-n Dear +AHs-0+AH0 +AHs-1+AH0,+AFw-n+AFw-n It's not: $50 but +AHs-2+AH0 thanks. +AFw-n "
so that
$to_replace = "\+AFw-n Dear \+AHs-0\+AH0 \+AHs-1\+AH0,\+AFw-n\+AFw-n It\'s not: \$50 but \+AHs-2\+AH0 thanks\. \+AFw-n"
and then
$all_text =~ s/$to_replace/the new text is here or a scalar/gi;

But the text fails to be replaced for some reason...
Maybe I'm forgetting something? some special characters? any lists?
Maybe I should just import text into a scalar from a file?

What could it be?
All ideas are welcome, thank you...
0
InGearX
Asked:
InGearX
  • 5
  • 3
1 Solution
 
ozoCommented:
$all_text=q"+AFw-n Dear +AHs-0+AH0 +AHs-1+AH0,+AFw-n+AFw-n It's not: $50 but +AHs-2+AH0 thanks. +AFw-n ";
$to_replace = q"+AFw-n Dear +AHs-0+AH0 +AHs-1+AH0,+AFw-n+AFw-n It's not: $50 but +AHs-2+AH0 thanks. +AFw-n";
$all_text =~ s/\Q$to_replace\E/the new text is here or a scalar/gi;
print $all_text;

0
 
ozoCommented:
#look at
$to_replace = "\+AFw-n Dear \+AHs-0\+AH0 \+AHs-1\+AH0,\+AFw-n\+AFw-n It\'s not: \$50 but \+AHs-2\+AH0 thanks\. \+AFw-n";
print $to_replace;
#you will see that the qq is processing the \s
0
 
manav_mathurCommented:
Can't we simply do

$to_replace=quotemeta("$original_excerpts") ;


0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
ozoCommented:
Or simpler
$to_replace=quotemeta($original_excerpts);
or
$to_replace="\Q$original_excerpts";
0
 
manav_mathurCommented:
$to_replace="\Q$original_excerpts\E" ; ##??

Manav
0
 
ozoCommented:
same thing.
0
 
manav_mathurCommented:
Doesnt \Q have a corresponding \E....or will Perl, in its quest for searching \E, regex-quote the whole thing??

Manav
0
 
ozoCommented:
In this case, $original_excerpts is the whole thing.
see
Gory details of parsing quoted constructs
in
perldoc perlop
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 5
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now