Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Re: seek and replace for exceptional characters

by Laurent_R (Vicar)
on Sep 23, 2013 at 18:00 UTC ( #1055362=note: print w/ replies, xml ) Need Help??


in reply to seek and replace for exceptional characters

Using the quotemeta function is the simplest solution. But so that you understand the underlying reason, your search regex could also be defined as follows (simply escaping the '+' sign):

my $A = qr (\+#);


Comment on Re: seek and replace for exceptional characters
Download Code
Re^2: seek and replace for exceptional characters
by semipro (Novice) on Sep 23, 2013 at 18:18 UTC
    Hi, I just tried quotemeta with my new $A code which is '#i1*#lper/2+#ginsl-#cdz/2'. But it does not work with and without quotementa. I know its a mix of everything but somehow I would like to replace it without interpretation. So my code would be now:
    #!/usr/bin/perl -w #use strict; my $SCU = 'C:/Users/user/Desktop/a.txt'; open (FILE, '<', $SCU) or die "$SCU File not found : $!"; my @lines = <FILE>; close (FILE); my $A= quotemeta '#i1*#lper/2+#ginsl-#cdz/2'; my $B= '$coin22'; my @newlines; push @newlines, s/$A/$B/rg for @lines; open (FILE, '>', $SCU) or die "Could not open file $SCU: $!"; print FILE @newlines; close (FILE);
    Somebody has a suggestion for me? Thanks a lot! Best regards
      Somebody has a suggestion for me?
      Yes. Show a line of your input file which should match this regular expression.
      Suggestion #1: Uncomment the use strict; line. Set it to autodestruct your computer if you ever accidentally comment it out again.

      Suggestion #2: Withdrawn. I just upgraded to v5.16.3 and your script runs just fine.

      Suggestion #3: Supply test data as previously noted. :-)

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (5)
As of 2014-08-23 16:39 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (175 votes), past polls