Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses

Re: seek and replace for exceptional characters

by Laurent_R (Canon)
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 (\+#);

Replies are listed 'Best First'.
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
      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. :-)

      Somebody has a suggestion for me?
      Yes. Show a line of your input file which should match this regular expression.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1055362]
NodeReaper selects a flavor of Kool-Aid

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (2)
As of 2018-05-27 13:02 GMT
Find Nodes?
    Voting Booth?