Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

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

by Rapunzel (Novice)
on Oct 24, 2008 at 08:54 UTC ( [id://719291]=note: print w/replies, xml ) Need Help??


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

Thanks for quick reply. This way it has deleted "\" from substring. On printing $string i am getting >>this is my sting "with and /slashes". put substing in quotes But i want it this way >>this is my sting "with \ and /slashes". put substing in quote
  • Comment on Re^2: How to substitute a string containing "\" with same string in quotes.

Replies are listed 'Best First'.
Re^3: How to substitute a string containing "\" with same string in quotes.
by ikegami (Patriarch) on Oct 24, 2008 at 09:08 UTC
    No, $string and $substring don't contains any "\" to begin with.
    # The string these literals produce don't contains any "\". my $string = "my string with \ and / slashes."; my $substring = "with \ and / slashes"; print("$string\n"); print("$substring\n"); print("\n"); # This is what the literals should be. $string = "my string with \\ and / slashes."; $substring = "with \\ and / slashes"; print("$string\n"); print("$substring\n"); print("\n"); # It works. $string =~ s/\Q$substring\E/\"$substring\"/gi; print("$string\n");
    my string with and / slashes. with and / slashes my string with \ and / slashes. with \ and / slashes my string "with \ and / slashes".
Re^3: How to substitute a string containing "\" with same string in quotes.
by Punitha (Priest) on Oct 24, 2008 at 09:06 UTC

    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://719291]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (6)
As of 2024-04-18 15:04 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found