Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

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

by Punitha (Priest)
on Oct 24, 2008 at 08:48 UTC ( #719287=note: print w/replies, xml ) Need Help??


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

Hi

Please note that in your example coding the '$sting' and '$substing' are misspelled in some occurances, to avoid this use 'use strict;'

You can escape those slashes in the matching string by using '\Q and \E' like

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

Punitha

  • Comment on Re: How to substitute a string containing "\" with same string in quotes.
  • Download Code

Replies are listed 'Best First'.
Re^2: How to substitute a string containing "\" with same string in quotes.
by Rapunzel (Novice) on Oct 24, 2008 at 08:54 UTC
    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
      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".

      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
Node Status?
node history
Node Type: note [id://719287]
help
Chatterbox?
[Corion]: Meh, first round of escalations for me not wanting to fix in production what a project has mismanaged. Now another project, which eats up all the resources until end of this year wants to take that task and put it on my list of things as well.
[Corion]: So now there will be the fun of me explaining to the project that $other_project had low priority because $project has high priority. If $project picks up the task from $other_project, they also will need to reprioritize my tasks to get that done...

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (8)
As of 2017-08-17 12:18 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Who is your favorite scientist and why?



























    Results (287 votes). Check out past polls.

    Notices?