Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Re^3: How to substitute a string containing "\" with same string in quotes.

by Punitha (Priest)
on Oct 24, 2008 at 09:06 UTC ( [id://719295]=note: print w/replies, xml ) Need Help??


in reply to Re^2: How to substitute a string containing "\" with same string in quotes.
in thread How to substitute a string containing "\" with same string in quotes.

Hi

Check the input itself is not containing the '\', because it is interpolated in double quotes.

use strict; my $string = "this is my sting with \ and /slashes. put substing in qu +otes"; my $substring = "with \ and /slashes"; print $string; $string =~ s/\Q$substring\E/\"$substring\"/gi; OUTPUT: this is my sting with and /slashes. put substing in quotes

Instead use single quote in the input like,

use strict; my $string = 'this is my sting with \ and /slashes. put substing in qu +otes'; my $substring = 'with \ and /slashes'; print $string; $string =~ s/\Q$substring\E/\"$substring\"/gi; OUTPUT: this is my sting with \ and /slashes. put substing in quotes

This will give the correct output

Punitha

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (2)
As of 2024-04-26 05:13 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found