?
Solved

simple replace fails...

Posted on 2005-02-24
10
Medium Priority
?
154 Views
Last Modified: 2010-03-05
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
Comment
Question by:InGearX
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
10 Comments
 
LVL 84

Accepted Solution

by:
ozo earned 2000 total points
ID: 13400754
$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
 
LVL 84

Expert Comment

by:ozo
ID: 13400763
#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
 
LVL 16

Expert Comment

by:manav_mathur
ID: 13401234
Can't we simply do

$to_replace=quotemeta("$original_excerpts") ;


0
Technology Partners: 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!

 
LVL 84

Expert Comment

by:ozo
ID: 13401447
Or simpler
$to_replace=quotemeta($original_excerpts);
or
$to_replace="\Q$original_excerpts";
0
 
LVL 16

Expert Comment

by:manav_mathur
ID: 13401518
$to_replace="\Q$original_excerpts\E" ; ##??

Manav
0
 
LVL 84

Expert Comment

by:ozo
ID: 13401585
same thing.
0
 
LVL 16

Expert Comment

by:manav_mathur
ID: 13401593
Doesnt \Q have a corresponding \E....or will Perl, in its quest for searching \E, regex-quote the whole thing??

Manav
0
 
LVL 84

Expert Comment

by:ozo
ID: 13401669
In this case, $original_excerpts is the whole thing.
see
Gory details of parsing quoted constructs
in
perldoc perlop
0

Featured Post

Technology Partners: 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!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Six Sigma Control Plans

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question