Rapunzel has asked for the wisdom of the Perl Monks concerning the following question:

I have a string that may contain backslash and forward slash. I have to search this substring in a string and if found I have to replace the substring with the substring in double quotes. Following is the code snippet which I am using. Let me know if this could be done in much neat way.
1 my $sting = "this is my sting with \ and /slashes. put substing in q +uotes"; 2 my $substring = "with \ and /slashes"; 3 $substing =~ s/\\/\\\\/g; 4 $string =~ s/$substring/\"$substring\"/gi; 5 $string =~ s/\\\\/\\/g;
I dont want to use line 3 and 5 of my code.

Please let me know better way to do this.

Thanks,
Rapunzel
  • Comment on How to substitute a string containing "\" with same string in quotes.
  • Download Code

Replies are listed 'Best First'.
Re: How to substitute a string containing "\" with same string in quotes.
by ikegami (Patriarch) on Oct 24, 2008 at 08:47 UTC
    $string =~ s/\Q$substring\E/\"$substring\"/gi;

    \Q..\E uses quotemeta to convert the substring into a regexp pattern that matches the substring.

      There is no real need to escape the quotes :)
      $string = 'a b c la la a b c la la'; $substring = 'b c'; $string =~ s/\Q$substring\E/"$substring"/gi; print $string; __END__ a "b c" la la a "b c" la la
Re: How to substitute a string containing "\" with same string in quotes.
by Punitha (Priest) on Oct 24, 2008 at 08:48 UTC

    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

      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